假设 一共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;
}
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;
}
<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; }
}
text.Enable = false;
<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; }
}
你的能改成DataGrid版的么