473,789 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C#.Net GridView Edit Problem

7 New Member
I am trying to use the .net 2.0 gridview control where I programmaticall y 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 EditItemTemplat e 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 17517
Frinavale
9,735 Recognized Expert Moderator Expert
I am trying to use the .net 2.0 gridview control where I programmaticall y 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 EditItemTemplat e 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, GridViewUpdateE ventArgs e)
{
int i = dg1.Rows[e.RowIndex].DataItemIndex;
TextBox txtName = dg1.Rows[e.RowIndex].Cells[2].FindControl("t xtName") 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
kageyone
7 New Member
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 Recognized Expert Moderator Expert
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
jappenzeller
5 New Member
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 programmaticall y 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 EditItemTemplat e 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 Recognized Expert Moderator Expert
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
6338
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 is that the gridview control is displaying the data correctly but it is not updating or deleting the rows. What I did was, in design view, added a gridview control and added an sqldatasource control. I configured the data source to update and delete. In the gridview tasks I selected enable...
1
1659
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 ran the app and everything worked great - not a single line of code written yet. Then I wanted to add a simple feature where the user could filter out data based on a value in a single column. I added a little code that set the FilterExpression property of the SqlDataSource, and that seemed...
4
4493
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 sure where to go to try to solve. I have 2 tables, a table of cars (pretty basic, an ID, a description, and a Color ID) and a table of colors (Color ID, and a color description). I've added a gridview and a detailsview as I'm playing with both and how to get editing features to work the way I...
3
19265
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 presses Start button then I create programatically a dataSet, set it as a DataSource for my GridView an add it to Session. 3. I want my users to be able to edit data displayed in GridView, so I added Edit command column. 4. When user clicks Edit, then I get the following error: "The GridView
5
25354
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 dropdownlist. I'm doing this in the rowdatabound event. My problem is that my code only works for the very first row in the gridview. For the first row, when I press "edit", my gridview goes to edit mode, my textboxes and dropdownlist populate. Further, my dropdownlist has the "Select" I forced in...
1
10408
by: Evan M. | last post by:
Here's my GridView and my SqlDataSource <asp:GridView ID="ContactHistoryGrid" runat="server" AutoGenerateColumns="False" DataSourceID="ContactHistoryDS" DataKeyNames="JobHistoryID" OnRowCreated="ContactHistoryGrid_RowCreated" CssClass="GridViewTable" GridLines="None" CellSpacing="1" CellPadding="3" AllowSorting="True" AllowPaging="True"> <EmptyDataTemplate>
6
2633
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 the user to be able to change values in multiple rows of the GridView and then hit an "OK" button which then applies all the changes together as a single unit. I also need the GridView to support paging; such that a user makes a change in one page, navigates to another, nagivates back again and...
0
4824
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 with a value in the grid row to pass to the select statement whcih returns the rows to the drop down. Its the problem is happening in the ObjectDataSource_Selecting Event of the dropdown control when the Edit command is clicked for a grid row. I'm using the syntax , GridView.rows.FindControl...
4
9791
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 GridView during the click event of a button. This sounds easy but the next requirement is that the GridView is editable. So I have included the SelectCommand and the UpdateCommand on the SqlDataSource to allow the GridView to be editable. I have now been able to get the GridView to display data...
11
6069
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. The datatable is based on a join so I don't know if that's what's causing the behavior or not. It seems like the Update button should at least do something. When the Edit button is clicked, the grid goes into Edit mode and the Cancel button takes the grid out of Edit mode. So, I don't get what...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.