gpt4 book ai didi

c# - 如何为每个循环创建新的 div

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:42 24 4
gpt4 key购买 nike

我正在将 asp.net 与 c# 和母版页一起使用。我试图同时为每个循环创建新的 div,以便能够按照我的意愿制作指定者。有什么方法可以做到这一点,或者如果仅从我的 aspx.cs 中,还有其他方法可以做到吗?

aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
while (Reader.Read())
{
Response.Write("<div id=test >");

Response.Write(Reader.GetString(3)+" ");
Response.Write(Reader.GetString(10) + "<br />");
Response.Write("<a href='Info.aspx?param=" + Reader.GetString(0) + "' target='_blank'>" + "More..." + "</a>");

Response.Write( "<br />");
Response.Write("");
Response.Write("</div>");

//TextBox1.Text = Reader.GetString(3);
//TextBox2.Text = Reader.GetString(10);

}
}

aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/MainDesigne.Master" AutoEventWireup="true" CodeBehind="MobilePageRouteDoc.aspx.cs" Inherits="WebApplication2.MobilePageRoteDoc" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">



<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js">
</asp:ScriptReference>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js">
</asp:ScriptReference>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js">
</asp:ScriptReference>
</Scripts>
</telerik:RadScriptManager>

<link href="Assets/Css/mobile.css" rel="stylesheet" />

<telerik:RadDropDownList ID="RadDropDownList1" runat="server" OnSelectedIndexChanged="RadDropDownList1_SelectedIndexChanged">
</telerik:RadDropDownList>

<div id="test">



</div>

</asp:Content>

最佳答案

您可以分配您的 <div id="test">属性 runat="server"像这样:

<div runat="server" id="test">

然后你就可以在后面的代码中访问它了:

test.InnerHtml = "your html here";

所以基本上首先在循环中创建您的 html(多个 div),然后将其添加到“测试”div。此外,我建议您将在循环中创建的每个元素的 ID 保持唯一,并且不要忘记添加双引号... id="test1" , id="test2" , 这是您修改后的代码:

protected void Page_Load(object sender, EventArgs e)
{
string html = "";
int i = 0;
while (Reader.Read())
{
html+= "<div id=\"test" + i + "\">";
html+= Reader.GetString(3) + " ";
html+= Reader.GetString(10) + "<br />";
html+= "<a href='Info.aspx?param=" + Reader.GetString(0) + "' target='_blank'>" + "More..." + "</a>";
html+= "<br />";
html+= "</div>";
i++;
}
test.InnerHtml = html;
}

关于c# - 如何为每个循环创建新的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29840040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com