简体   繁体   中英

How to make textbox readonly in a gridview?

I have a gridview where in textbox should be editable in few cases only.. i have tried the following code but its not working how to resolve this and make textbox non editable..

protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
         ....
        if (some condition)
           {
             ..................
           } 
        else(some condition)
           {
            .............
           }
        else if(some other)
           {
             TextBox itm = (GVEditRate.Rows[e.NewEditIndex].FindControl("TextBox1") as TextBox);
              itm.Enabled = true;
             TextBox prfc = (GVEditRate.Rows[e.NewEditIndex].FindControl("TextBox2") as TextBox); 
              prfc.Enabled = false;
             TextBox anest = (GVEditRate.Rows[e.NewEditIndex].FindControl("TextBox3") as TextBox);
              anest.Enabled = false;
           }

Have you tried This:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == DataControlRowState.Alternate)
      {
       //on you condition
        TextBox txt = (TextBox)e.Row.FindControl("ControlID");
        if(txt !=null)
        {
          txt.Attributes.Add("readonly", "readonly");           
         // txt.Attributes.Remove("readonly"); To remove readonly attribute
        }
      }
    }

As it is in comment you can do the following code on RowDataBound Event

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == DataControlRowState.Alternate)
    {
       //on you condition
        TextBox txt = (TextBox)e.Row.FindControl("ControlID");
        txt.ReadOnly = true;
    }
}

I prefer use Data-Binding Expressions in the page in the column of the grid

 <asp:TextBox runat="server" Enabled='<%# YourMethodThatReturnCondition(Eval("Field1"), Eval("Field2"), ...)%>' ></asp:TextBox> 

Wehre YourMethodThatReturnCondition is a method that return a boolean

    <asp:GridView ID="grdTaskDataCat1" OnRowCommand="grdTaskDataCat1_RowCommand" OnSorting="grdTaskDataCat1_Sorting" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true" AllowSorting="true" Width="100%" DataKeyNames="ID" runat="server" OnRowDataBound="grdTaskDataCat1_RowDataBound">
    <Columns>                                                            <asp:TemplateField HeaderText="Sr No" Visible="false">                                                      <ItemTemplate>                                                               <asp:TextBox ID="txtId" Visible="false" ReadOnly Style="width: 30%" Text='<%# Bind("ID")%>' runat="server"></asp:TextBox>                                                  </ItemTemplate>                                                          </asp:TemplateField>

    </Columns> 
    </asp:GridView>  

On ROW data bound of the gridview

protected void grdTaskDataCat1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
TextBox txt = (TextBox)e.Row.FindControl("txtTaskName");

                if (txt.Text != "")
                {
                    txt.Attributes.Add("readonly", "readonly");

                }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM