472,334 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 17371
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

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

Similar topics

3
by: | last post by:
Hello, I have created an ASP.NET 2.0 application that utilized a Gridview Control to display and update/delete data. The problem I am having...
1
by: Andrew Nav | last post by:
I dragged a table from a datasource on to my web form and the GridView cpntrol was created. I turned on paging, and Edit and Delete buttons. I...
4
by: P. Yanzick | last post by:
Hello, I've been playing with master/detail views as well as editing in the gridview, and I ran across a strange problem that I am not exactly...
3
by: misiek | last post by:
Hi all. I have following problem: 1. In my web page I have a GridView control, which does not have a DataSourceId set in designer. 2. When user...
5
by: maurban | last post by:
Hi there experts, I have a gridview with a couple textboxes and a dropdownlist. I'm trying to insert a default value into my database driven...
1
by: Evan M. | last post by:
Here's my GridView and my SqlDataSource <asp:GridView ID="ContactHistoryGrid" runat="server" AutoGenerateColumns="False"...
6
by: McGeeky | last post by:
Hi. GridView's inbuilt capability to edit/update individual rows, one at a time, is useful but inadequate for a scenario we currently face. I want...
0
by: mike0870 | last post by:
Hi, I've been at this one for hours and cannot not find any posts of anyone having the same problem. Ther scenario is, I need to fill a drop down box...
4
by: mohaaron | last post by:
This seems like it should be simple to do but for some reason I have been unable to make it work. I would like to databind a SqlDataSource to a...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.