|
-
Mar 5th, 2010, 09:25 PM
#1
Thread Starter
Lively Member
[RESOLVED] Retreive GridView Textbox
Hi,
I am having a few problems trying to get a textbox I added to a gridview.
Adding the textbox to gridview in the form load event
Code:
Dim txt As New TextBox
txt.ID = "txt1"
txt.Width = 20
txt.Text = 1
GridView1.Rows(0).Cells(0).AddAt(0, txt)
change value in textbox and click Update
Code:
Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
lblUpdateValue.Text = ctype(GridView1.Rows(0).Cells(0).Controls(0), textbox).Text
End Sub
The problem is that there is now no textbox in that cell even tho it is visible on webpage and I can even type in it?????
Can someone please help shed some light. Thanks in advance.
-
Mar 5th, 2010, 11:13 PM
#2
Re: Retreive GridView Textbox
Is that code in page load run every load or not page.ispostback because dynamically added controls need to be recreated every postback to maintain their state. Also you set the text value which would overwrite user entered values unless it was done only first page load.
-
Mar 6th, 2010, 12:27 AM
#3
Thread Starter
Lively Member
Re: Retreive GridView Textbox
 Originally Posted by brin351
Is that code in page load run every load or not page.ispostback because dynamically added controls need to be recreated every postback to maintain their state. Also you set the text value which would overwrite user entered values unless it was done only first page load.
Oh ok. I see. Is there a way to keep the control there?
Ok I have now added.
Code:
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox id="txt1" runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
However when I attempt to change the value and click update txt1.text reads as "" instead of the value I typed in.
Edited: Adding autopostback = "True" to the textbox works but only if I deselect the textbox first then click update. Clicking update with the textbox selected doesn't give me the updated value.
Last edited by Developer2007; Mar 6th, 2010 at 12:49 AM.
-
Mar 6th, 2010, 04:10 AM
#4
Re: Retreive GridView Textbox
Hey,
I am not sure that I follow what you are saying.
What exactly are you trying to achieve here. I think the bigger picture will help to identify the correct solution.
Gary
-
Mar 6th, 2010, 09:10 AM
#5
Hyperactive Member
Re: Retreive GridView Textbox
First of all, your syntax is incorrect.
 Originally Posted by Developer2007
Code:
GridView1.Rows(0).Cells(0).AddAt(0, txt)
Should be
Code:
GridView1.Rows(0).Cells(0).Controls.AddAt(0, txt)
Thence try to cast the control, check it if it's not nothing, and then use it's value.
Code:
Dim TextBoxVar As TextBox = TryCast(GridView1.Rows(0).Cells(0).FindControl("txt1"), TextBox)
If TextBoxVar IsNot Nothing Then
lblUpdateValue.Text = TextBoxVar.Text
End If
Hope this helps .
-
Mar 6th, 2010, 09:17 AM
#6
Junior Member
Re: Retreive GridView Textbox
Code:
<asp:DataGrid CellPadding="3" ID="DataGrid1" runat="server"
OnItemCommand="Recordoperate"
second build the method
private void RecordOperate(Object sndr, DataGridCommandEventArgs e)
{
TextBox txtb = (TextBox)e.item.FindControl("Textbox1")
string str = txtb.Text
//ToDo : use the variable in anything you want
}
Hope this helps....
Last edited by TimBebe; Mar 6th, 2010 at 09:20 AM.
-
Mar 6th, 2010, 09:48 AM
#7
Hyperactive Member
Re: Retreive GridView Textbox
 Originally Posted by TimBebe
Code:
<asp:DataGrid CellPadding="3" ID="DataGrid1" runat="server"
OnItemCommand="Recordoperate"
second build the method
private void RecordOperate(Object sndr, DataGridCommandEventArgs e)
{
TextBox txtb = (TextBox)e.item.FindControl("Textbox1")
string str = txtb.Text
//ToDo : use the variable in anything you want
}
Hope this helps....
Hey, you should put semicolon on line endings, or else you will get syntax errors. Meanwhile, you should check if the control was properly cast before using it.
Code:
private void RecordOperate(object sndr, DataGridCommandEventArgs e)
{
TextBox txtb = (TextBox)e.item.FindControl("Textbox1");
if (txtb != null) {
string str = txtb.Text;
//ToDo : use the variable in anything you want
}
}
-
Mar 6th, 2010, 06:09 PM
#8
Thread Starter
Lively Member
Re: Retreive GridView Textbox
Hey guys. I bit off an update. The problem was me changing the ID of the gridviewrow.
Finding this made my weekend a whole lot better. Thanks for all the help everyone.
-
Mar 7th, 2010, 04:57 AM
#9
Re: [RESOLVED] Retreive GridView Textbox
Hey,
Glad to hear that you got it working!!
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|