doPostBack is undefined when programmatically adding button to gridview

1k views Asked by At

I'm populating a GridView programmatically and adding a button to the last row:

ButtonField bc = new ButtonField();
bc.ButtonType = ButtonType.Button;
bc.Text = "Edit";
bc.CommandName = "EditAsset";
grid.Columns.Add(bc);

In the markup I specify a handler:

<asp:GridView ID="gvListView" OnRowCommand="gvListView_RowHandler" runat="server"></asp:GridView>

I add the handler to the codebehind:

protected void gvListView_RowHandler(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "EditAsset")
    {

    }
}

When I run it and click a button in the gridview, I get a javascript error that __doPostBack is undefined. Anyone know why the js for that wouldn't be generated?

The dynamically generated button call looks like this:

onclick="javascript:__doPostBack(&#39;lvStuff$gvListView&#39;,&#39;EditAsset$0&#39;)"

edit: The doPostBack was generated on the page, but has some sort of comment around it?

//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
1

There are 1 answers

0
Jason Kelley On BEST ANSWER

Well, I feel silly. The problem was that I was trying to end my earlier script tags in the header wrong. I was doing this:

<script type="text/javascript" src="jslib/jquery-1.9.1.js" />

Instead of this:

<script type="text/javascript" src="jslib/jquery-1.9.1.js"></script>

Which was causing the javascript to fail. Didn't realize you couldn't close script tags in the xml fashion.