在设定的RowDataBound后的OnClientClick事件不触发 [英] Event not firing after setting OnClientClick in RowDataBound
问题描述
我有一个Asp.net GridView控件(与数据绑定填充)。
我的一个栏目是一个ButtonField字段(很明显,他自己的CommandName)。
I have an Asp.net GridView (populate with a data binding). One of my columns is a ButtonField (obviously with his own CommandName).
中的* GridView_RowCommand *作品完美,但如果我加* GridView_RowDataBound *(在此我简单地添加一个JavaScript确认)* GridView_RowCommand *事件未在回发解雇了。
The *GridView_RowCommand* works perfectly, but if i add a *GridView_RowDataBound* (in which I simply add a javascript confirm) the *GridView_RowCommand* event is not fired in the PostBack.
这可能是什么问题/解决方案?
What could be the problem/solution?
添加code为更好地理解:
Adding code for better understanding:
.aspx的code:
Aspx code:
<asp:GridView ID="GridView1" runat="server"
OnRowCommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="MyField1" HeaderText="MyField1" />
<asp:BoundField DataField="MyField2" HeaderText="MyField2" />
<asp:ButtonField Text="MyAction" ButtonType="Image" ImageUrl="myaction.gif" CommandName="myaction" />
</Columns>
</asp:GridView>
C#code:
c# code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
(e.Row.Cells[e.Row.Cells.Count - 1].Controls[0] as ImageButton).OnClientClick = "javascript:return confirm (\"Do action?\");";
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "myaction")
{
DoMyAction();
}
}
编辑:
我忘了告诉我的GridView是一个Ajax TabContainer的(AjaxControlToolkit)在
I forgot to tell that my GridView is inside an ajax TabContainer (AjaxControlToolkit)
推荐答案
这是什么是图像按钮通常发出的:
this is what is emitted normally for the Image button:
<input type="image" src="myaction.gif" alt="MyAction" onclick="javascript:__doPostBack('GridView1','myaction$1')" style="border-width:0px;" />
当您在$ C $设置的OnClientClick C-背后,它将prePEND您code,但仍增加__doPostBack函数调用,导致下面的HTML:
when you set OnClientClick in your code-behind, it will prepend your code but still add the __doPostBack function call, resulting in the following html:
<input type="image" src="myaction.gif" alt="MyAction" onclick="javascript:return confirm('Do action?');javascript:__doPostBack('GridView1','myaction$1')" style="border-width:0px;" />
的onclick事件处理程序将返回它得到一个机会做正确的回发(它只是提交表单)之前。
the onclick event handler will return before it gets a chance to do the postback properly (it will just submit the form).
这样做:
(e.Row.Cells[e.Row.Cells.Count - 1].Controls[0] as ImageButton).OnClientClick = "if (!confirm('Do action?')) { return false; }";
应该允许客户端点击事件运行__doPostBack功能,提高RowCommand事件服务器端的预期,当用户点击OK。
should allow the client-side click event to run the __doPostBack function and raise the RowCommand event server-side as expected when the user clicks OK.
(作为一个侧面说明,这是ASP.NET的WebForms的缺点之一的一个很好的例子 - 有很多的HTML生成,你只是无法控制的ASP.NET MVC FTW!)
(As a side-note, this is a good example of one of the drawbacks of ASP.NET WebForms - there is a lot of html being generated that you just have no control over. ASP.NET MVC FTW!)
这篇关于在设定的RowDataBound后的OnClientClick事件不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!