C# 在foreach-asp.netc中从数据库调用Div
我的数据库行是这样的val1-val2-val5,在我的aspx页面中我得到了5个div,它们的id是val1,val2,val3。。。等等,所有这些都是虚假的。我希望它们与基于信息的数据库行一致 我用这个方法,C# 在foreach-asp.netc中从数据库调用Div,c#,html,asp.net,foreach,C#,Html,Asp.net,Foreach,我的数据库行是这样的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).ToSt
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;
}
}
例如,我想知道是否有一种方法可以在没有开关盒的情况下实现这一点
foreach (Literal item in results)
{item.visible = true }
谢谢..嗯。你可以试试这样的东西
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_在_页面中为空。