Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid editmode textbox ID

Status
Not open for further replies.

TomTT

Programmer
Jun 3, 2002
55
US
I have a datagrid that I populate from a database. I put the grid into edit mode and change the text in one of the textboxes. I click the update link and go to a sub
that does a response.write of the datagrid textbox.

Dim LName As textbox
Dim strTest As string
LName = e.item.cells(2).controls(0)
strTest = LName.text

Response.Write(strTest)

I expect to get the value I entered into the datagrid textbox, but what I get is the original value in that cell of the textbox.

What am I missing here?

Thanks
Tom T
 
Try this:
Code:
Dim LName As textbox
Dim strTest As string
LName = e.item.FindControl("MyTextBox")
strTest = LName.Text
where "MyTextBox" is the ID property of the text box in your datagrid
 
You might need to cast the control, I think it's something like this in VB:
Code:
LName = CType(e.item.FindControl("MyTextBox"), TextBox)
 
Thanks LV,

The problem I seem to be having is properly identifying the textbox that is created in the datagrid when I go into edit mode.

The following line of code should correctly identify the
textbox, but it doesn't seem to.

Dim LName as Textbox
LName = e.Item.Cells(2).Controls(0)

It's like the identifier for LName is actually for the
cell of the grid without the textbox.

I tried naming other controls values (Controls(1)) etc.
but just get errors.

I can't seem to nail down how to spedify the edit textbox as my target for the value of strTest.

Thanks
TT
 
Could you please post a block of HTML for your grid? In particular for that column with "edit" text boxes?
 
Here is the section of code from the datagrid:

<ASP:TemplateColumn>
<ItemTemplate>
<%# Container.DataItem(&quot;fname&quot;)%>
</ItemTemplate>

<EditItemTemplate>
<ASP:TextBox ID=&quot;Name&quot; Text='<%# Container.DataItem
(&quot;fname&quot;)%>' runat=&quot;Server&quot; />
</EditItemTemplate>
</ASP:TemplateColumn>

Here is the code for the OnEditCommand:

Dim LName As textbox
Dim strTest As string

lName= e.item.FindControl(&quot;Name&quot;)
strTest = lName.text
response.write(strTest)

It seems that the response.write should correctly show the new entry in the textbox of the grid, but it writes out the old value ???

Thanks for the help
TT
 
If it's writing out the old value, make sure that your Bind() function is enclosed in if(!Page.IsPostBack).

For example:

if(!Page.IsPostBack)
{
Bind();
}

private void Bind()
{
//Your databinding function here
}

If you don't do it this way, then when you click the update button, the new values will be overwritten with the old values due to re-binding on Page PostBack.

-----------------------------------------------
&quot;The night sky over the planet Krikkit is the least interesting sight in the entire universe.&quot;
-Hitch Hiker's Guide To The Galaxy
 
Thanks for the suggestion. I do have the dataBind() code within a If Page.IsPostBack loop.

What's throwing me is the fact that I am calling the datagrid textbox control by name and should be getting the value input prior to clicking the update link.

Regards,
TomT
 
Just a note:

If, in the update sub, I assign a value to the dataset row being edited:

objDataSet.Tables(&quot;orders&quot;).Rows(e.Item.ItemIndex)(2) = &quot;TEST WORD&quot;

The value is successfully assigned to the dataset and is reflected in the datagrid after postback. Still seems like I'm just not latching onto the correct textbox control to grab its value.

TT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top