473,325 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. <asp:UpdatePanel  ID="UpdatePanel1" runat="server"><ContentTemplate> 
  2.  
  3.  
  4.  <table style="font-family:Arial;">
  5.  
  6.         <asp:GridView ID="GridView1" width="800px" runat="server" AutoGenerateColumns="false"
  7.               AllowSorting = "true" AllowPaging = "true" AutoGenerateDeleteButton = "false"
  8.               AutoGenerateEditButton = "True" AutoGenerateSelectButton = "false" PageSize = "4"
  9.               OnRowEditing="GridView1_RowEditing"
  10.               OnRowCancelingEdit="GridView1_RowCancelingEdit" 
  11.               OnRowUpdating="GridView1_RowUpdating"
  12.               OnPageIndexChanging="GridView1_PageIndexChanging"
  13.               PagerSettings-Mode = "NextPreviousFirstLast" 
  14.               BorderWidth="1px" BackColor="White" GridLines="Vertical" 
  15.               CellPadding="4" BorderStyle="None"
  16.               BorderColor="#DEDFDE" ForeColor="Black">
  17.               <FooterStyle BackColor="#CCCC99"></FooterStyle>
  18.               <PagerStyle ForeColor="Black" HorizontalAlign="Right" 
  19.               BackColor="#F7F7DE"></PagerStyle>
  20.               <HeaderStyle ForeColor="White" Font-Bold="True" 
  21.               BackColor="#6B696B"></HeaderStyle>
  22.               <AlternatingRowStyle BackColor="LightGray"></AlternatingRowStyle>
  23.  
  24.               <Columns>
  25.  
  26.                 <%--  
  27.                 <asp:TemplateField>
  28.                     <ItemTemplate>
  29.                         <asp:CheckBox runat="server" ID="RowLevelCheckbox" />
  30.                     </ItemTemplate>
  31.                 </asp:TemplateField> --%>
  32.  
  33.  
  34.                 <asp:BoundField DataField="distrib_zone_id" headertext = "Distrib Zone Id"  ReadOnly = "false" sortexpression = "distrib_zone_id" ItemStyle-Width = "200" />
  35.  
  36.                 <asp:BoundField DataField="distrib_zone_desc" headertext = "Distrib Zone Desc" ReadOnly = "false" sortexpression = "distrib_zone_desc" ItemStyle-Width = "600" />
  37.  
  38.                 <asp:BoundField DataField="district_id" headertext = "District Id" ReadOnly = "false" sortexpression = "district_id" ItemStyle-Width = "375" />
  39.  
  40.                 <asp:BoundField DataField="carrier_id" headertext = "Carrier Id" ReadOnly = "false" sortexpression = "carrier_id" ItemStyle-Width = "375" />
  41.  
  42.                 <asp:BoundField DataField="carrier_full_name" headertext = "Carrier Name" ReadOnly = "false" sortexpression = "carrier_full_name" ItemStyle-Width = "700" />
  43.  
  44.                 <asp:BoundField DataField="partner_id" headertext = "Partner Id"  ReadOnly = "false" sortexpression = "partner_id" ItemStyle-Width = "400" />                              
  45.  
  46.  
  47.               </Columns>
  48.  
  49.         </asp:GridView>
  50.  
  51.         <tr>
  52.             <td>
  53.                 &nbsp
  54.                 <br />
  55.                 <asp:Label ID="lblWhichButton" runat="server" Text="WhichButton" Width="300px" Font-Bold="true"></asp:Label>
  56.             </td>
  57.          </tr>
  58.          <tr>
  59.             <td >
  60.  
  61.             </td>
  62.  
  63.             <td>
  64.                 <asp:Label ID="lblPartners" runat="server" Text="Partners  " Width="100px" Font-Bold="true"></asp:Label>
  65.                 &nbsp&nbsp
  66.                  <asp:DropDownList id="PartnerList"
  67.                     AutoPostBack="False"
  68.                     runat="server"/>
  69.                   &nbsp&nbsp&nbsp&nbsp
  70.                   <asp:Button ID="btn_AssignPartner" runat="server" Text="Assign Partner" />
  71.                   <asp:Button ID="btn_RemovePartner" runat="server" Text="Remove Partner" />
  72.                   &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
  73.                   &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
  74.                   &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
  75.                   &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
  76.                   <asp:Button ID="btn_Exit" runat="server" Text="Exit" />
  77.  
  78.             </td>
  79.          </tr>
  80.     </table>
  81.  
  82.  </ContentTemplate>
  83.  
  84. </asp:UpdatePanel>
Nov 16 '11 #1
1 6153
Frinavale
9,735 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.     <title></title>
  7. </head>
  8. <body>
  9.     <form id="form1" runat="server">
  10.     <div>
  11.         <asp:ScriptManager ID="smanager" runat="server" />
  12.         <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  13.             <ContentTemplate>
  14.                 <table style="font-family: Arial;">
  15.                     <asp:GridView ID="GridView1" Width="800px" runat="server" AutoGenerateColumns="false"
  16.                         AllowSorting="true" AllowPaging="true" AutoGenerateDeleteButton="false" AutoGenerateEditButton="True"
  17.                         AutoGenerateSelectButton="false" PageSize="4" OnRowEditing="GridView1_RowEditing"
  18.                         OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating"
  19.                         OnPageIndexChanging="GridView1_PageIndexChanging" PagerSettings-Mode="NextPreviousFirstLast"
  20.                         BorderWidth="1px" BackColor="White" GridLines="Vertical" CellPadding="4" BorderStyle="None"
  21.                         BorderColor="#DEDFDE" ForeColor="Black">
  22.                         <FooterStyle BackColor="#CCCC99"></FooterStyle>
  23.                         <PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#F7F7DE"></PagerStyle>
  24.                         <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#6B696B"></HeaderStyle>
  25.                         <AlternatingRowStyle BackColor="LightGray"></AlternatingRowStyle>
  26.                         <Columns>
  27.                             <asp:BoundField DataField="distrib_zone_id" HeaderText="Distrib Zone Id" ReadOnly="false"
  28.                                 SortExpression="distrib_zone_id" ItemStyle-Width="200" />
  29.                             <asp:BoundField DataField="distrib_zone_desc" HeaderText="Distrib Zone Desc" ReadOnly="false"
  30.                                 SortExpression="distrib_zone_desc" ItemStyle-Width="600" />
  31.                             <asp:BoundField DataField="district_id" HeaderText="District Id" ReadOnly="false"
  32.                                 SortExpression="district_id" ItemStyle-Width="375" />
  33.                             <asp:BoundField DataField="carrier_id" HeaderText="Carrier Id" ReadOnly="false" SortExpression="carrier_id"
  34.                                 ItemStyle-Width="375" />
  35.                             <asp:BoundField DataField="carrier_full_name" HeaderText="Carrier Name" ReadOnly="false"
  36.                                 SortExpression="carrier_full_name" ItemStyle-Width="700" />
  37.                             <asp:BoundField DataField="partner_id" HeaderText="Partner Id" ReadOnly="false" SortExpression="partner_id"
  38.                                 ItemStyle-Width="400" />
  39.                         </Columns>
  40.                     </asp:GridView>
  41.                     <tr>
  42.                         <td>
  43.                             &nbsp
  44.                             <br />
  45.                             <asp:Label ID="lblWhichButton" runat="server" Text="WhichButton" Width="300px" Font-Bold="true"></asp:Label>
  46.                         </td>
  47.                     </tr>
  48.                     <tr>
  49.                         <td>
  50.                         </td>
  51.                         <td>
  52.                             <asp:Label ID="lblPartners" runat="server" Text="Partners  " Width="100px" Font-Bold="true"></asp:Label>
  53.                             <asp:DropDownList ID="PartnerList" AutoPostBack="False" runat="server" />
  54.                             <asp:Button ID="btn_AssignPartner" runat="server" Text="Assign Partner" />
  55.                             <asp:Button ID="btn_RemovePartner" runat="server" Text="Remove Partner" />
  56.                             <asp:Button ID="btn_Exit" runat="server" Text="Exit" />
  57.                         </td>
  58.                     </tr>
  59.                 </table>
  60.             </ContentTemplate>
  61.         </asp:UpdatePanel>
  62.     </div>
  63.     </form>
  64. </body>
  65. </html>
Here is my C# code for the page:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace WebApplication1
  9. {
  10.     public partial class WebForm1 : System.Web.UI.Page
  11.     {
  12.         private System.Data.DataSet oDataSet;
  13.         private System.Data.DataTable oDataTable;
  14.         protected void Page_Load(object sender, EventArgs e)
  15.         {
  16.             //Retrieving the datasource for the grid view
  17.             if (Session["oDataSet"] != null)
  18.             {
  19.                 oDataSet = (System.Data.DataSet)Session["oDataSet"];
  20.             }
  21.             else
  22.             {
  23.                 createDataTable();
  24.                 Session["oDataSet"] = oDataSet;
  25.             }
  26.  
  27.         }
  28.  
  29.         protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
  30.         {
  31.             //Setting the page indext to the new page index
  32.             GridView1.PageIndex = e.NewPageIndex;
  33.         }
  34.  
  35.         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  36.         {
  37.             //Retrieve the table from the session object.
  38.             System.Data.DataTable dt = oDataSet.Tables[0];
  39.  
  40.             //Update the values.
  41.             GridViewRow row = GridView1.Rows[e.RowIndex];
  42.             dt.Rows[row.DataItemIndex]["distrib_zone_id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
  43.             dt.Rows[row.DataItemIndex]["distrib_zone_desc"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
  44.             dt.Rows[row.DataItemIndex]["district_id"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
  45.             dt.Rows[row.DataItemIndex]["carrier_id"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
  46.             dt.Rows[row.DataItemIndex]["carrier_full_name"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
  47.             dt.Rows[row.DataItemIndex]["partner_id"] = ((TextBox)(row.Cells[6].Controls[0])).Text;
  48.             //Reset the edit index.
  49.             GridView1.EditIndex = -1;
  50.  
  51.         }
  52.  
  53.         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  54.         {   //Cancelling the edit
  55.             GridView1.EditIndex = -1;
  56.         }
  57.  
  58.         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  59.         {   //Setting the edit index to the row that is being edited
  60.             //This is probably not going to wrok with paging
  61.             GridView1.EditIndex = e.NewEditIndex;
  62.         }
  63.         void Page_PreRender(object sender, EventArgs e)
  64.         {   //Setting the data source for the gridview and binding to the data
  65.             GridView1.DataSource = oDataSet;
  66.             GridView1.DataBind();
  67.         }
  68.  
  69.         private void createDataTable()
  70.         {   //Since I don't have a database, I'm creating a data set and adding 
  71.             //a data table to the data set with populated default data
  72.             oDataSet = new System.Data.DataSet();
  73.             oDataTable = new System.Data.DataTable();
  74.  
  75.             oDataTable.Columns.Add("distrib_zone_id", typeof(string));
  76.             oDataTable.Columns.Add("distrib_zone_desc", typeof(string));
  77.             oDataTable.Columns.Add("district_id", typeof(string));
  78.             oDataTable.Columns.Add("carrier_id", typeof(string));
  79.             oDataTable.Columns.Add("carrier_full_name", typeof(string));
  80.             oDataTable.Columns.Add("partner_id", typeof(string));
  81.             for (int i = 0; i < 10; i++)
  82.             {
  83.                 System.Data.DataRow oDataRow = oDataTable.NewRow();
  84.                 string distrib_zone_id = String.Format("{0}{1}", "distrib_zone_id", i);
  85.                 string distrib_zone_desc = String.Format("{0}{1}", "distrib_zone_desc", i);
  86.                 string district_id = string.Format("{0}{1}", "district_id", i);
  87.                 string carrier_id = string.Format("{0}{1}", "carrier_id", i);
  88.                 string carrier_full_name = string.Format("{0}{1}", "carrier_full_name", i);
  89.                 string partner_id = string.Format("{0}{1}", "partner_id", i);
  90.                 DateTime updatetime = DateTime.Now;
  91.                 oDataRow[0] = distrib_zone_id;
  92.                 oDataRow[1] = distrib_zone_desc;
  93.                 oDataRow[2] = district_id;
  94.                 oDataRow[3] = carrier_id;
  95.                 oDataRow[4] = carrier_full_name;
  96.                 oDataRow[5] = partner_id;
  97.                 oDataTable.Rows.Add(oDataRow);
  98.             }
  99.  
  100.             oDataSet.Tables.Add(oDataTable);
  101.  
  102.         }
  103.     }
  104. }
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
Nov 17 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: brett | last post by:
I have an AJAX updatepanel in an aspx page. Inside the updatepanel is a placeholder control, which loads various ascx files. This all works fine. One of the ascx files also has an updatepanel in...
0
by: Reinhard | last post by:
Hi, Need some directions on Ajax, UpdatePanel and Gridview to achive the following: I like to show a Gridview with Data ONLY if the user is interested in Details, but without a full page...
0
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi, I have a Wizard that have about 5 steps and in every step (page) I do have some tables, checkboxes, gridview...etc and I'm trying to Cut and Paste this wizard into Ajax UpdatePanel for Ajax...
1
by: SachinSachin | last post by:
Hi All, I am implementing a custom gridview control, that emits some javascript to blink a row whenever a new row is added in gridview. The gridview is inside the <asp:updatepanel> for...
23
by: rsdev | last post by:
Hi, I have read hundreds of post regarding this issue, but none have an answer for my situation. So hopefully somebody here can help! I have a gridview in an updatepanel (AJAX control toolkit)...
0
by: Don | last post by:
I have a GridView inside an UpdatePanel. This GridView has a command field (edit, update, cancel). When I click edit I go into update mode, refreshing only the GridView, as you would expect. ...
2
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i'm in a gridview edit row that has a DropDownList in one column and a status Label in the 2nd column. When i changed the value in the dropdownlist, there's a client-side javascirpt...
4
by: foolmelon | last post by:
Before AJAX, we were able to focus a cell in a gridview during a fullpage postback. After putting the gridview inside an UpdatePanel, we cannot focus a cell in this gridview anymore. Does anybody...
19
by: lisles | last post by:
Hi.i want to use a ajax updatepanel.on postnack i only want the datalist "dlEvnt" to get refreshed.but thats not happening.can anybody tell me what to do. Here's my code edit: massive code block...
0
by: Kishore Jangid | last post by:
I have placed a Ajax UpdatePanel to a webpage. When i click a button the data from the server are called and are fed into a HtmlGenericControl and then added to the Content of the UpdatePanel. Now...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.