如何以编程方式在 DetailsView 中创建按钮并为其指定 CommandName?
在DetailsView 中,我以编程方式创建一个删除按钮并为其指定一个CommandName。当我测试我的应用程序时,该按钮创建得很好,但是当我单击删除按钮时,什么也没有发生。如果我定期在 DetailsView 中创建具有相同 CommandName 的完全相同的按钮,则它可以正常工作并正确触发 DetailsView 的 ItemCommand。这意味着我在代码中创建按钮的方式有问题,但我不知道它是什么。我需要指定一个 UniqueID 或类似的东西吗?
这是我以编程方式创建删除按钮的代码,该按钮不起作用:
Public Sub GetAttachments(ByVal requestID As Integer)
Try
Dim pnlAttachments As Panel = dtlApplication.FindControl("pnlAttachments")
Dim btnDelete As New LinkButton
btnDelete.Text = "delete"
btnDelete.CssClass = "lblDeleteAttachment"
btnDelete.CommandName = "DeleteAttachment"
btnDelete.ID = "lnkDeleteAttachment"
pnlAttachments.Controls.Add(btnDelete)
Catch ex As Exception
'notify user on screen
lblGeneralError.Text = ex.ToString
lblGeneralError.CssClass = "red"
End Try
End Sub
如果我像这样定期创建按钮,它工作得很好:
<asp:LinkButton runat="server" ID="lnkDeleteAttachment" Text="delete" commandname="DeleteAttachment" CssClass="lblDeleteAttachment"></asp:LinkButton>
这是以编程方式创建的按钮的渲染页面输出,该按钮不起作用:
<a id="MainContent_dtlApplication_lnkDeleteAttachment" href="javascript:__doPostBack('ctl00$MainContent$dtlApplication$lnkDeleteAttachment','')">delete</a>
这里是正常创建的正常按钮的渲染页面输出:
<a id="MainContent_dtlApplication_lnkDeleteAttachment" href="javascript:__doPostBack('ctl00$MainContent$dtlApplication$lnkDeleteAttachment','')">delete</a>
您可以看到它们是相同的。
注意:我以编程方式创建它的原因是因为最终我将在 For Next 语句中添加几个删除按钮,此时我无法选择是否定期或以编程方式创建它。
Within a DetailsView I am creating a delete button programmatically and giving it a CommandName. The button gets created fine when I test my app, but when I click the delete button, nothing happens. If I create this exact same button with the same CommandName regularly in the DetailsView, it works perfectly and fires the ItemCommand of the DetailsView correctly. That means there is something wrong with the way I'm creating my button in the code, but I can't figure out what it is. Do I need to specify a UniqueID or something like that?
Here is my code that programmatically creates the delete button, which doesn't work:
Public Sub GetAttachments(ByVal requestID As Integer)
Try
Dim pnlAttachments As Panel = dtlApplication.FindControl("pnlAttachments")
Dim btnDelete As New LinkButton
btnDelete.Text = "delete"
btnDelete.CssClass = "lblDeleteAttachment"
btnDelete.CommandName = "DeleteAttachment"
btnDelete.ID = "lnkDeleteAttachment"
pnlAttachments.Controls.Add(btnDelete)
Catch ex As Exception
'notify user on screen
lblGeneralError.Text = ex.ToString
lblGeneralError.CssClass = "red"
End Try
End Sub
And if I create the button regularly like this, it works fine:
<asp:LinkButton runat="server" ID="lnkDeleteAttachment" Text="delete" commandname="DeleteAttachment" CssClass="lblDeleteAttachment"></asp:LinkButton>
Here is the rendered page output for the programmatically created button that doesn't work:
<a id="MainContent_dtlApplication_lnkDeleteAttachment" href="javascript:__doPostBack('ctl00$MainContent$dtlApplication$lnkDeleteAttachment','')">delete</a>
And here is the rendered page output for the regularly created button that works:
<a id="MainContent_dtlApplication_lnkDeleteAttachment" href="javascript:__doPostBack('ctl00$MainContent$dtlApplication$lnkDeleteAttachment','')">delete</a>
You can see they are identical.
Note: The reason I'm creating it programmatically is because eventually I'm going to add several delete buttons inside a For Next statement, and at this point I won't have a choice of whether or not to create it regularly or programmatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望触发 onclick 事件处理程序,则需要在每次回发时添加 onclick 事件处理程序。我的建议是,您在标记中将其声明为
visible
属性为 false,并在需要时使其可见。您不必处理此类事情,并且控件无论如何都不会呈现,除非在某个时刻设置为可见。You need to add the onclick event handler on every postback if you want it to fire. My suggestion is that you let it declared in your markup with a
visible
property to false and just make it visible when you need it to be visible. You won't have to deal with this kind of thing and the control will not be rendered anyway unless is set to visible at some point.请参考代码。我可以在button1_Command 中捕获按钮命令。但是,您需要附加事件处理程序-button1.Command += button1_Command;
Please refer to the code. I can catch the button command in button1_Command. However, you need to attach the event handler - button1.Command += button1_Command;
在标记中创建按钮然后以编程方式隐藏或显示它可能会更容易。
It might me easier to create the button in markup and then hide or show it programmatically.