Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
given that I have this object in my grid view:

asp:TemplateField HeaderText="Display Text" SortExpression="DisplayText">
        <ItemTemplate><%# Eval("DisplayText") %></ItemTemplate>
        <EditItemTemplate>
           <asp:TextBox ID="txtDisplayText" Text='<%# Eval("DisplayText") %>' runat="server"></asp:TextBox>
        </EditItemTemplate>
        <FooterTemplate>
             <asp:TextBox ID="txtDisplayText" Text="xxx" runat="server"></asp:TextBox>
        </FooterTemplate>
</asp:TemplateField>


When I type a value into the txtDisplayText I do not see what I typed when the event OnRowCommand is executed. Rather I see the text defined in the aspx file for txtDisplayText("xxx").
What have I missed?

What I have tried:

fiddling with viewstate; searching this site; Google
Posted
Updated 12-Dec-18 8:00am
Comments
Richard Deeming 12-Dec-18 13:49pm    
What code are you using to try to read the value?
string text = ((TextBox)yourGridView.FooterRow.FindControl("txtDisplayText")).Text;
RmcbainTheThird 12-Dec-18 13:54pm    
yes, that is how I am grabbing the value
Richard Deeming 12-Dec-18 13:50pm    
Are you binding the grid on every page load, without checking the IsPostBack property first?
RmcbainTheThird 12-Dec-18 13:54pm    
yes I am. is that an oopsie :)
Richard Deeming 12-Dec-18 13:57pm    
Yes. Every time you bind the grid, it throws away anything that the user typed in, and recreates the controls. :)

1 solution

As discussed in the comments, the problem is caused by binding the grid on every page load without checking the IsPostBack property first.

Every time you bind the grid, it throws away anything that the user has typed, and recreates the entire grid control tree based on the markup and the data.

Instead of:
protected void Page_Load(object sender, EventArgs e)
{
    BindGrid();
}

Use:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid();
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900