Problem getting commandeventargs in a team event after re-creating controls in the postback

I'm having trouble getting args batch events after a second click using the code below.

therefore - when I process a button click and create a new button to replace the one that was there, I lose the viewport the next time I click the button.

Any suggestions on what I need to do to get this to work? I cannot change the structure significantly, since I have to generate a variable number of completely unrelated buttons in the command handler.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            LinkButton btn = new LinkButton();
            btn.ID = "btn1";
            this.Panel1.Controls.Add(btn);
            btn.Command += new CommandEventHandler(myLinkButton_Command);          
        }
        else
        {
            LinkButton btn = new LinkButton();
            btn.ID = "btn1";
            this.Panel1.Controls.Add(btn);
            btn.Text = "My Button 1";
            btn.CommandArgument = "1";
            btn.Command += new CommandEventHandler(myLinkButton_Command);
        }
    }

    void myLinkButton_Command(object sender, CommandEventArgs e)
    {
        int newArg = Convert.ToInt32(e.CommandArgument) + 1;// empty string on second mouse click
        this.Panel1.Controls.Clear();
        LinkButton myLinkButton = new LinkButton();          
        myLinkButton.ID = "btn1";
        this.Panel1.Controls.Add(myLinkButton);
        myLinkButton.Text = "My Button " + newArg.ToString();
        myLinkButton.CommandArgument = newArg.ToString();
    }
}
+3
source share
2 answers

, . , () . , , viewstate , .

<asp:Panel ID="Panel1" runat="server">
</asp:Panel>

<asp:Panel ID="Panel1" runat="server" />

.

+5

CommandArgument Page_Load.

0

Source: https://habr.com/ru/post/1705983/


All Articles