Richard Neil Ilagan
Richard Neil Ilagan

Reputation: 14747

asp:Button is not calling server-side function

I'm instantiating an asp:Button inside a data-bound asp:GridView through template fields. Some of the buttons are supposed to call a server-side function, but for some weird reason, it doesn't. All the buttons do when you click them is fire a postback to the current page, doing nothing, effectively just reloading the page.

Below is a fragment of the code:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" CssClass="l2 submissions" ShowHeader="false">
    <Columns>      

        <asp:TemplateField>            
            <ItemTemplate><asp:Panel ID="swatchpanel" CssClass='<%# Bind("status") %>' runat="server"></asp:Panel></ItemTemplate>                
            <ItemStyle Width="50px" CssClass="sw" />
        </asp:TemplateField>

        <asp:BoundField DataField="description" ReadOnly="true">                                
        </asp:BoundField>

        <asp:BoundField DataField="owner" ReadOnly="true">
            <ItemStyle Font-Italic="true" />
        </asp:BoundField>

        <asp:BoundField DataField="last-modified" ReadOnly="true">
            <ItemStyle Width="100px" />
        </asp:BoundField>

        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="viewBtn" cssclass='<%# Bind("sid") %>' runat="server" Text="View" OnClick="viewBtnClick" />
            </ItemTemplate>
        </asp:TemplateField>                                         

    </Columns>
</asp:GridView>

The viewBtn above should call the viewBtnClick() function on server-side. I do have that function defined, along with a proper signature (object,EventArgs). One thing that may be of note is that this code is actually inside an ASCX, which is loaded in another ASCX, finally loaded into an ASPX.

Any help or insight into the matter will be SO appreciated. Thanks!

(oh, and please don't mind my trashy HTML/CSS semantics - this is still in a very,very early stage :p)

Upvotes: 4

Views: 1362

Answers (2)

Tejs
Tejs

Reputation: 41236

Is AutoWireupEvents set to true?

Upvotes: 3

Dan Davies Brackett
Dan Davies Brackett

Reputation: 10071

If your GridView is being databound during Page_Prerender, the asp:button in question doesn't exist when postback events are processed.

Try binding the gridview during Page_Load, or using the GridView's OnItemCommand event rather than the button's OnClick.

Upvotes: 3

Related Questions