Results 1 to 9 of 9

Thread: [RESOLVED] Retreive GridView Textbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    97

    Resolved [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.

  2. #2
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    97

    Re: Retreive GridView Textbox

    Quote Originally Posted by brin351 View Post
    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.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  5. #5
    Hyperactive Member nepalbinod's Avatar
    Join Date
    Sep 2007
    Posts
    293

    Re: Retreive GridView Textbox

    First of all, your syntax is incorrect.
    Quote Originally Posted by Developer2007 View Post
    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 .

  6. #6
    Junior Member
    Join Date
    Feb 2010
    Posts
    25

    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.

  7. #7
    Hyperactive Member nepalbinod's Avatar
    Join Date
    Sep 2007
    Posts
    293

    Re: Retreive GridView Textbox

    Quote Originally Posted by TimBebe View Post
    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
        }
    }

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    97

    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.

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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
  •  



Click Here to Expand Forum to Full Width