Mitesh Jain
Mitesh Jain

Reputation: 565

In gridview, updatepanel is not working

html code

<asp:GridView ID="gridprodlist" runat="server" AutoGenerateColumns="False" EmptyDataText="No record" Width="100%">
<Columns>
<asp:TemplateField HeaderText="Edit&lt;br/&gt;Delete&lt;br/&gt;Disable">
<ItemTemplate>
<table cellpadding="0" cellspacing="0" border="0">
     <tr>
        <td valign="top">
         <asp:UpdatePanel ID="UpdatePanel100" runat="server">
            <ContentTemplate>
        <asp:CheckBox ID="chkdis" runat="server" AutoPostBack="True" OnCheckedChanged="chkdis_CheckedChanged" ValidationGroup='<%# Eval("pid") %>' Text="Disable" />
            </ContentTemplate>
          </asp:UpdatePanel>
         </td>
    </tr>
</table>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />

.cs code

protected void chkdis_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chkdis = (CheckBox)sender;
    int id = Convert.ToInt32(((CheckBox)sender).ValidationGroup);

    if (chkdis.Checked == true)
    {

    }
    else
    {

    }

}

I had put updatepanel inside the gridview but still On click of my checkbox my page get refresh. what should i do? I dont want my page to get refresh

Upvotes: 1

Views: 1962

Answers (4)

user240141
user240141

Reputation:

Move UpdatePanel to out of the GridView, and wrap the GridView in the UpdatePanel. If you still need postback on some controls then use, just before the closing tag of UpdatePanel.

<Triggers> 
<PostBackTriggers ControlId="YourControlId"/>
<Triggers>
</asp:UpdatePanel>

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460098

Try to register this CheckBox as async-postback control from codebehind:

protected void Page_Init(Object sender, EventArgs e)
{
     gridprodlist.DataBound += GridprodList_DataBound;
}  

private void GridprodList_DataBound(Object sender, EventArgs e)
{
    ScriptManager sm = ScriptManager.GetCurrent(Page);
    foreach(GridViewRow row in gridprodlist.Rows)
    {
        CheckBox chkdis = (CheckBox) row.FindControl("chkdis");
        sm.RegisterAsyncPostBackControl(chkdis);
    }
}

its working for first time only but on second time again my page gets refresh

Then try to use RowCreated which is triggered on every postback for every row:

ScriptManager sm = ScriptManager.GetCurrent(Page);

private void GridprodList_RowCreated(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chkdis = (CheckBox) e.Row.FindControl("chkdis");
        sm.RegisterAsyncPostBackControl(chkdis);
    }
}

Upvotes: 2

Mrinal Saurabh
Mrinal Saurabh

Reputation: 978

It doesn't work like this because GridView is converted to table while being rendered. The update panel is not certainly being added to each row on being rendered. So, the best way is to put the gridview inside the updatepanel.

Upvotes: 0

Brian Mains
Brian Mains

Reputation: 50728

Try moving the UpdatePanel out of the GridView, and wrap the entire GridView in the UpdatePanel, and see if that makes a difference.

Upvotes: 2

Related Questions