我有两个标签(label4和label5),其中文本是从存储在表(tblp)的b列和c列中的数据中检索的。标签存在于转发器控件内,现在根据存储在同一表的列d中的数据,我试图在页面加载时使用以下规则显示和隐藏label4和label5的文本:
问题在于,当我尝试使用标签作为我所制作方法的参数时,标签会显示错误。
下面给出了我所做的代码示例。
HTML
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Eval("b") %>'></asp:Label>
<asp:Label ID="Label5" runat="server" Text='<%#Eval("c") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
背后的代码
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = j.getentry(Label4.Text, Label5.Text);
if (dt.Rows.Count>0)
{
if (dt.Rows[0]["d"].ToString() == "s")
{
DataTable dp = j.getall();
if (dp.Rows.Count > 0)
{
Repeater1.DataSource = dt; /*To show text*/
Repeater1.DataBind();
}
}
else if (dt.Rows[0]["d"].ToString() == "h")
{
Label4.Visible = false;
Label5.Visible = false; /*To hide text*/
}
else if (dt.Rows[0]["d"].ToString() == "u")
{
DataTable dp = j.getall();
if (dp.Rows.Count > 0)
{
Repeater1.DataSource = dt; /*To show text*/
Repeater1.DataBind();
}
}
}
}
使用的方法
public DataTable getentry(string b, string c)
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
string sql = "select *from tblp where b=@b and c=@c ";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@b", b);
cmd.Parameters.AddWithValue("@c", c);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable getall()
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
string sql = "select a,b,c from tblp";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dp = new DataTable();
da.Fill(dp);
return dp;
}
答案 0 :(得分:1)
另一种解决方案是
1.在内置转发器中放置HiddenField
并使用Repeater_ItemDataBound
这样的
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:HiddenField runat="server" ID="hidd1" Value='<%#Eval("d") %>' />
<asp:Label ID="Label4" runat="server" Text='<%#Eval("b") %>'></asp:Label>
<asp:Label ID="Label5" runat="server" Text='<%#Eval("c") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
Repeater1_ItemDataBound
遍历每个Repeater项,并通过检查条件将可见性设置为true或false 更新:由于您要将D列值调到隐藏字段,因此无需比较您的值(dt.Rows[0]["d"].ToString() == "h")
您可以直接比较每个项目绑定中存储在hiddenfield中的值
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
string hidd1 = ((HiddenField)(e.Item.FindControl("hidd1"))).Value; // find hidden field
Label l1 = (Label)(e.Item.FindControl("Label4"));// find lable4 value
Label l2 = (Label)(e.Item.FindControl("Label4"));//// find lable5 value
if (hidd1.ToLower == "s")// your conditon
{
l1.Visible = true;// your code1
l2.Visible = true;
}
else if (hidd1.ToLower == "h")
{
l1.Visible = false;// your code2
l2.Visible = false;
}
else
{
// defalut
}
}
如果您仍有疑问,请详细了解 Repeater.ItemDataBound Event here
答案 1 :(得分:0)
您可以使用Visible
属性
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Visible='<%# Convert.ToBoolean(Eval("SomeCondition")) %>' Text='<%#Eval("b") %>'></asp:Label>
<asp:Label ID="Label5" runat="server" Visible='<%# Convert.ToBoolean(Eval("SomeCondition")) %>' Text='<%#Eval("c") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
答案 2 :(得分:0)
试试这个
else if (dt.Rows[0]["d"].ToString() == "h")
{
(Label)(Repeater1.FindControl("Label4")).Visible = false;
(Label)(Repeater1.FindControl("Label5")).Visible = false; /*To hide text*/
}