asp.net如何在按钮单击事件中添加动态LinkBut​​ton?

时间:2012-11-29 09:41:03

标签: asp.net

有没有办法在单击提交按钮后添加LinkBut​​ton并让它正确回发。以这种情况为例。

页面上载了一个上传控件和一个提交按钮。在做出选择并且用户单击提交按钮之后,我想在另一个分隔符中显示已使用文件名旁边的可选删除按钮上载的文件。问题是,当用户单击提交按钮时,我尝试在单击处理程序上添加控件,因为这是请求文件的位置,但是当我尝试在控件响应中添加链接按钮时,事件当然不会挂钩

<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);
}

有没有人有任何好的想法来解决这个问题?我已经浏览了整个网络,我认为我对页面生命周期有相当不错的把握。但这很烦人。我似乎经常遇到这些问题。

1 个答案:

答案 0 :(得分:0)

你可以在开头添加LinkButton pDelete,但是在Click上设置Visible和其他属性,那么它将始终存在并且问题将会消失。