ImageButton in GridView header under UpdatePanel n

2019-07-29 07:21发布

问题:

I have a form with update panel and containt a gridview. When users click on the ImageButton, there will be a div that showing 'Processing' and the footer will be showed with empty textboxes for data entry and saving purposes. I found out that the Save button works fine, but not the ImageButton in the header. When I clicked on the ImageButton, it won't fire unless I removed the UpdatePanel. Any suggestion?

<asp:UpdatePanel runat="server" ID="UpdatePanel1" >
        <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:TemplateField ShowHeader="False">
                        <HeaderTemplate><center>
                         <asp:ImageButton ID="ImgAdd" AlternateText="Add" runat="server"
                           ImageUrl="~/img/tDocumentAdd.png" Width="15" Height="15"
                           OnClick="ImgAdd_Click" CausesValidation="false" /></center>
                        </HeaderTemplate>
                   </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:Button runat="Server" id="bthSave" OnClick="SaveData" />
         </ContentTemplate>

回答1:

This could be because the image button resides inside the header part of your grid view. Try putting a prerender event and registering the imagebutton as postback control

.aspx

<HeaderTemplate><center>
   <asp:ImageButton ID="ImgAdd" AlternateText="Add" runat="server"
    ImageUrl="~/img/tDocumentAdd.png" Width="15" Height="15"
    OnClick="ImgAdd_Click" onprerender="ImgAdd_PreRender" /></center>

Code behind

protected void ImgAdd_PreRender(object sender, EventArgs e)
{
   ImageButton btn=sender as ImageButton;
   ScriptManager sc = ScriptManager.GetCurrent(this.Page);
   sc.RegisterPostBackControl(btn);
}


回答2:

Try this code !!!.aspx!!!
 <HeaderTemplate><center>
     <asp:ImageButton ID="ImgAdd" AlternateText="Add" runat="server"
     ImageUrl="~/img/tDocumentAdd.png" Width="15" Height="15"
     OnClick="ImgAdd_Click" onprerender="ImgAdd_PreRender" /></center>
  code behind`enter code here !!!  (.cs)`   !!!
 protected void ImgAdd_PreRender(object sender, EventArgs e)
  {
    ImageButton btn=sender as ImageButton;
   ScriptManager sc = ScriptManager.GetCurrent(this.Page);
   sc.RegisterPostBackControl(btn);
  }`