首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据表中存储的数据隐藏和显示中继器内的标签

如何根据表中存储的数据隐藏和显示中继器内的标签
EN

Stack Overflow用户
提问于 2016-07-02 09:43:58
回答 3查看 1.9K关注 0票数 0

我有两个标签(label4和label5),其中文本从存储在表(tblp)的列b和列c中的数据中检索。标签在中继器控件中存在,现在根据存储在同一表d列中的数据,我试图使用以下规则在页面加载时显示和隐藏label4和label5的文本:

  • 如果列d包含字母s,那么两个标签文本都应该是可见的。
  • 如果列d包含字母h,那么两个标签文本都应该是不可见的。
  • 如果列d包含字母u,那么两个标签文本都应该是可见的。

问题是,当试图使用标签作为我所做方法的参数时,标签会显示错误。

下面给出了我所做工作的代码示例。

HTML

代码语言:javascript
复制
  <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>

代码背后

代码语言:javascript
复制
   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();
            }
        }

    }


}

使用方法

代码语言:javascript
复制
 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;

}
EN

回答 3

Stack Overflow用户

发布于 2016-07-02 11:14:16

另一个解决办法是

1.在中继器内放一个HiddenField,然后像这样使用Repeater_ItemDataBound

代码语言:javascript
复制
 <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>
  1. 使用Repeater1_ItemDataBound迭代每个Repeater项,并通过检查条件将可见性设置为true或false

更新:由于您正在将D列值存储到隐藏字段,因此不需要比较类似于以下(dt.Rows[0]["d"].ToString() == "h")的值

可以直接比较每个项绑定上隐藏字段中存储的值。

代码语言:javascript
复制
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事件的文章

票数 1
EN

Stack Overflow用户

发布于 2016-07-02 10:58:48

您可以使用Visible属性

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2016-07-02 13:03:20

尝尝这个

代码语言:javascript
复制
else if (dt.Rows[0]["d"].ToString() == "h")
    {
        (Label)(Repeater1.FindControl("Label4")).Visible = false;
       (Label)(Repeater1.FindControl("Label5")).Visible = false;  /*To hide text*/
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38158724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档