我有两个标签(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;
}发布于 2016-07-02 11:14:16
另一个解决办法是
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")的值
可以直接比较每个项绑定上隐藏字段中存储的值。
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事件的文章
发布于 2016-07-02 10:58:48
您可以使用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>发布于 2016-07-02 13:03:20
尝尝这个
else if (dt.Rows[0]["d"].ToString() == "h")
{
(Label)(Repeater1.FindControl("Label4")).Visible = false;
(Label)(Repeater1.FindControl("Label5")).Visible = false; /*To hide text*/
}https://stackoverflow.com/questions/38158724
复制相似问题