3
I am feeding a webform with textboxes and buttons from a Repeater, according to the tuples(table people) returned in an SQL query, linking the code to specific attributes within these controls. For textboxes I am using the Hidden attribute, while for buttons I am using the Commandargument attribute in order to use it with the Oncommand method. Now I need the Oncommand method to identify which textbox(Hidden attribute) corresponds to the linked Commandargument so that I can enter the information in my database. Is there any way to do this check or even an easier way to achieve 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"]));
}
populaFormulario:
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:
Can you use JS? Because if you can, you then have the opportunity to make the call from a method that validates your function, and can even be asynchronous. Javascript will make it easier to find the textbox you want. But with C# in codebehind it is also possible.
– Andrew Paes
Of course, it could be in JS yes, however, I’m still new to JS and I have no idea how to develop a function that satisfies my goal. I could create the function to check the controls that have the same attribute value, but I would not know how to handle the insertion in the database after the recovery of this function, since I perform all the insertion in the button in the codebehind. How would you do that?
– Luiz Antonio Lima