cyclonebri Posted April 1, 2004 Posted April 1, 2004 Hey everyone. I was wondering if there is a quick and painless way of getting the data entered by the user in the textboxes that are generated in a datagrid when the edit button is clicked? In other words, I have used the editor to create an edit/update/cancel button column, and have hardmapped all the other columns in the designer, without editing the HTML. I therefore have no idea what the textboxes in the columns are named or what their ID is, so I can't do a find control on them. I can get the original values using this line: m_myOldVals(0) = m_myDS.Tables(0).Rows(e.Item.ItemIndex).Item("AccountStatus") now all I need to do is get the values as added by the user in the generated textboxes, and I can perform the actual update. Any help would be greatly appreciated. In addition, I have also tried the following but it did not work: m_myNewVals(0) = e.Item.Cells(0).Text Thanks in advance for any help! Brian Quote
Moderators Robby Posted April 1, 2004 Moderators Posted April 1, 2004 Try this... dim txt as textbox = CType(e.Item.Cells(1).Controls(0), TextBox) myNewSomething = CType(txt.Text, Integer) or in a EditItemTemplate of a TemplateColumn dim dd as dropdownlist = CType(e.Item.FindControl("myDropDown"), DropDownList) myNEwSomwthing = CType(dd.SelectedItem.Value, Integer) Quote Visit...Bassic Software
cyclonebri Posted April 1, 2004 Author Posted April 1, 2004 Try this... dim txt as textbox = CType(e.Item.Cells(1).Controls(0), TextBox) myNewSomething = CType(txt.Text, Integer) or in a EditItemTemplate of a TemplateColumn dim dd as dropdownlist = CType(e.Item.FindControl("myDropDown"), DropDownList) myNEwSomwthing = CType(dd.SelectedItem.Value, Integer) Robby, Thank you very much! You rock! I implemented your code, but I was getting the old values so I had to use a session variable to stop the grid from reloading on postback, but once I got that working I was able to easily retrieve the values using your example. Thank you! Quote
Moderators Robby Posted April 1, 2004 Moderators Posted April 1, 2004 Instead of using Session, do this.... in your Page_Load event.... If not IsPostBack Then LoadMyDataGridRoutine() End If Quote Visit...Bassic Software
cyclonebri Posted April 2, 2004 Author Posted April 2, 2004 Instead of using Session, do this.... in your Page_Load event.... If not IsPostBack Then LoadMyDataGridRoutine() End If Yeah, I will look into that tomorrow when I get back to work, the thing is that I think I need it to rebind on postback for other events, like paging and sorting, which I have implemented, but I'll check it out just in case... Thanks again for your help! Brian Quote
eramgarden Posted April 2, 2004 Posted April 2, 2004 This is what I dont understand...i'm new to .net so bear with me... 1. In my page load, I call databind() routine and populate the DataGrid. 2. In my Update rotuine..when the changes are entered in the text boxes, I read the CURRENT values in the Datagrid and do my update. questions 3. Where/when do I get the previous values of the DataGrid?? I dont see "tables()" as a member of datagrid This is what I did to hold the value of the Datagrid before being update..I do NOT think this is a good idea..I do a select, populate datagrid, then open the connection again and read the values again in session vars.. cmd.CommandText = "Select callerid,....., from..." cnn.Open() Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) dgViewContactDetail.DataSource = dr dgViewContactDetail.DataBind() If Not IsPostBack Then [b]'this holds the orig value of datagrid[/b] cnn.Open() Dim dr1 As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) If dr1.Read() Then Session("test") = dr1.GetString(5) End If End If dr.Close() cnn.Close() Quote
cyclonebri Posted April 2, 2004 Author Posted April 2, 2004 This is what I dont understand...i'm new to .net so bear with me... 1. In my page load, I call databind() routine and populate the DataGrid. 2. In my Update rotuine..when the changes are entered in the text boxes, I read the CURRENT values in the Datagrid and do my update. questions 3. Where/when do I get the previous values of the DataGrid?? I dont see "tables()" as a member of datagrid This is what I did to hold the value of the Datagrid before being update..I do NOT think this is a good idea..I do a select, populate datagrid, then open the connection again and read the values again in session vars.. cmd.CommandText = "Select callerid,....., from..." cnn.Open() Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) dgViewContactDetail.DataSource = dr dgViewContactDetail.DataBind() If Not IsPostBack Then [b]'this holds the orig value of datagrid[/b] cnn.Open() Dim dr1 As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) If dr1.Read() Then Session("test") = dr1.GetString(5) End If End If dr.Close() cnn.Close() The tables() method is a member of the dataset object. If you have used a dataset to generate the values for the grid, you could get the values from the dataset.tables(0)...It seems to me, however, that you are using a datareader to generate the data for your datagrid. This means that you are taking a disconnected snapshot of the database, which is ok but it makes it more difficult to work with. One thing I noticed also, is that you said If dr1.Read() Then Session("test") = dr1.GetString(5) End If From what I've seen, it's usually pretty conventional to do this instead while dr1.read() 'use if logic if you are reading in more than one row of records Session("test") = dr1.getString(5) wend but, to make it possible for you to use the dataset logic, you could use your current command object and do something like this: dim myDS as DataSet = new DataSet("WhateverYouWantHere") dim myDA as DataAdapter = new DataAdapter dim sTest as string = "" 'do all the same code to set up your connection and your command, and then: try cnn.Open() myDA.SelectCommand = cmd myDA.fill(myDS) sTest = myDS.Tables(0).Rows(rownumber).Item("FieldName") catch ex as exception 'some error reporting System.Diagnostics.Trace.WriteLine(ex.ToString()) finally if not cnn.State = ConnectionState.Closed then cnn.close() endif end try Good luck! Quote
eramgarden Posted April 2, 2004 Posted April 2, 2004 Wow, yeah, got it working... But i have to put this code in... If Not IsPostBack Then ... end if otherwise, the orig value of datagrid will be replaced by the new value..right? This is great! Quote
cyclonebri Posted April 3, 2004 Author Posted April 3, 2004 Wow, yeah, got it working... But i have to put this code in... If Not IsPostBack Then ... end if otherwise, the orig value of datagrid will be replaced by the new value..right? This is great! If you don't have a condition for the postback and you do a databind on each page load, you will lose the new data in the textboxes before you can capture it, at least from my experience. Your new data will not show up in the fields of the grid until it is updated to the server and the dataset is refilled from your sql query. Other than that, sounds like you have it. Have a great weekend! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.