ASP.NET的GridView ItemTemplate中的LinkButton支持RowCommand后的JavaScript确认 [英] ASP.NET GridView ItemTemplate LinkButton Support for RowCommand AFTER JavaScript Confirmation
问题描述
由于以下的GridView code:
Given the following GridView code:
<asp:GridView ID="gvReq" runat="server" DataSourceID="objdsReq" >
<Columns>
<asp:TemplateField HeaderText="Control">
<ItemTemplate>
<asp:LinkButton ID="lbdelete" runat="server" CommandArgument='<%# Container.DataItemIndex %>' ForeColor="Red" CommandName="DeleteReq">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="objdsReq" runat="server" SelectMethod="GetDataTable" >
<%-- parameter list --%>
</asp:ObjectDataSource>
在RowDataBound事件,JavaScript的code增加:
In the RowDataBound event, JavaScript code is added:
Protected Sub gvReq_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvReq.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim lbdelete As LinkButton = e.Row.Cells(DELETE_CELL).Controls.Item(1)
lbdelete.Attributes.Add("onclick", "javascript:if(confirm('Are you sure you want to delete?')){return true}else{return false}")
JavaScript的火灾,但RowCommand事件永远不会火 - 我猜,因为它只是由JavaScript处理:
The JavaScript fires, but the RowCommand event will never fire - I'm guessing because it is only handled by the JavaScript:
Protected Sub gvReq_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvReq.RowCommand
Dim dataItemIndex As Integer = Convert.ToInt32(e.CommandArgument)
Dim reqID As Integer = Convert.ToInt32(gvReq.DataKeys(dataItemIndex).Values(0))
If e.CommandName = "DeleteReq" Then
JavaScript的确认对话框被管理放在那里通过的要求。
The JavaScript confirmation dialog was put there by requirement by Management.
现在,我该如何让RowCommand事件处理程序,如果有人点击确定
来JavaScript的确认框火?
Now, how do I get the RowCommand Event Handler to fire if someone clicks OK
to the JavaScript confirm box?
推荐答案
可以使用的的OnClientClick
属性的LinkButton
而不是增加对的RowDataBound
?
Can you use the OnClientClick
attribute of the LinkButton
instead of adding the attribute on rowdatabound
?
此外,它似乎还没有真正建立在您的网格视图中的onrowcommand属性。
Also it appears you haven't actually set the onrowcommand attribute on your grid view.
例如:
<asp:GridView ID="gvReq" runat="server" DataSourceID="objdsReq" OnRowCommand="gvReq_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Control">
<ItemTemplate>
<asp:LinkButton ID="lbdelete" runat="server"
CommandArgument='<%# Container.DataItemIndex %>'
ForeColor="Red"
CommandName="DeleteReq"
OnClientClick="return confirm('Are you sure you want to delete?');"
>Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我beleive了JavaScript起着回发了很大的作用的链接按钮,这样添加的OnClick
在该行数据绑定我会影响回发的javascript。
I beleive that javascript plays a large role in postback for a link button, so adding the OnClick
in the row databound my be affecting the postback javascript.
另外,请查阅这篇文章出来这样做,而不是使用defaul JavaScript的confirm一个稍微票友的方式:的 http://mattberseth.com/blog/2007/07/confirm_gridview_deletes_with.html
Also check this article out for a slightly "fancier" way of doing this, instead of using the defaul javascript confirm: http://mattberseth.com/blog/2007/07/confirm_gridview_deletes_with.html
这篇关于ASP.NET的GridView ItemTemplate中的LinkButton支持RowCommand后的JavaScript确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!