locked
set the properties of a Textbox in an EditItemTemplate RRS feed

  • Question

  • User154499744 posted

    I have Gridview. In the Gridview I have a TemplateField that has an EditItemTemplate with a Textbox in it. The Gridview also has a CommandField with ShowEditButton set to true.

    As you might expect from this brief description, when the user hits the Edit button on a row, the row goes into an edit mode and the Textbox shows up and the user is able to edit the value.

    Here is my question. How can I set the properties of the Textbox? For example, depending on certain conditions, I want to disable the Textbox so that the user can not edit the existing value. I think the first thing I need to know is what is the Event on the Gridview that I need to write the code in?

    Maybe someone will be better than me on finding a specific example.

    Thursday, February 23, 2017 5:03 PM

All replies

  • User2103319870 posted

    How can I set the properties of the Textbox? For example, depending on certain conditions, I want to disable the Textbox so that the user can not edit the existing value. I think the first thing I need to know is what is the Event on the Gridview that I need to write the code in?

    You can use the Gridview rowediting event and then find the row which is in editmode. From the row you can find the textbox control and disable it

    /// <summary>
            /// Handles the RowEditing event of the GridView1 control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewEditEventArgs"/> instance containing the event data.</param>
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                //Set the edit index in gridview
                GridView1.EditIndex = e.NewEditIndex;
                //calling the method to bind grid with data again
                GetCustomerDetails();
                //Provide your condition here
                if("your condition is true")
                {
                    //Here you can find the textbox control which you want to disable
                    //change textbox id here
                    TextBox txtbox = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("TextBox1");
                    txtbox.Enabled=false;
                }
    
            }

    Source URL

    Thursday, February 23, 2017 6:08 PM
  • User-1838255255 posted

    Hi NewKid1nTown,

    According to your description, I make a sample, I think you could add condition to judge, then disable the control. Here is the complete sample code:

    I judge the textbox1 value, if textbox1 value equal to "234", then disable textbox2, this will disable edit of textbox2.

    Sample Code: 

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnRowEditing="GridView1_RowEditing" AutoGenerateColumns="False">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Label1") %>'></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("Label2") %>'></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowEditButton="True" />
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
                table.Columns.Add("Label1", typeof(string));
                table.Columns.Add("Label2", typeof(string));
                table.Rows.Add("234", "3232");
                GridView1.DataSource = table;
                GridView1.DataBind();
            }
    
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView1.EditIndex = e.NewEditIndex;
                TextBox txtbox = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("TextBox1");
                TextBox newtxtbox = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("TextBox2");
                string value = txtbox.Text;
                if (value == "234")
                {
                    newtxtbox.Enabled = false;
                }
            }

    Result:

    Best Regards,

    Eric Du

    Friday, February 24, 2017 8:42 AM