Programmatically Accessing Html Control Created in ASP.NET

2k Views Asked by At

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

There are 2 best solutions below

1
On

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());
0
On

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.