【问题标题】:Summoning Div from database in foreach - asp.net c#从 foreach 中的数据库中调用 Div - asp.net c#
【发布时间】:2014-12-09 11:41:14
【问题描述】:

我得到了这样的数据库行“val1-val2-val5”,在我的 aspx 页面中,我得到了 5 个 div(他们的 id 的 val1、val2、val3...等),所有这些都是可见的错误。我希望它们在基于信息的数据库行中可见。

我用这个方法;

 string query = @"SELECT [Params]     
 FROM [Products] WHERE Id = '"+id+"'";
        string result= Library.Database.ExecuteScalar(query).ToString();

        string[] results;
        results= result.Split('-');

        foreach (var item in results)
        {
            switch (item)
            {
                case "val1": val1.Visible = true;
                    break;
                case "val2": val2.Visible = true;
                    break;
                case "val3": val3.Visible = true;
                    break;
                case "val4": val4.Visible = true;
                    break;
                case "val5": val5.Visible = true;
                    break;
            }
        }

我想知道是否有办法在没有“switch-case”的情况下做到这一点,例如

    foreach (Literal item in results)
      {item.visible = true }

谢谢你..

【问题讨论】:

    标签: c# html asp.net foreach


    【解决方案1】:

    嗯。你可以尝试这样的事情。

    foreach(var item in results)
    {
    Control Div_In_the_Page = FindControl(item);
    if(Div_In_the_Page  != null)
        Div_In_the_Page.visible = true;
    }
    

    这只是您可以尝试的模型。我不完全确定

     Div_In_the_Page.visible = true; //dont know if this will work. Else just use the css attributes to sort that out 
    

    当您想从服务器端编辑 div 时,您需要确保的一件事是确保 div 具有此属性

    <div runat="server"><!-- put things in here --></div>
    

    【讨论】:

    • Div_In_the_Page 即将为空。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 2021-06-29
    • 2018-11-09
    • 1970-01-01
    • 2016-01-02
    相关资源
    最近更新 更多