[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.
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.
Re: Retreive GridView Textbox
Quote:
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.
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
Re: Retreive GridView Textbox
First of all, your syntax is incorrect.
Quote:
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 :).
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....
Re: Retreive GridView Textbox
Quote:
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
}
}
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.
Re: [RESOLVED] Retreive GridView Textbox
Hey,
Glad to hear that you got it working!!
Gary