以编程方式访问在 ASP.NET 中创建的 Html 控件

发布于 2024-12-28 05:30:50 字数 2040 浏览 2 评论 0原文

你好呀。我在 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>   

和代码隐藏文件中..

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();
}

Hello there. I have created a new div inside another div in asp.net. Now I want to use the innerhtml property of that newly created div. I can't find a way..

This is what im doing in my .aspx page..

      <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>   

and in codebehind file..

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();        
    }
}

I have a function in which I'm building the string to introduce new elements inside the forNewEmployee div. This is how it is defined..

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青芜 2025-01-04 05:30:50

我不完全确定您可以使用 div 做到这一点,但确定您可以使用 asp:Panel 做到这一点:

this.forNewEmployee.Controls.Add(new Literal(newRow());

I'm not entirely sure you can do it with div but sure you can do it with asp:Panel:

this.forNewEmployee.Controls.Add(new Literal(newRow());
醉南桥 2025-01-04 05:30:50

最简单的事情是使用 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。

The easiest thing would be to use the asp .net panel, and create and add asp .net controls (even if they are literal controls).

So basically your NewRow function will be:

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;

} 

This way, you will be able to do a FindControl with Control ID that you specified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文