Html ASP NET SQL-为每行创建一个新div
正在寻找一种解决方案,以便为SQL Server数据库表中的每一行创建一个包含其内容的新div:Html ASP NET SQL-为每行创建一个新div,html,asp.net,sql-server,vb.net,foreach,Html,Asp.net,Sql Server,Vb.net,Foreach,正在寻找一种解决方案,以便为SQL Server数据库表中的每一行创建一个包含其内容的新div: Dim com As SqlCommand = New SqlCommand("select Date,Title,Content from Log where Author = @Author", con) com.Parameters.Add("@Author", SqlDbType.Char) com.Parameters("@Author").Value = User.Identity.Na
Dim com As SqlCommand = New SqlCommand("select Date,Title,Content from Log where Author = @Author", con)
com.Parameters.Add("@Author", SqlDbType.Char)
com.Parameters("@Author").Value = User.Identity.Name
Dim dt As DataTable = New DataTable()
For Each Datarow In dt.Rows
//create New div with its content
Next
这一个正在工作,但是还不能让它创建一个包含其内容的新div/表
谢谢你的帮助
DataSet ds = new DataSet();
SqlDataAdapter da1 = new SqlDataAdapter("select Date,Title,Content from Log where Author = @Author", ConfigurationManager.ConnectionStrings["sasa"].ConnectionString);
da1.Fill(ds);
string makehtml = string.Empty;
//however when you have DataSet then you can iterate easily and cerate whatever you want
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
makehtml += "<div><span>Date:</span> " + ds.Tables[0].Rows[i]["Date"] + " <span>Title:</span> " + ds.Tables[0].Rows[i]["Title"] + "< span > Content:</ span > " + ds.Tables[0].Rows[i]["Content"] + " </div>";
}
您还可以使用repeater代替Listview。
另一种方法可以完成相同的操作:
<%-- statrt here --%>
<% if (ds.Tables[0].Rows.Count > 0){
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{%>
<div class="col-md-12">
<div class="col-md-12"><span>Date:</span><%=ds.Tables[0].Rows[i]["Date"].ToString()%>
<span>Title:</span><%=ds.Tables[0].Rows[i]["Title"].ToString()%><span> Content:</span><%=ds.Tables[0].Rows[i]["Content"].ToString()%></div>
</div>
<%}} %>
<%-- Ends here --%>
0){
对于(int i=0;i
日期:
标题:内容:
此代码将出现在aspx页面中,但为此,您必须公开数据集,以便轻松访问。Dim com As SqlCommand=New SqlCommand(“从日志中选择日期、标题、内容,其中Author=@Author”,con)
Dim com As SqlCommand = New SqlCommand("select Date,Title,Content from Log where Author = @Author", con)
com.Parameters.Add("@Author", SqlDbType.Char)
com.Parameters("@Author").Value = User.Identity.Name
Dim dt As DataTable = New DataTable()
Dim _div As New HtmlGenericControl("div")
Dim _label As New HtmlGenericControl("label") ' for example that content you want to display is the fullname, for my example i will use label tag
Dim _indx As Integer = 0
For Each row In dt.Rows
_div = New HtmlGenericControl("div")
_div .ID = "div" & _indx.toString().trim
_div .Attributes.Add("class", "your class that you want to add") ' optional only if you want
_label = New HtmlGenericControl("label")
_label.ID = "lbl" & _indx.toString().trim
_label.InnerText = row("name of the field in your dt ex. fullname").toString().trim
_div .Controls.Add(_label)
_pnl1.Controls.Add(_div)
Next
.. In the design page
<body>
<asp:Panel ID="pnl1" runat="server"> <!-- you can use panel to properly place your dynamic div -->
</asp:Panel>
</body>
添加(“@Author”,SqlDbType.Char)
com.Parameters(“@Author”).Value=User.Identity.Name
作为数据表的Dim dt=新数据表()
Dim_div作为新的HtmlGenericControl(“div”)
Dim _labelas New HtmlGenericControl(“label”)'例如,要显示的内容是全名,例如,我将使用label标记
Dim_indx为整数=0
对于dt.行中的每一行
_div=新的HtmlGenericControl(“div”)
_div.ID=“div”和_indx.toString()修剪
_添加(“类”,“您要添加的类”)”仅在需要时可选
_标签=新的HtmlGenericControl(“标签”)
_label.ID=“lbl”和_indx.toString()修剪
_label.InnerText=row(“dt ex.fullname中的字段名称”).toString().trim
_div.Controls.Add(_标签)
_pnl1.Controls.Add(\u div)
下一个
…在设计页面中
…希望这段代码有帮助在Web表单中,通常的做法是将数据表绑定到某种控件,如转发器。您知道如何将css样式应用于div吗?如下面的
makehtml+=“css将正常工作。您可以使用style属性,也可以使用css类。例如:@Luke发送一个示例,然后我将完全理解您的css问题。如果css类不工作,请尝试使用内联css。在Web表单中向网页添加重复内容,这确实是一种可怕的方式。”。
<%-- statrt here --%>
<% if (ds.Tables[0].Rows.Count > 0){
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{%>
<div class="col-md-12">
<div class="col-md-12"><span>Date:</span><%=ds.Tables[0].Rows[i]["Date"].ToString()%>
<span>Title:</span><%=ds.Tables[0].Rows[i]["Title"].ToString()%><span> Content:</span><%=ds.Tables[0].Rows[i]["Content"].ToString()%></div>
</div>
<%}} %>
<%-- Ends here --%>
Dim com As SqlCommand = New SqlCommand("select Date,Title,Content from Log where Author = @Author", con)
com.Parameters.Add("@Author", SqlDbType.Char)
com.Parameters("@Author").Value = User.Identity.Name
Dim dt As DataTable = New DataTable()
Dim _div As New HtmlGenericControl("div")
Dim _label As New HtmlGenericControl("label") ' for example that content you want to display is the fullname, for my example i will use label tag
Dim _indx As Integer = 0
For Each row In dt.Rows
_div = New HtmlGenericControl("div")
_div .ID = "div" & _indx.toString().trim
_div .Attributes.Add("class", "your class that you want to add") ' optional only if you want
_label = New HtmlGenericControl("label")
_label.ID = "lbl" & _indx.toString().trim
_label.InnerText = row("name of the field in your dt ex. fullname").toString().trim
_div .Controls.Add(_label)
_pnl1.Controls.Add(_div)
Next
.. In the design page
<body>
<asp:Panel ID="pnl1" runat="server"> <!-- you can use panel to properly place your dynamic div -->
</asp:Panel>
</body>