我在前台中定义了TextBox列如下:
<asp:TemplateField HeaderText="数量">
<ItemTemplate>
   <asp:TextBox   ID="textboxband"  Runat=Server  BorderStyle="None" BackColor="#d4d0c8" Text=<%#DataBinder.Eval(Container.DataItem, "lQuantity")%>/>
</ItemTemplate>
<ItemStyle Width="15%" />
</asp:TemplateField>请问我如何能在后台代码中修改TextBox的属性。因为我在网格中不同记录需要设置不同的TextBox外观。

解决方案 »

  1.   

    ----绑定后,根据绑定的不同数据,定制不同外观---------
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox tb = e.Row.Cells[3].FindControl("TextBox1") as TextBox;//我这里是第4列
            if (tb != null)
            {
                if(tb.Text.StartsWith("A"))
                    tb.BackColor = System.Drawing.Color.Red;
                else if (tb.Text.StartsWith("B"))
                    tb.BackColor = System.Drawing.Color.Blue;
            }
        }
    }------------------
    如果你不是在绑定时期操作,可以随时用下面方法改其外观(例如,按下某按钮后)
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox tb = GridView1.Rows[0].Cells[3].FindControl("TextBox1") as TextBox;//第一行第4列
        if (tb != null)
        {
            tb.ForeColor = System.Drawing.Color.Red;
            tb.BorderWidth = Unit.Pixel(3);
        }
    }==== 
    ~~~~ 我的Blog:http://blog.csdn.net/quou2002