rock_y
rock_y

Reputation: 17

How to stop whole page refresh on click of gridview row

On click of grid view row i don't want the whole page to reloading. Below is the Code that getting called.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textdecoration='underline';this.style.background='#DDDDDD';";
            if ((e.Row.RowIndex % 2) == 0)  // if even row
                 e.Row.Attributes["onmouseout"] = "this.style.textdecoration='none';this.style.cursor='pointer';this.style.background='#EFF3FB';";
            else  
                 e.Row.Attributes["onmouseout"] = "this.style.textdecoration='none';this.style.cursor='pointer';this.style.background='White';";
              e.Row.Attributes.Add("onclick", "Javascript:__doPostBack('myClick','" + e.Row.RowIndex + "');");


            }
        }
}

Upvotes: 0

Views: 5690

Answers (1)

Eric Falsken
Eric Falsken

Reputation: 4932

The ASP.NET Ajax controls let you do partial page updates. You can put the GridView into an UpdatePanel.

<asp:UpdatePanel ID="myUpdatePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
    <ContentTemplate>
 <asp:gridview id="CustomersGridView" 
   datasourceid="CustomersSource" 
   autogeneratecolumns="False"
   autogenerateselectbutton="True"
   allowpaging="True" 
   selectedindex="1"
   onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
   onselectedindexchanging="CustomersGridView_SelectedIndexChanging"   
   runat="server" DataKeyNames="CustomerID">

     <Columns>
         <asp:BoundField DataField="CustomerID" 
             HeaderText="CustomerID" 
             InsertVisible="False" ReadOnly="True" 
             SortExpression="CustomerID" />
         <asp:BoundField DataField="FirstName" 
             HeaderText="FirstName" 
             SortExpression="FirstName" />
         <asp:BoundField DataField="MiddleName" 
             HeaderText="MiddleName" 
             SortExpression="MiddleName" />
         <asp:BoundField DataField="LastName" 
             HeaderText="LastName" 
             SortExpression="LastName" />
         <asp:BoundField DataField="Phone" 
             HeaderText="Phone" 
             SortExpression="Phone" />
     </Columns>

   <selectedrowstyle backcolor="LightCyan"
     forecolor="DarkBlue"
     font-bold="true"/>  

 </asp:gridview>
     </ContentTemplate>
     <Triggers>
         <asp:PostBackTrigger ControlID="CustomersGridView" EventName="SelectedIndexChanged" />
     </Triggers>
</asp:UpdatePanel>

Upvotes: 1

Related Questions