Get Controls in htmlstring
-
Hey. I am working on application in which I want to create dynamic controls and retrives them in html string. for example. public string CreateControl() { HtmlTable hTable = new HtmlTable(); hTable.CellPadding = 2; hTable.CellSpacing = 2; hTable.Width = "100%"; HtmlTableRow hRow = new HtmlTableRow(); HtmlTableCell hCell = new HtmlTableCell(); hCell.InnerText = "Username"; hRow.Cells.Add(hCell); hTable.Rows.Add(hRow); // Now here I want to return created controls in html string. } It should be like this <table CellPadding=2 CellSpacing = 2 Width=100><tr><td>Username</td></tr></table> Thanks Imrankhan
please don't forget to vote on the post that helped you.
-
Hey. I am working on application in which I want to create dynamic controls and retrives them in html string. for example. public string CreateControl() { HtmlTable hTable = new HtmlTable(); hTable.CellPadding = 2; hTable.CellSpacing = 2; hTable.Width = "100%"; HtmlTableRow hRow = new HtmlTableRow(); HtmlTableCell hCell = new HtmlTableCell(); hCell.InnerText = "Username"; hRow.Cells.Add(hCell); hTable.Rows.Add(hRow); // Now here I want to return created controls in html string. } It should be like this <table CellPadding=2 CellSpacing = 2 Width=100><tr><td>Username</td></tr></table> Thanks Imrankhan
please don't forget to vote on the post that helped you.
Your best bet to do that, is to use a string builder to generate the HTML yourself.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Your best bet to do that, is to use a string builder to generate the HTML yourself.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Christian Graus wrote:
Your best bet to do that, is to use a string builder to generate the HTML yourself.
I have to write all controls in HTML and I dont want to write inlinne code at server side. I know It is possible because I had done it before one year ago when I worked on custom control but now I forget it. :doh: regard Imrankhan
please don't forget to vote on the post that helped you.
-
Christian Graus wrote:
Your best bet to do that, is to use a string builder to generate the HTML yourself.
I have to write all controls in HTML and I dont want to write inlinne code at server side. I know It is possible because I had done it before one year ago when I worked on custom control but now I forget it. :doh: regard Imrankhan
please don't forget to vote on the post that helped you.
You're right, 5 seconds with google got me this:
public string RenderControl(Control ctrl)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);ctrl.RenderControl(hw); return sb.ToString();
}
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
You're right, 5 seconds with google got me this:
public string RenderControl(Control ctrl)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);ctrl.RenderControl(hw); return sb.ToString();
}
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Thanks Christian.
please don't forget to vote on the post that helped you.
-
Hey. I am working on application in which I want to create dynamic controls and retrives them in html string. for example. public string CreateControl() { HtmlTable hTable = new HtmlTable(); hTable.CellPadding = 2; hTable.CellSpacing = 2; hTable.Width = "100%"; HtmlTableRow hRow = new HtmlTableRow(); HtmlTableCell hCell = new HtmlTableCell(); hCell.InnerText = "Username"; hRow.Cells.Add(hCell); hTable.Rows.Add(hRow); // Now here I want to return created controls in html string. } It should be like this <table CellPadding=2 CellSpacing = 2 Width=100><tr><td>Username</td></tr></table> Thanks Imrankhan
please don't forget to vote on the post that helped you.
Imran Khan Pathan wrote:
hCell.InnerText = "Username"; hRow.Cells.Add(hCell); hTable.Rows.Add(hRow); // Now here I want to return created controls in html string.
Now use the following to get the string :
StringBuilder sb = new StringBuilder(); StringWriter tw = new StringWriter(sb); HtmlTextWriter hw = new HtmlTextWriter(tw); hTable.RenderControl(tw); string renderedoutput = sb.ToString();
Now find the control string from the object, inspect
renderedoutput
. :rose:Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Microsoft Bing MAP using Javascript
CLR objects in SQL Server 2005
Uncommon C# Keywords/xml> -
Thanks Christian.
please don't forget to vote on the post that helped you.
-
Imran Khan Pathan wrote:
please don't forget to vote on the post that helped you.
LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.
ROTFL - yes, that is quite ironic.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Imran Khan Pathan wrote:
please don't forget to vote on the post that helped you.
LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.
sashidhar wrote:
LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.
I tried to give him 5 vote but it did't work. when I click on button to vote, it goes to continuous process then I have to refresh page again. I am not able to vote any post here.
please don't forget to vote on the post that helped you.