Gridview Edit in AJAX UpdatePanel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Schlecht
    New Member
    • Nov 2011
    • 1

    Gridview Edit in AJAX UpdatePanel

    Hello, I have a simple question. Are you allowed to edit gridview columns for a gridview that resides in an Updatepanel?

    I'm playing around with a simple project I created to learn more about this. If I don't have the gridview control in the updatepanel, I'm able to press the edit button on one of the rows and am able to modify the text in the column. Otherwise it won't allow me to edit the column.

    Here's my markup
    Code:
    <asp:UpdatePanel  ID="UpdatePanel1" runat="server"><ContentTemplate> 
    
      
     <table style="font-family:Arial;">
                                                      
            <asp:GridView ID="GridView1" width="800px" runat="server" AutoGenerateColumns="false"
                  AllowSorting = "true" AllowPaging = "true" AutoGenerateDeleteButton = "false"
                  AutoGenerateEditButton = "True" AutoGenerateSelectButton = "false" PageSize = "4"
                  OnRowEditing="GridView1_RowEditing"
                  OnRowCancelingEdit="GridView1_RowCancelingEdit" 
                  OnRowUpdating="GridView1_RowUpdating"
                  OnPageIndexChanging="GridView1_PageIndexChanging"
                  PagerSettings-Mode = "NextPreviousFirstLast" 
                  BorderWidth="1px" BackColor="White" GridLines="Vertical" 
                  CellPadding="4" BorderStyle="None"
                  BorderColor="#DEDFDE" ForeColor="Black">
                  <FooterStyle BackColor="#CCCC99"></FooterStyle>
                  <PagerStyle ForeColor="Black" HorizontalAlign="Right" 
                  BackColor="#F7F7DE"></PagerStyle>
                  <HeaderStyle ForeColor="White" Font-Bold="True" 
                  BackColor="#6B696B"></HeaderStyle>
                  <AlternatingRowStyle BackColor="LightGray"></AlternatingRowStyle>
                                
                  <Columns>
                   
                    <%--  
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox runat="server" ID="RowLevelCheckbox" />
                        </ItemTemplate>
                    </asp:TemplateField> --%>
    
                                                   
                    <asp:BoundField DataField="distrib_zone_id" headertext = "Distrib Zone Id"  ReadOnly = "false" sortexpression = "distrib_zone_id" ItemStyle-Width = "200" />
                                    
                    <asp:BoundField DataField="distrib_zone_desc" headertext = "Distrib Zone Desc" ReadOnly = "false" sortexpression = "distrib_zone_desc" ItemStyle-Width = "600" />
                    
                    <asp:BoundField DataField="district_id" headertext = "District Id" ReadOnly = "false" sortexpression = "district_id" ItemStyle-Width = "375" />
                    
                    <asp:BoundField DataField="carrier_id" headertext = "Carrier Id" ReadOnly = "false" sortexpression = "carrier_id" ItemStyle-Width = "375" />
                    
                    <asp:BoundField DataField="carrier_full_name" headertext = "Carrier Name" ReadOnly = "false" sortexpression = "carrier_full_name" ItemStyle-Width = "700" />
    
                    <asp:BoundField DataField="partner_id" headertext = "Partner Id"  ReadOnly = "false" sortexpression = "partner_id" ItemStyle-Width = "400" />                              
                    
                                        
                  </Columns>
                             
            </asp:GridView>
            
            <tr>
                <td>
                    &nbsp
                    <br />
                    <asp:Label ID="lblWhichButton" runat="server" Text="WhichButton" Width="300px" Font-Bold="true"></asp:Label>
                </td>
             </tr>
             <tr>
                <td >
                  
                </td>
                
                <td>
                    <asp:Label ID="lblPartners" runat="server" Text="Partners  " Width="100px" Font-Bold="true"></asp:Label>
                    &nbsp&nbsp
                     <asp:DropDownList id="PartnerList"
                        AutoPostBack="False"
                        runat="server"/>
                      &nbsp&nbsp&nbsp&nbsp
                      <asp:Button ID="btn_AssignPartner" runat="server" Text="Assign Partner" />
                      <asp:Button ID="btn_RemovePartner" runat="server" Text="Remove Partner" />
                      &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                      &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                      &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                      &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                      <asp:Button ID="btn_Exit" runat="server" Text="Exit" />
                      
                </td>
             </tr>
        </table>
    
     </ContentTemplate>
    
    </asp:UpdatePanel>
    Last edited by Frinavale; Nov 17 '11, 04:44 PM. Reason: Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Not sure what you're doing wrong...it should work.
    You're probably not binding in the right spot.

    Here is what I tried based on what you posted.
    Here's my ASP markup for a web form called WebForm1.aspx:
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="smanager" runat="server" />
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <table style="font-family: Arial;">
                        <asp:GridView ID="GridView1" Width="800px" runat="server" AutoGenerateColumns="false"
                            AllowSorting="true" AllowPaging="true" AutoGenerateDeleteButton="false" AutoGenerateEditButton="True"
                            AutoGenerateSelectButton="false" PageSize="4" OnRowEditing="GridView1_RowEditing"
                            OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating"
                            OnPageIndexChanging="GridView1_PageIndexChanging" PagerSettings-Mode="NextPreviousFirstLast"
                            BorderWidth="1px" BackColor="White" GridLines="Vertical" CellPadding="4" BorderStyle="None"
                            BorderColor="#DEDFDE" ForeColor="Black">
                            <FooterStyle BackColor="#CCCC99"></FooterStyle>
                            <PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#F7F7DE"></PagerStyle>
                            <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#6B696B"></HeaderStyle>
                            <AlternatingRowStyle BackColor="LightGray"></AlternatingRowStyle>
                            <Columns>
                                <asp:BoundField DataField="distrib_zone_id" HeaderText="Distrib Zone Id" ReadOnly="false"
                                    SortExpression="distrib_zone_id" ItemStyle-Width="200" />
                                <asp:BoundField DataField="distrib_zone_desc" HeaderText="Distrib Zone Desc" ReadOnly="false"
                                    SortExpression="distrib_zone_desc" ItemStyle-Width="600" />
                                <asp:BoundField DataField="district_id" HeaderText="District Id" ReadOnly="false"
                                    SortExpression="district_id" ItemStyle-Width="375" />
                                <asp:BoundField DataField="carrier_id" HeaderText="Carrier Id" ReadOnly="false" SortExpression="carrier_id"
                                    ItemStyle-Width="375" />
                                <asp:BoundField DataField="carrier_full_name" HeaderText="Carrier Name" ReadOnly="false"
                                    SortExpression="carrier_full_name" ItemStyle-Width="700" />
                                <asp:BoundField DataField="partner_id" HeaderText="Partner Id" ReadOnly="false" SortExpression="partner_id"
                                    ItemStyle-Width="400" />
                            </Columns>
                        </asp:GridView>
                        <tr>
                            <td>
                                &nbsp
                                <br />
                                <asp:Label ID="lblWhichButton" runat="server" Text="WhichButton" Width="300px" Font-Bold="true"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                            </td>
                            <td>
                                <asp:Label ID="lblPartners" runat="server" Text="Partners  " Width="100px" Font-Bold="true"></asp:Label>
                                <asp:DropDownList ID="PartnerList" AutoPostBack="False" runat="server" />
                                <asp:Button ID="btn_AssignPartner" runat="server" Text="Assign Partner" />
                                <asp:Button ID="btn_RemovePartner" runat="server" Text="Remove Partner" />
                                <asp:Button ID="btn_Exit" runat="server" Text="Exit" />
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        </form>
    </body>
    </html>
    Here is my C# code for the page:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication1
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            private System.Data.DataSet oDataSet;
            private System.Data.DataTable oDataTable;
            protected void Page_Load(object sender, EventArgs e)
            {
                //Retrieving the datasource for the grid view
                if (Session["oDataSet"] != null)
                {
                    oDataSet = (System.Data.DataSet)Session["oDataSet"];
                }
                else
                {
                    createDataTable();
                    Session["oDataSet"] = oDataSet;
                }
    
            }
    
            protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                //Setting the page indext to the new page index
                GridView1.PageIndex = e.NewPageIndex;
            }
    
            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                //Retrieve the table from the session object.
                System.Data.DataTable dt = oDataSet.Tables[0];
    
                //Update the values.
                GridViewRow row = GridView1.Rows[e.RowIndex];
                dt.Rows[row.DataItemIndex]["distrib_zone_id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
                dt.Rows[row.DataItemIndex]["distrib_zone_desc"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
                dt.Rows[row.DataItemIndex]["district_id"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
                dt.Rows[row.DataItemIndex]["carrier_id"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
                dt.Rows[row.DataItemIndex]["carrier_full_name"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
                dt.Rows[row.DataItemIndex]["partner_id"] = ((TextBox)(row.Cells[6].Controls[0])).Text;
                //Reset the edit index.
                GridView1.EditIndex = -1;
    
            }
    
            protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {   //Cancelling the edit
                GridView1.EditIndex = -1;
            }
    
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {   //Setting the edit index to the row that is being edited
                //This is probably not going to wrok with paging
                GridView1.EditIndex = e.NewEditIndex;
            }
            void Page_PreRender(object sender, EventArgs e)
            {   //Setting the data source for the gridview and binding to the data
                GridView1.DataSource = oDataSet;
                GridView1.DataBind();
            }
    
            private void createDataTable()
            {   //Since I don't have a database, I'm creating a data set and adding 
                //a data table to the data set with populated default data
                oDataSet = new System.Data.DataSet();
                oDataTable = new System.Data.DataTable();
    
                oDataTable.Columns.Add("distrib_zone_id", typeof(string));
                oDataTable.Columns.Add("distrib_zone_desc", typeof(string));
                oDataTable.Columns.Add("district_id", typeof(string));
                oDataTable.Columns.Add("carrier_id", typeof(string));
                oDataTable.Columns.Add("carrier_full_name", typeof(string));
                oDataTable.Columns.Add("partner_id", typeof(string));
                for (int i = 0; i < 10; i++)
                {
                    System.Data.DataRow oDataRow = oDataTable.NewRow();
                    string distrib_zone_id = String.Format("{0}{1}", "distrib_zone_id", i);
                    string distrib_zone_desc = String.Format("{0}{1}", "distrib_zone_desc", i);
                    string district_id = string.Format("{0}{1}", "district_id", i);
                    string carrier_id = string.Format("{0}{1}", "carrier_id", i);
                    string carrier_full_name = string.Format("{0}{1}", "carrier_full_name", i);
                    string partner_id = string.Format("{0}{1}", "partner_id", i);
                    DateTime updatetime = DateTime.Now;
                    oDataRow[0] = distrib_zone_id;
                    oDataRow[1] = distrib_zone_desc;
                    oDataRow[2] = district_id;
                    oDataRow[3] = carrier_id;
                    oDataRow[4] = carrier_full_name;
                    oDataRow[5] = partner_id;
                    oDataTable.Rows.Add(oDataRow);
                }
    
                oDataSet.Tables.Add(oDataTable);
    
            }
        }
    }
    Check out the code I posted and see if you're doing something differently with regards to binding or something...

    We can discuss any differences that you have if you'd like.

    Like I said, it works fine for me.

    -Frinny

    Comment

    Working...