How to set div from code behind [Resolved]

Posted by pranaykssoftech-26135 under ASP.NET on 3/13/2014 | Points: 10 | Views : 1098 | Status : [Member] | Replies : 3
Hi,

I am trying to do design in code behind file. Suppose --

---------------------------------------------------------------------------------------------------
<div id='dv1' runat='server' style='margin-top: 15px;'>
Test Content
<div>
---------------------------------------------------------------------------------------------------

is design page, but I am trying it in code behind file like this --

---------------------------------------------------------------------------------------------------
System.Web.UI.HtmlControls.HtmlGenericControl dv1 = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
dv1.ID = "dv1";
dv1.Style.Add("margin-top", "15px");
this.Controls.Add(dv1);

public string str1 += dv1
public string str1 += "Test Content"
---------------------------------------------------------------------------------------------------

str1 is my public string which will be used in aspx design. Now the question is how to close this div element and how can I put my other controls between starting div tag and ending div tag.

Thanks in advance.




Responses

Posted by: A2H on: 3/13/2014 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Hi,
When you use the below line of code to generate div you don't need to worry about closing tags.It render with closing tags at runtime.
System.Web.UI.HtmlControls.HtmlGenericControl dv1 = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV"); 

To adding other controls to your div you can use the below codes

//You can add your controls here
//Sample Example for text box
TextBox objtxtbox = new TextBox();
objtxtbox.ID = "txtbox1";
dv1.Controls.Add(objtxtbox);


One thing you need to note here when you add controls to div you need to ensure that the div is added to form.

//Add your div to your form in page
form1.Controls.Add(dv1);

Assuming that you have form tag in your page like given below
<form id="form1" runat="server"> 
</form>


Complete Code is given below

System.Web.UI.HtmlControls.HtmlGenericControl dv1 = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
dv1.ID = "dv1";
dv1.Style.Add("margin-top", "15px");
//Assigning your text to div here
dv1.InnerText = "Text Content";
//You can add your controls here
//Sample Example for text box
TextBox objtxtbox = new TextBox();
objtxtbox.ID = "txtbox1";
dv1.Controls.Add(objtxtbox);
//Add your div to your form in page
form1.Controls.Add(dv1);


Thanks,
A2H
My Blog

pranaykssoftech-26135, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: pranaykssoftech-26135 on: 3/20/2014 [Member] Starter | Points: 25

Up
0
Down
Thank you very much.
That is what I am looking for.
Thanks a lot.

pranaykssoftech-26135, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/20/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Glad to be of help
Have a Great day....

Thanks,
A2H
My Blog

pranaykssoftech-26135, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response