Results 1 to 8 of 8

Thread: Update is not picking up current value of text box

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Update is not picking up current value of text box

    Here is an extract of some code on a webform. The code is within a GridView called gvContactSummary.

    Code:
    <asp:TemplateField HeaderText="Details">
        <ItemTemplate>
            <asp:HyperLink ID="hlDetails" runat="server"></asp:HyperLink>
            <asp:Label runat="server" ID="lblDescription" Text="" ToolTip='<%# Eval("Description")%>' />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtValue" Text='<%# Eval("Details")%>' CssClass="NormalTextbox"
                Width="320px" />
        </EditItemTemplate>
    </asp:TemplateField>
    when you click the edit link that appears a bit further across the grid the 'EditItemTemplate' is displayed and the Textbox with ID="txtValue" appears - with the correct data to edit displayed in it.

    If I change the data in the text box and click Update this happens ... (just an extract)

    Code:
                DataAccess da = new DataAccess();
                GridViewRow row = gvContactSummary.Rows[e.RowIndex];
                Label AIID = (Label)row.FindControl("AIID");
                Label LocationID = (Label)row.FindControl("LocationID");
                Label Type = (Label)row.FindControl("lblType");
                TextBox txtVal = (TextBox)row.FindControl("txtValue");
    
                if (AIID.Text != "0")
                {
                    //Update Addn info ID
                    string Description = "";
                    da.UpdateContactAddnContactInfo(Convert.ToInt32(AIID.Text), txtVal.Text, Description);
                }
    Now I know the update is happening correctly because if I hard code the value of the text box 'txtValue' this hard-coded value is then written to the database when Update is clicked.

    But, when it is not hardcoded - ie. when the textbox contains a value from the database (in this case a phone number) then ...

    If I click Edit- the textbox appears and I remove the existing phone number, enter a new one and click Update. But the value of txtVal.Text is showing the number before I edited it.

    Can anyone explain why, when the Update link is clicked, txtVal.Text does not contain the current value of the text box txtValue - it contains the original value.

    Thanks for any help. Spent all afternoon on this and got nowhere!
    Last edited by Webskater; Sep 4th, 2007 at 11:36 AM.

  2. #2
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Update is not picking up current value of text box

    Try another property, i think you need to use .Value.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Update is not picking up current value of text box

    Thanks for your answer but it's definitely textBox.Text to get at the contents of a textbox.

    Cheers

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Update is not picking up current value of text box

    The first question I ask myself in such a situation is, am I re-populating/re-binding the grid anywhere else in the code? Such as page_load, most commonly...

    If so, is there a Page.IsPostBack check?

    Then I become aware of my coworkers staring at me, since it is not considered "normal" to talk to oneself in an office environment.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Update is not picking up current value of text box

    Quote Originally Posted by mendhak
    The first question I ask myself in such a situation is, am I re-populating/re-binding the grid anywhere else in the code? Such as page_load, most commonly...

    If so, is there a Page.IsPostBack check?

    Then I become aware of my coworkers staring at me, since it is not considered "normal" to talk to oneself in an office environment.
    Thanks for your reply.

    This is the code for the gridview tag ...

    Code:
    <asp:GridView ID="gvContactSummary" runat="server" AutoGenerateColumns="False" Width="100%"
    CssClass="GridHeadDisplay" HeaderStyle-Height="0" 
    OnRowDataBound="gvContactSummary_RowDataBound"
    OnRowEditing="gvContactSummary_RowEditing" 
    OnRowUpdating="gvContactSummary_RowUpdating"
    OnRowDeleting="gvContactSummary_RowDeleting" 
    OnRowCancelingEdit="gvContactSummary_RowCancelingEdit">
    In the grid, when you click the Edit link I assume the page goes back to the server and redisplays with the EditItemTemplate(s) showing instead of the ItemTemplate(s). When this happens an 'Update' link appears and the fields you have set to be editable appear in textboxes. When you click the Update link I assume the OnRowUpdating event calls 'gvContactSummary_RowUpdating'.
    This, presumably, should be processed at the server before Page_Load - but I get the impression it is happening after Page_Load - because the original value in the textbox is being used, not the new value just typed in the textbox.

    If I set break-points in gvContactSummary_RowDataBound and in gvContactSummary_RowUpdating ... gvContactSummary_RowDataBound is being called first ... so it looks like the grid is being re-populated before the update happens. Any ideas why? How I can force the update to be done before the page re-populates.

    I probably ought to mention the grid in question is on a tab of a TabContainer which is an Ajax control.

    Cheers again.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Update is not picking up current value of text box

    How are you populating the gridview? Looking at your gridview tag, I don't see a datasource attribute, so I assume you are doing it in code. Where in code are you populating the gridview? Show us that code.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Update is not picking up current value of text box

    Quote Originally Posted by mendhak
    How are you populating the gridview? Looking at your gridview tag, I don't see a datasource attribute, so I assume you are doing it in code. Where in code are you populating the gridview? Show us that code.
    Thanks again for your help - managed to fix it by mucking about with which procedures were called on postback.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Update is not picking up current value of text box

    Yes!!!

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