开发者

asp:Button is not calling server-side function

开发者 https://www.devze.com 2022-12-28 14:24 出处:网络
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

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, w开发者_JAVA技巧hich 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)


Is AutoWireupEvents set to true?


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.

0

精彩评论

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