Ok, I want to access div control by id witch is changed dynamically from C# code. Maybe it will be clearer with code:
string divHtml = "";
for (int j = 1; j < 4; j++)
{
string ph = "placeHolder" + j;
Control phd = (Control)FindControl(ph);
StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
phd.RenderControl(w);
divHtml = divHtml + sw.GetStringBuilder().ToString();
}
and ascx part is:
<div runat="server" id="placeHolder1" class="placeholder" >
</div>
It pass compile time but phd is at null value, not to the value of control. It works fine if i access control directly like this:
StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
placeHolder1.RenderControl(w);
divHtml = divHtml + sw.GetStringBuilder().ToString();
Thank you in advance....
FindControl finds only controls in the first level, so if your control is located down in the control tree you need to write your own nested FindControl to make it work.
Have look at ASP.NET Is there a better way to find controls that are within other controls? for more info.
Or if you have the parent (page) it should work to call page.FindControl("placeHolder1") on that control directly.
I figured that it is problem with nesting, thanks to #bang. But since I use master page to display my content, this is solution that is best for me:
for (int j = 1; j < 4; j++)
{
string ph = "placeHolder" + j;
ContentPlaceHolder cph = this.Master.FindControl("MainContent") as ContentPlaceHolder;
Control ctrlPh = cph.FindControl(ph);
StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
ctrlPh.RenderControl(w);
divHtml = divHtml + sw.GetStringBuilder().ToString();
}
Thank you #bang, you pushed me in right direction.
Related
I am using Gembox.Documents to insert an HTML file into a Word or PDF document.
Unfortunately, in the resulting Word (or pdf), the height of the contents of the rows (cells) in the table is too high and does not correspond to the original one in the HTML file and I cannot change this with the help of CSS or HTML.
Can you, please, suggest solutions to the problem?
string fileName="zzzz";
var destinationDocument = new DocumentModel();
var section = new Section(destinationDocument);
destinationDocument.Sections.Add(section);
var srcDocument = DocumentModel.Load(TempPath + fileName + ".html");
var pageSetup = srcDocument.Sections[0].PageSetup;
var destpagesPageSetup = destinationDocument.Sections[0].PageSetup;
destpagesPageSetup.Orientation = Orientation.Landscape;
destpagesPageSetup.PageWidth = 1000;
destpagesPageSetup.PageHeight = 1000;
destpagesPageSetup.RightToLeft = true;
destpagesPageSetup.PageMargins.Left = 20;
destpagesPageSetup.PageMargins.Right = 0;
destpagesPageSetup.PageMargins.Bottom = 0;
destpagesPageSetup.PageMargins.Top = 0;
destpagesPageSetup.PageMargins.Gutter = 0;
destpagesPageSetup.PageMargins.Footer = 0;
var mapping = new ImportMapping(srcDocument, destinationDocument, false);
var blocks = srcDocument.Sections[0].Blocks;
foreach (Block b in blocks)
{
//b.ParentCollection.TableFormat.DefaultCellSpacing = 1;
Block b1 = destinationDocument.Import(b, true, mapping);
section.Blocks.Add(b1);
}
var pageSetup1 = section.PageSetup;
destinationDocument.Save(TempPath + fileName + ".pdf");
thanks
This issue occurred because of the cell margins appearing from the HTML content.
After investigating that HTML, the issue was resolved, the fix is available in the current latest bugfix version:
https://www.gemboxsoftware.com/document/downloads/bugfixes.html
Or in the current latest NuGet package:
https://www.nuget.org/packages/GemBox.Document/
I created a dynamic link button, which needs to be inserted in a StringBuilder to not ruin the design of my aspx page.
So here's a "part" of my code where I need to insert my LinkButton:
design.Append("<h3>");
NewAddToCart(); //This is where my linkbutton should be inserted
design.Append("</h3></div>");
My NewAddToCart() is constructed on the following code:
private void NewAddToCart()
{
LinkButton lbtnAddtoCart = new LinkButton();
lbtnAddtoCart.ID = "lbtnCart" + i;
lbtnAddtoCart.CommandArgument = i.ToString();
lbtnAddtoCart.CssClass = "glyphicon glyphicon-shopping-cart pull-right";
lbtnAddtoCart.Click+=lbtnAddtoCart_Click;
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter html = new HtmlTextWriter(sw))
{
lbtnAddtoCart.RenderControl(html);
}
}
}
I have some settings stored in web.config like this:
<add key="Answers" value="radiobutton1,radiobutton2,radiobutton3"/>
Radiobutton1, radiobutton2 and radiobutton3 are radiobutton label values.
In settings.cs I have a function to retrieve value from web.config:
public static string Answers
{
get
{
return System.Configuration.ConfigurationManager.AppSettings["Answers"];
}
}
.ascx file:
<table runat="server" OnPreRender="Radio_PreRender" id="table1" name="table1">
</table>
My ascx.cs file contains this function:
protected void Radio_PreRender(object sender, EventArgs e)
{
if (Settings.Answers != "")
{
int counter = 0;
string a = Settings.Answers;
string[] words = a.Split(',');
StringWriter stringwriter = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(stringwriter);
foreach (string word in words)
{
writer.WriteBeginTag("tr");
writer.WriteBeginTag("td");
writer.Write("abc123");
RadioButton rdb1 = new RadioButton();
rdb1.Checked = true;
rdb1.GroupName = "rdbgroup";
rdb1.ID = "radiobutton" + counter;
rdb1.Text = word;
table1.Controls.Add(rdb1 );
writer.WriteEndTag("td");
writer.WriteBeginTag("tr");
table1.Render(writer);
counter++;
}
}
}
In other words, I want to generate a dynamic number of this code inside table1:
<tr>
<td>
// input type="radiobutton" and label go here.
</td>
</tr>
At the moment radiobuttons are not generated, because they can't be direct child elements to a table. If I specify a div instead, radiobuttons are generated, but everything I try to write with HtmlTextWriter is not. I understand that my html has to be rendered by using table1.Render(writer); or something similar, but I can't figure it out.
You can try creating a table and add it to the page you are working on, using the exmaple below you can replace the textbox with a radiobutton
Here is an example:
//Creat the Table and Add it to the Page
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
// Now iterate through the table and add your controls
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
// Set a unique ID for each TextBox added
tb.ID = "TextBoxRow_" + i + "Col_" + j;
// Add the control to the TableCell
cell.Controls.Add(tb);
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
// Add the TableRow to the Table
table.Rows.Add(row);
}
I do this quite often by running the user control through its full asp.net lifetime and getting it as a string:
http://www.diaryofaninja.com/blog/2009/09/14/a-simple-solution-to-viewing-a-preview-of-any-page-in-your-site
This way you can use ASP.Net as a templating engine for anything
Page tempPage = new Page();
UserControl myUserControl = new MyUserControl();
tempPage.Controls.Add(myUserControl);
StringWriter sw = new StringWriter();
HttpContext.Current.Server.Execute(tempPage, sw, false);
if (!String.IsNullOrEmpty(sw.ToString()))
{
return sw.ToString();
}
I'm just trying to get the equivalent HTML code that represent a specific control in asp.
for example i have the following label in ASP
Label x=new Label();
x.ID="a123";
x.Text="b123";
i just want to find a way to get
"<span id='a123'>b123</span>"
You can use this method to render controls to html.
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();
}
And use
Label x = new Label();
x.ID = "a123";
x.Text = "b123";
var html = RenderControl(x);
will give you <span id="a123">b123</span>
How can I get a server-side control's tag? I'm guessing it's something like below:
TextBox textBox=new TextBox();
GetTag(TextBox textBox)
{
...
}
And the result is something like <asp:TextBox /> (the control's design time tag).
When I change the control to CheckBox then the result should be something like <asp:CheckBox />.
There are no methods that can return these tags for you but constructing a dictionary your self should be easy enough.
Dictionary<Type, string> d = new Dictionary<Type, string>();
d.Add(typeof(TextBox), #"<\asp:TextBox />");
TextBox t = new TextBox();
string tag= (d[t.GetType()]);
And so on...
You can use RenderControl method
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
using (HtmlTextWriter textWriter = new HtmlTextWriter(sw))
{
textBox.RenderControl(textWriter);
}
}
sb will have textBox's html content.
Edit:
You can get aspx content and find element tag. like this:
String path = Server.MapPath("~/[aspx path]");
string content = File.ReadAllText(path);
string controlID = "textBox";
int startIndex = content.IndexOf("<asp:TextBox ID=\"" + controlID + "\"");
bool foundEndTag = false;
string controlTag = "";
int i = startIndex;
while (!foundEndTag)
{
if (content[i] == '>')
foundEndTag = true;
controlTag += content[i];
i++;
}