假设  一共3列数据  A,B,C
A,C是直接绑定显示<%#Eval("A") %>  <%#Eval("C") %>
B是textbox文本框<asp:TextBox ID="TextBox1" runat="server" Width="45"  Text='<%#Eval("B") %>'></asp:TextBox>我想在数据绑定的时候,判断,如果C=1,对应的B的文本框为不可修改,
如果C=2,对应的B的文本框正常可修改
问:需要写在哪个事件?如何写?
r是对象,不用在意,我下在下面这个事件里,但是没用,求教
Data_g_ItemDataBound
if (r.B== "1")
        {
            TextBox text = (TextBox)e.Item.FindControl("TextBox1");
            text.ReadOnly = false;
        }
        if (r.B== "2")
        {
            TextBox text = (TextBox)e.Item.FindControl("TextBox1");
            text.ReadOnly = true;
        }

解决方案 »

  1.   

    asp.net GridView控件没有ItemDataBound事件。你的ItemDataBound那里来的?
      

  2.   

    webform的GridView控件要用RowBound事件我的测试代码
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                onrowdatabound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:BoundField DataField="age" HeaderText="Age" />
                    <asp:TemplateField HeaderText="Description">
                        
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server"  Text='<%# Eval("Description") %>' ></asp:TextBox>
                        </ItemTemplate>
                        
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>    public partial class WebForm3 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {            if (!IsPostBack)
                {
                    List<Person> people = new List<Person>()
                    {
                        new Person(){Name = "David", age = 10, description="this is kid"},
                        new Person(){Name = "Jack", age = 30, description="this is man"},
                        new Person(){Name = "Mary", age = 40, description="this is woman"},
                        new Person(){Name = "Mark", age = 3, description="this is baby"}                };                GridView1.DataSource = people;
                    GridView1.DataBind();
                }        }        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    int age = int.Parse(e.Row.Cells[1].Text);
                    if (age <= 5)
                    {
                        TextBox descriptionControl = e.Row.FindControl("TextBox1") as TextBox;
                        if (descriptionControl != null)
                            descriptionControl.ReadOnly = true;
                    }
                    
                }        }
        }    class Person
        {
            public string Name { get; set; }
            public int age { get; set; }
            public string description { get; set; }
        }
      

  3.   

    rowdatabound事件
    text.Enable = false;
      

  4.   

    在帖一遍:
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                onrowdatabound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:BoundField DataField="age" HeaderText="Age" />
                    <asp:TemplateField HeaderText="Description">
                        
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server"  Text='<%# Eval("Description") %>' ></asp:TextBox>
                        </ItemTemplate>
                        
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        public partial class WebForm3 : System.Web.UI.Page
        {
            
            protected void Page_Load(object sender, EventArgs e)
            {            if (!IsPostBack)
                {
                    List<Person> people = new List<Person>()
                    {
                        new Person(){Name = "David", age = 10, description="this is kid"},
                        new Person(){Name = "Jack", age = 30, description="this is man"},
                        new Person(){Name = "Mary", age = 40, description="this is woman"},
                        new Person(){Name = "Mark", age = 3, description="this is baby"}                };                GridView1.DataSource = people;
                    GridView1.DataBind();
                }        }        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    int age = int.Parse(e.Row.Cells[1].Text);
                    if (age <= 5)
                    {
                        TextBox descriptionControl = e.Row.FindControl("TextBox1") as TextBox;
                        if (descriptionControl != null)
                            descriptionControl.ReadOnly = true;
                    }
                    
                }        }
        }    class Person
        {
            public string Name { get; set; }
            public int age { get; set; }
            public string description { get; set; }
        }
      

  5.   

    能DataGrid 改写下吗?GridView的有些不一样
      

  6.   

    我用的是DataGrid 不是GridView
      

  7.   

    还真有,我从来没有用过,都是GridViewhttp://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.aspx
      

  8.   

    我要是能看的懂连接里的东西,就不用发帖了.....
    你的能改成DataGrid版的么