我正在使用带有母版页的c#的asp.net。我试图为while中的每个循环创建新的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>
答案 0 :(得分:2)
您可以像这样分配<div id="test">
属性runat="server"
:
<div runat="server" id="test">
然后您可以在后面的代码中访问它:
test.InnerHtml = "your html here";
所以基本上首先在循环中创建你的html(多个div),然后将它添加到'test'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;
}
答案 1 :(得分:1)
如果您想添加<div>
,可以使用可生成HTML的HtmlGenericControl
关注此内容
HtmlGenericControl div = new HtmlGenericControl();
div.ID = "AnyDivID";
div.TagName = "div";
div.Attributes["class"] = "YourClassName";
div.InnerText = string.Format("Inner text= {0} ;", "YourInnerText");
form1.Controls.Add(div);