简体   繁体   中英

asp.net How to add dynamic LinkButton in the button click event?

Is there a way to add LinkButton after a submit button is clicked and have it post back correctly. Take this scenario for instance.

A page is loaded with an upload control on it and a submit button. After a selection is made and the user clicks the submit button, I would like to show in another divider the files that have been uploaded with an optional remove button beside the file name. The issue is that when the user clicks the submit button I try to add the controls on the click handler because that is where the file is requested but when I try to add a linkbutton in the control response and the events of course do not hook up.

<form .....
<telerik:RadAsyncUpload ID="CtrlRadAsyncUpload" runat="server">
</telerik:RadAsyncUpload>
<asp:Button ID="CtrlSave" runat="server" Text="Submit Plans" />


protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    CtrlSave.Click += new EventHandler(CtrlSave_Click);
}

protected void Page_Load(object sender, EventArgs e)
{
 //the problem is here, the new files are not created until after telerik has processed it own button click. I could add the buttons here, but the files are not posted yet. So i try to add them in the button click event. see below.
}

void CtrlSave_Click(object sender, EventArgs e)
{
 any LinkButtons created and added to the controls collection are there, but they do not post back properly

 //get uploaded data
 LinkButton pDelete = new LinkButton();
            pDelete.Text = "Remove";
            pDelete.Command += new CommandEventHandler(pDelete_Command);
            pDelete.CommandArgument = pFile;
            pDelete.CommandName = "Delete";
            Controls.Add(pDelete);
}

Does any one have any good ideas to solve this? I have looked all around the web and I think I have a fairly decent grasp on the page lifecycle. But this is annoying. I seem to get stuck with these issues more often than not.

LinkButton pDelete在开始时添加LinkButton pDelete ,但是在“单击”上将其设置为“可见”和其他属性,则它将始终存在并且问题将消失。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM