How to change Text property of a Button within a for

0

I am having a question in ASP.NET, I am creating a loop of repetition inside the HTML so that 60 buttons appear, each with a different number in the Text, going from 0 to 59, and then I need to get that text and put it in a textbox. I was doing it as below:

<%for (int i = 0; i < 60; i++)
{%>
    <asp:Button ID="Button1" runat="server" Text="0" OnClick="Button3_Click" />
    <% Button1.Text = (i + 1).ToString(); 
}%>

But it happens that when in the "Button3_Click" event I make the code below, the textbox gets value 0.

protected void Button3_Click(object sender, EventArgs e)
{
    tbAssento.Text = Button1.Text;
} 

I've also tried to declare a public variable that receives the value of i and sends it to the textbox but does not work either. What do I do?

    
asked by anonymous 04.03.2017 / 20:56

1 answer

0

I recommend that you use repeater rather than for explicit:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Repeater runat="server" ID="rpBotoes" OnItemDataBound="rpBotoes_ItemDataBound">
  <ItemTemplate>
    <asp:Button ID="btnAssento" runat="server" Text="0" />
  </ItemTemplate>
</asp:Repeater>

And on the server:

protected void rpBotoes_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Button b = e.Item.FindControl("btnAssento") as Button;
    b.Text = e.Item.DataItem.ToString();
    b.Click += (senderBtn, eBtn) =>
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "teste", $"alert('{b.Text}')", true);
    };
}

I hope this helps.

    
04.03.2017 / 23:24