我有一个中继器(名为list_user
),其中包含字段(id_user
,name
,mail
)和按钮contact
。我想执行允许禁用某些用户名的联系人按钮的控件
这是我的代码
aspx:
<asp:Repeater ID="list_user" runat="server" OnItemDataBound="list_user_ItemDataBound1">
<HeaderTemplate>
<table border="1" class="grid">
<thead>
<tr>
<th> Id user </th>
<th> username </th>
<th> mail</th>
<th> Contact</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" ID="id_user" Text='<%# Eval("iduser")%>'></asp:Label></td>
<td><asp:Label runat="server" ID="username" Text='<%# Eval("name")%>' ></asp:Label></td>
<td><asp:Label runat="server" ID="mail" Text='<%# Eval("mail_adress")%>'></asp:Label></td>
<td><asp:Button runat="server" ID="contact" Text="contact_user" OnClick="contact_Click1"/></td>
</ItemTemplate>
<FooterTemplate>
.....
C#后面的代码
protected void list_user_ItemDataBound1(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label nom = (System.Web.UI.WebControls.Label)e.Item.FindControl("username") as System.Web.UI.WebControls.Label;
System.Web.UI.WebControls.Button btn = (System.Web.UI.WebControls.Button)e.Item.FindControl("contact") as System.Web.UI.WebControls.Button;
if (nom.Text == "alex")
{ btn.Enabled = true; }
else
{ btn.Enabled = false; }
}
但显示没有任何反应
答案 0 :(得分:0)
我使用了上面的代码,它的运行效果非常好。您可能在 DataBind 上遇到了问题。您正在寻找有效的数据绑定。我在User类中使用了您的属性,并创建了用户列表,然后将其绑定到转发器控件,效果很好。
Aspx
<asp:Repeater ID="list_user" runat="server" OnItemDataBound="list_user_ItemDataBound1">
<HeaderTemplate>
<table border="1" class="grid">
<thead>
<tr>
<th> Id user </th>
<th> username </th>
<th> mail</th>
<th> Contact</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" ID="id_user" Text='<%# Eval("iduser")%>'></asp:Label></td>
<td><asp:Label runat="server" ID="username" Text='<%# Eval("name")%>' ></asp:Label></td>
<td><asp:Label runat="server" ID="mail" Text='<%# Eval("mail_adress")%>'></asp:Label></td>
<td><asp:Button runat="server" ID="contact" Text="contact_user" OnClick="contact_Click" /></td>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater>
代码文件
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
List<User> list = new List<User>();
User u = new User();
u.iduser = 0;
u.name = "alex";
u.uContact = "555399";
u.mail_adress = "fdsfsfdsddsff@gmail.com";
list.Add(u);
for (int i = 1; i < 5;i++ )
{
u = new User();
u.iduser = i;
u.name = "alex "+i;
u.uContact = "555399";
u.mail_adress = "fsddfsdsdf@gmail.com";
list.Add(u);
}
list_user.DataSource = list;
list_user.DataBind();
}
}
class User
{
public int iduser { get; set; }
public string name { get; set; }
public string mail_adress { get; set; }
public string uContact { get; set; }
}
protected void list_user_ItemDataBound1(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label nom = (System.Web.UI.WebControls.Label)e.Item.FindControl("username") as System.Web.UI.WebControls.Label;
System.Web.UI.WebControls.Button btn = (System.Web.UI.WebControls.Button)e.Item.FindControl("contact") as System.Web.UI.WebControls.Button;
if (nom.Text == "alex")
{ btn.Enabled = true; }
else
{ btn.Enabled = false; }
}
}
protected void contact_Click(object sender, EventArgs e)
{
//Add for click
}
输出结果