我有一個Asp.net GridView(使用數據綁定填充)。 我的一列是一個ButtonField(顯然帶有他自己的CommandName)。在RowDataBound中設置OnClientClick後事件不會觸發
的GridView_RowCommand作品完美,但如果我添加一個GridView_RowDataBound(其中我只需添加一個JavaScript確認)GridView_RowCommand事件不會在回發解僱。
可能是什麼問題/解決方案?
添加代碼,以便更好地理解:
.aspx的代碼:
<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#代碼:
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)
它的工作!謝謝! 我仍然想知道爲什麼「javascript:return confirm(\」Do action?\「);」不同於「if(!confirm('Do action?')){return false;}」 和btw你是對的:ASP.NET MVC FTW! – p4bl0 2010-01-20 10:35:36
我試着回答自己..如果點擊'否',但工作版本返回false,但如果點擊「是」,則返回true。 – p4bl0 2010-01-20 10:43:10
你已經得到它:)很高興我能幫上忙。 – 2010-01-20 12:01:35