Barnabeck
Barnabeck

Reputation: 481

textbox: is data bound only when visible?

I have a gridview with an invisible TextBox control that is data-bound on a click event. Another click event sets the visibilty to true, but the TextBox is then empty. I could rebind the gridview but don't understand why the TextBox loses it's value. Is this standard behavior?

<asp:templatefield HeaderText="NewRate" Visible="false">
    <ItemTemplate>
        <asp:TextBox ID="TXBX_NewRate" runat="server" Text = '<%# Bind("NewRate") %>' width="20px" />
        <asp:Label ID="LabelRequirement" runat="server" Text = '<%# Bind("Requirement") %>'/>
    </ItemTemplate>
</asp:templatefield>

and the event methode:

    protected void CheckedChanged_EditarPlazo(object sender, EventArgs e)
    {
        if (CKBX_NuevoPlazo.Checked == true)
        {
            GridView_ContractFileContent.Columns[11].Visible = true;

Upvotes: 0

Views: 94

Answers (1)

Skaria Thomas
Skaria Thomas

Reputation: 419

Use css class to invisible a column in GridView instead of GridView_ContractFileContent.Columns[11].Visible = false; as this loses value in postback.

In aspx

<head runat="server">
    <title></title>
    <style type="text/css">
        .hideGridColumn {
            display: none;
        }

        .showGridColumn {
            display: block;
        }
    </style>
</head>

In code behind file

  if (CKBX_NuevoPlazo.Checked == true)
            {
                GridView_ContractFileContent.Columns[1].HeaderStyle.CssClass = "showGridColumn";
                GridView_ContractFileContent.Columns[1].ItemStyle.CssClass = "showGridColumn";
            }
            else
            {
                GridView_ContractFileContent.Columns[1].HeaderStyle.CssClass = "hideGridColumn";
                GridView_ContractFileContent.Columns[1].ItemStyle.CssClass = "hideGridColumn";
            }

Upvotes: 1

Related Questions