TextBox and C # + ASP.NET Dynamic Button

2

I'm feeding a webform with textboxes and buttons from a repeater, according to the tuples (people table) returned in an SQL query, linking the code to specific attributes within those controls. For the textbox I'm using the hidden attribute, while for the buttons I'm using the CommandArgument attribute in order to use it with the OnCommand method. Now I need in the OnCommand method to identify which textbox (hidden attribute) corresponds to the linked CommandArgument so that I can insert information into my database. Is there any way to do this check or even an easier way to achieve that my goal?

My repeater:

<div class="col-md-6" runat="server">
     <asp:Repeater ID="rptControles" runat="server">
          <ItemTemplate>
               <div class="row">
                    <div class="col-md-12">
                         <div class="form-group">
                              <div id="formulario" class="input-group" runat="server">
                                   <asp:TextBox ID="txtURL" class="form-control" hidden='<%# DataBinder.Eval(Container.DataItem, "pes_codigo") %>' placeholder='<%# DataBinder.Eval(Container.DataItem, "pes_nome") %>' runat="server"></asp:TextBox>
                                   <span class="input-group-btn">
                                        <asp:Button ID="btnValidar" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "pes_codigo") %>' OnCommand="btnValidar_Command" CssClass="btn btn-default" runat="server" Text="Go!" CausesValidation="false" />
                                   </span>
                              </div>
                         </div>
                    </div>
               </div>
          </ItemTemplate>
     </asp:Repeater>
 </div>

Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
    populaFormulario(Convert.ToInt32(Session["codigo_evento"]));
}

populaFormula:

public void populaFormulario(int codigoEvento)
{
    ParticipanteDB parDB = new ParticipanteDB();

    rptControles.DataSource = parDB.SelecionarParticipantes(codigoEvento).Tables[0].DefaultView;
    rptControles.DataBind();
}

btnValidar_Command:

protected void btnValidar_Command(object sender, CommandEventArgs e)
{
    var button = (Button)sender;

    var textbox = (TextBox)button.Parent.FindControl("txtURL");

    TextBox1.Text = e.CommandArgument.ToString() + " - " + textbox.Text;
}

Example of return of textboxes and buttons:

    
asked by anonymous 23.12.2014 / 17:47

1 answer

1

Since your CommandArgument has the same value as the hidden attribute of the textbox, take advantage of it and use the CommandArgument itself, which is sent in CommandEventArgs.

EDIT: Since you want the value of the TextBox for the button, you can then use the Parent property of the sender (in the case of your button). The parent will be your form div. Once accessing this div, we use the FindControl method to fetch a control called txtUrl. Remembering to make the appropriate casts, here is the example:

   using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace WebApplication3
    {
        public partial class _Default : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
#region Só um exemplo
                var tabela = new DataTable();
                tabela.Columns.Add(new DataColumn { ColumnName = "pes_codigo" });
                tabela.Columns.Add(new DataColumn { ColumnName = "pes_nome" });

                tabela.Rows.Add("1", "João");
                tabela.Rows.Add("2", "Paulo");
                tabela.Rows.Add("3", "Pedro");
                tabela.Rows.Add("4", "Mateus");

                rptControles.DataSource = tabela;
                rptControles.DataBind();
#endregion
            }

            protected void btnValidar_Command(object sender, CommandEventArgs e)
            {
                var button = (Button)sender;//Pegamos o botão.

                var textbox = (TextBox)button.Parent.FindControl("txtUrl");//button.Parent é a div id= formulário, logo iremos procurar o textbox txtUrl nessa Div.

                Response.Write(textbox.Text);//Pronto, temos o valor do textbox que você quer.
                Response.Write(e.CommandArgument);//Em e.CommandArgument terá o valor de pes_codigo que vem da tabela pessoas.
            }

        }
    }

Note: I only added a CausesValidation="False" to the buttons, to avoid problems with validation.

    
23.12.2014 / 20:56