How to change a Button’s Text property inside a for

Asked

Viewed 451 times

0

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

<%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 turns out that when in the event "Button3_click" I do the code below, the textbox gets value 0.

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

I’ve also tried declaring a public variable that gets the value of i and sends to the textbox but also doesn’t work. What I do?

1 answer

0

I recommend using Peater instead of 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.