[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

Dynamically Generating html file from c sharp at runtime

I am trying to create a web page. It should design the page at runtime with c#. Lets assume the following example:
1.there is a panel.
2. Inside the panel there is a HTML Table.
2. Inside HTML table there are 1 ROW and 2 Columns.
3. Inside first Column there is a label field and in the second column there is a textbox field.

Please give a sample code in c#.
0
salmanfazal
Asked:
salmanfazal
  • 5
  • 3
1 Solution
 
CB_ThirumalaiCommented:
0
 
CB_ThirumalaiCommented:
It also has the C# code with it.
0
 
CB_ThirumalaiCommented:
Here is the sample of your requirement.

Have a PlaceHolder control inside your ASPX page as below.  This will make sure that the Server Controls are added with the <form> tag to avoid any runtime error on the page.  This is because, if you have this.Page.Controls.Add, then we are not sure whether the controls are added inside the form element.

So,

<asp:PlaceHolder ID="phContent" runat="server"></asp:PlaceHolder>

And, place the below code in the Page_Load event, or you can manipulate to place it in any other event as well.
Panel p = new Panel();
p.ID = "PanelControl";

HtmlTable ht = new HtmlTable();
ht.ID = "htTable1";
HtmlTableRow htr = new HtmlTableRow();
htr.ID = "htrRow1";
HtmlTableCell htc1 = new HtmlTableCell();
Label htc1Label = new Label();
htc1Label.ID = "htc1Label";
htc1Label.Text = "Name";
htc1.Controls.Add(htc1Label);

HtmlTableCell htc2 = new HtmlTableCell();
TextBox htc2TextBox = new TextBox();
htc2TextBox.ID = "htc2TextBox";
htc2TextBox.Text = "salmanfazal";
htc2.Controls.Add(htc2TextBox);

htr.Cells.Add(htc1);
htr.Cells.Add(htc2);
ht.Rows.Add(htr);

p.Controls.Add(ht);
phContent.Controls.Add(p);

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
salmanfazalAuthor Commented:
I think i am missing some reference, its giving me the following error.

The type or namespace name 'HtmlTable' could not be found (are you missing a using directive or an assembly reference?)

Another query is ,  make one button in the same above code and define an event on it.

0
 
CB_ThirumalaiCommented:
Import Namespace as Using System.Web.UI.HtmlControls;

For the second question, here is the modified code.  An Event is also appended.  You have to manipulate it further.
Panel p = new Panel();
p.ID = "PanelControl";

HtmlTable ht = new HtmlTable();
ht.ID = "htTable1";
HtmlTableRow htr = new HtmlTableRow();
htr.ID = "htrRow1";
HtmlTableCell htc1 = new HtmlTableCell();
Label htc1Label = new Label();
htc1Label.ID = "htc1Label";
htc1Label.Text = "Name";
htc1.Controls.Add(htc1Label);

HtmlTableCell htc2 = new HtmlTableCell();
TextBox htc2TextBox = new TextBox();
htc2TextBox.ID = "htc2TextBox";
htc2TextBox.Text = "salmanfazal";
htc2.Controls.Add(htc2TextBox);

HtmlTableCell htc3 = new HtmlTableCell();
Button htc3Button = new Button();
htc3Button.ID = "htc3Button";
htc3Button.Text = "Click Me";
htc3Button.Click += new EventHandler(htc3Button_Click);
htc3.Controls.Add(htc3Button);

htr.Cells.Add(htc1);
htr.Cells.Add(htc2);
htr.Cells.Add(htc3);

ht.Rows.Add(htr);

p.Controls.Add(ht);

phContent.Controls.Add(p);



        void htc3Button_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

Open in new window

0
 
salmanfazalAuthor Commented:
One last question, lets assume that above page is loaded based on user profile with the field level security.
 I mean to say i am designing the page at runtime with code. I will design the controls based on database (each field).
    1. How to manage or arrange the controls in this scenario keeping in mind we are using HTML tables to place the controls

    2. when the button control is hidden to the user. How to handle the events? What to do in this scenario?
0
 
CB_ThirumalaiCommented:
1. It is based on your requirement.  You can do or arrange the way you like based on the requirement, just that your logic should be correct.
2. If the button is hidden, then there is no way the user can trigger the button related events.  But if you want to call any code you may put that in a common function and call the function from both the button events and also from anywhere else.
0
 
salmanfazalAuthor Commented:
Thanks!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now