如何在gridview中只读文本框?

时间:2014-09-25 06:15:48

标签: c# asp.net gridview

我有一个gridview,在文本框中只能在少数情况下编辑..我已经尝试了以下代码,但它无法解决这个问题并使文本框不可编辑..

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;
           }

4 个答案:

答案 0 :(得分:1)

你试过这个:

    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
        }
      }
    }

答案 1 :(得分:0)

正如评论中所示,您可以在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;
    }
}

答案 2 :(得分:0)

我更喜欢在网格列的页面中使用数据绑定表达式     

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

Wehre YourMethodThatReturnCondition是一个返回布尔值的方法

答案 3 :(得分:0)

    <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>  

在gridview的ROW数据绑定

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");

                }
}
}