你好。我在asp.net中的另一个div中创建了一个新的div。现在我想使用新创建的div的innerhtml属性。我找不到办法......
这就是我在.aspx页面中所做的..
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="forNewEmployee" runat="server"></div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="link" EventName="click" />
</Triggers>
</asp:UpdatePanel>
<div class="addMore">
<asp:Button ID="link" CssClass="label" runat="server" Text="Add More - Image"
onclick="link_Click" />
</div>
并在codebehind文件..
protected void link_Click(object sender, EventArgs e)
{
if (clickCheck != 0)
{
// access the newly generated div 'forNewEmployee' + specific id and call its innerHtml Method?
System.Web.UI.HtmlControls.HtmlControl div = (System.Web.UI.HtmlControls.HtmlControl)ScriptManager1.FindControl("forNewEmployee" + clickCheck);
}
else {
this.forNewEmployee.InnerHtml = newRow();
}
}
我有一个函数,我正在构建字符串以在forNewEmployee div中引入新元素。这是如何定义..
protected string newRow() {
// check for click
clickCheck++;
// check for new row
countForEmployee++;
// building our new row
rowString.AppendLine("<label for='empName" + countForEmployee.ToString() + "'>Your Employee Name: <span>(required)</span></label>");
rowString.AppendLine("<input id='empName" + countForEmployee.ToString() + "' class='registrationformEmployeeText' />");
rowString.AppendLine("</div>");
rowString.AppendLine("<div id='forNewEmployee" + countForEmployee.ToString() + "' name='forNewEmployee" + countForEmployee.ToString() + "' runat='server'></div>");
return rowString.ToString();
}
答案 0 :(得分:0)
我不完全确定您可以使用div
执行此操作但请确保您可以使用asp:Panel
执行此操作:
this.forNewEmployee.Controls.Add(new Literal(newRow());
答案 1 :(得分:0)
最简单的方法是使用asp .net面板,创建并添加asp .net控件(即使它们是文字控件)。
所以基本上你的NewRow功能将是:
protected Panel newRow() {
// check for click
clickCheck++;
// check for new row
countForEmployee++;
// building our new row
Panel pnl = new Panel();
pnl.Controls.Add(new Literal("<label for='empName" + countForEmployee.ToString() + "'>Your Employee Name: <span>(required)</span></label>"));
pnl.Controls.Add(new Literal("<input id='empName" + countForEmployee.ToString() + "' class='registrationformEmployeeText' />"));
Panel forNewEmployee = new Panel();
forNewEmployee.ID = "forNewEmployee" + countForEmployee.ToString();
pnl.Controls.Add(forNewEmployee);
return pnl;
}
这样,您就可以使用您指定的控件ID执行FindControl。