469,353 Members | 2,053 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

C#.Net GridView Edit Problem

I am trying to use the .net 2.0 gridview control where I programmatically set the datasource to an xml file read into a dataset. I am able to use the commandfield for the edit button and it puts me in edit mode. I have a template field with an EditItemTemplate textbox. I change the text in this textbox and click the update button. The RowUpdating event fires and my handler takes over. When I try to get the changed text in that textbox, I get the original data only. What am I doing wrong???

The grid:
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="dg1" runat="server" CssClass="GridBorder" HeaderStyle-CssClass="THGRID" 
  2.                 DataKeyNames="code" AutoGenerateColumns="false" CellPadding="3" 
  3.                 Font-Size="8pt" GridLines="Both" Showfooter="false" 
  4.                 PageSize="10"  PagerSettings-PageButtonCount="15" PagerSettings-Mode="NextPreviousFirstLast"
  5.                 BorderWidth="1px" BorderStyle="Solid" BorderColor="black"
  6.                 OnRowEditing="dg1_OnRowEditing"
  7.                 OnRowCancelingEdit="dg1_RowCancelingEdit"
  8.                 OnRowUpdating="dg1_RowUpdating">
  9.               <Columns>
  10.                     <asp:CommandField ShowEditButton="True" />
  11.                     <asp:CommandField ShowDeleteButton="True" />
  12.                     <asp:BoundField DataField="code" HeaderText="Code" readonly="true" />
  13.                     <asp:TemplateField HeaderText="Name">
  14.                         <ItemTemplate>
  15.                             <asp:Label ID="lblName" runat="server"                       Text='<%# Eval("Name") %>'>                            </asp:Label>                            </ItemTemplate>
  16.                         <EditItemTemplate>                        <asp:textbox ID="txtName" runat="server" Width="250px"
  17.  * * * * *          Text='<%# Bind("Name")%>'>                    </asp:textbox>
  18.             </EditItemTemplate>        
  19.                     </asp:TemplateField>                             
  20.               </Columns>
  21. </asp:GridView>
  22.  
RowUpdating event handler
Expand|Select|Wrap|Line Numbers
  1.    protected void dg1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  2.  
  3.     {
  4.             int i = dg1.Rows[e.RowIndex].DataItemIndex;
  5.             TextBox txtName = dg1.Rows[e.RowIndex].Cells[2].FindControl("txtName") as TextBox;
  6.             string newname = txtName.Text;
  7.  
// The value of the txtName textbox is always the original value...never the new value that I put in the field. What am I doing Wrong???


Ken
May 15 '07 #1
5 17254
Frinavale
9,735 Expert Mod 8TB
I am trying to use the .net 2.0 gridview control where I programmatically set the datasource to an xml file read into a dataset. I am able to use the commandfield for the edit button and it puts me in edit mode. I have a template field with an EditItemTemplate textbox. I change the text in this textbox and click the update button. The RowUpdating event fires and my handler takes over. When I try to get the changed text in that textbox, I get the original data only. What am I doing wrong???
...
RowUpdating event handler
protected void dg1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
int i = dg1.Rows[e.RowIndex].DataItemIndex;
TextBox txtName = dg1.Rows[e.RowIndex].Cells[2].FindControl("txtName") as TextBox;
string newname = txtName.Text;

// The value of the txtName textbox is always the original value...never the new value that I put in the field. What am I doing Wrong???


Ken
Make sure that when you create the data source for your GridView that it is only done when it is not postback. Otherwise your GridView's data source will be reset to what it was originally before you can grab the new stuff.

-Frinny
May 15 '07 #2
Make sure that when you create the data source for your GridView that it is only done when it is not postback. Otherwise your GridView's data source will be reset to what it was originally before you can grab the new stuff.

-Frinny
Of course. I am embarrassed. That took care of it...however, I had to cache the dataset so that my bind's would work. Thanks!
May 15 '07 #3
Frinavale
9,735 Expert Mod 8TB
Of course. I am embarrassed. That took care of it...however, I had to cache the dataset so that my bind's would work. Thanks!
That's right :)
I forgot to mention that you should store and retrieve the Grid View's data source in session or something.

Good stuff!

-Frinny
May 15 '07 #4
Are you binding the data before you hit the handler? That will reset the value before you get a chance to see the new text value.


I am trying to use the .net 2.0 gridview control where I programmatically set the datasource to an xml file read into a dataset. I am able to use the commandfield for the edit button and it puts me in edit mode. I have a template field with an EditItemTemplate textbox. I change the text in this textbox and click the update button. The RowUpdating event fires and my handler takes over. When I try to get the changed text in that textbox, I get the original data only. What am I doing wrong???

The grid:
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="dg1" runat="server" CssClass="GridBorder" HeaderStyle-CssClass="THGRID" 
  2.                 DataKeyNames="code" AutoGenerateColumns="false" CellPadding="3" 
  3.                 Font-Size="8pt" GridLines="Both" Showfooter="false" 
  4.                 PageSize="10"  PagerSettings-PageButtonCount="15" PagerSettings-Mode="NextPreviousFirstLast"
  5.                 BorderWidth="1px" BorderStyle="Solid" BorderColor="black"
  6.                 OnRowEditing="dg1_OnRowEditing"
  7.                 OnRowCancelingEdit="dg1_RowCancelingEdit"
  8.                 OnRowUpdating="dg1_RowUpdating">
  9.               <Columns>
  10.                     <asp:CommandField ShowEditButton="True" />
  11.                     <asp:CommandField ShowDeleteButton="True" />
  12.                     <asp:BoundField DataField="code" HeaderText="Code" readonly="true" />
  13.                     <asp:TemplateField HeaderText="Name">
  14.                         <ItemTemplate>
  15.                             <asp:Label ID="lblName" runat="server"                       Text='<%# Eval("Name") %>'>                            </asp:Label>                            </ItemTemplate>
  16.                         <EditItemTemplate>                        <asp:textbox ID="txtName" runat="server" Width="250px"
  17.  * * * * *          Text='<%# Bind("Name")%>'>                    </asp:textbox>
  18.             </EditItemTemplate>        
  19.                     </asp:TemplateField>                             
  20.               </Columns>
  21. </asp:GridView>
  22.  
RowUpdating event handler
Expand|Select|Wrap|Line Numbers
  1.    protected void dg1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  2.  
  3.     {
  4.             int i = dg1.Rows[e.RowIndex].DataItemIndex;
  5.             TextBox txtName = dg1.Rows[e.RowIndex].Cells[2].FindControl("txtName") as TextBox;
  6.             string newname = txtName.Text;
  7.  
// The value of the txtName textbox is always the original value...never the new value that I put in the field. What am I doing Wrong???


Ken
Jul 13 '07 #5
Frinavale
9,735 Expert Mod 8TB
Are you binding the data before you hit the handler? That will reset the value before you get a chance to see the new text value.
They were resetting the data source of the GridView every postback...when you do this, you lose the data that the user entered and replace it with what was in the original data soruce.
Jul 13 '07 #6

Post your reply

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

Similar topics

1 post views Thread by Andrew Nav | last post: by
4 posts views Thread by P. Yanzick | last post: by
1 post views Thread by Evan M. | last post: by
6 posts views Thread by McGeeky | last post: by
11 posts views Thread by SAL | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.