开发者

Event not firing after setting OnClientClick in RowDataBound

I have an Asp.net GridView (populate with a data binding). One of my columns is a ButtonField (obviously with his own CommandName).

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?

Adding code for better understanding:

Aspx code:

<asp:GridVi开发者_开发问答ew 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:

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

EDIT:

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;" />

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;" />

the onclick event handler will return before it gets a chance to do the postback properly (it will just submit the form).

Doing this:

(e.Row.Cells[e.Row.Cells.Count - 1].Controls[0] as ImageButton).OnClientClick = "if (!confirm('Do action?')) { return false; }";

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.

(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!)


At first glance this seems fine. Have you tried running your code outside the TabContainer?

It might get you in the right direction.


Use sender as argument on ClientScript.GetPostBackClientHyperlink function so it will create client js event dinamicaly. Bellow the example in RowDataBound, where sender is GridView (VB). YourCommand will be passed as argument for RowCommand event to be raised.

e.Row.Cells(columnIndex).Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(sender, "YourCommand$" & e.Row.DataItemIndex))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜