gpt4 book ai didi

c# - asp中继器按钮文本更改

转载 作者:行者123 更新时间:2023-11-30 13:22:30 25 4
gpt4 key购买 nike

美好的一天,我希望转发器内的按钮文本根据 sql 选定值的内容动态更改。

这是我的代码:

asp.cs

if (!IsPostBack) 
{
string getEmp = "Select employeeid, photo, lastname, firstname, departmentname, designation,userType from tblEmployee e inner join tblDepartment d on d.departmentid=e.department";
SqlCommand com = new SqlCommand(getEmp,con);
con.Open();
SqlDataReader dr = com.ExecuteReader();
Button btnSet = (Button)FindControl("btnSet");
if (dr.HasRows)
{
if (dr.Read())
{
if (btnSet != null)
{
if (dr["userType"].ToString() == "2")
{
btnSet.Text = "Set as Normal User";
}
else
{
btnSet.Text = "Set as Power User";
}
}
}
}
Repeater1.DataSource = dr;
Repeater1.DataBind();
dr.Dispose();
con.Close();

aspx

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<asp:Button ID="btnSet" commandname="set" commandargument=<%# Eval ("employeeid") %> runat="server" class="tiny success expand button" Text="" />

最佳答案

您可以订阅转发器的ItemDatabound事件。它允许您访问项目的控件并根据当前项目的值更改它们:

private void Repeater1_ItemDatabound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Retrieve button of the line
var btn = e.Item.FindControl("btnSet") as Button;
if (btn != null)
{
// Set text of button based on e.Item.DataItem;
}
}
}

关于c# - asp中继器按钮文本更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21522220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com