I am trying to dynamically create a form in asp.net and render it out on the page by performing a text replace on the rendered controls.
(I cannot just push the controls to a panel on the page, that would be the ideal situation and I wouldn't be having this problem right now)
I create a label, textbox and a button and add them all to a panel. This panel is then rendered into a string using a TextWriter, which I then to use perform my replace on my copy.
Label lbl = new Label();
lbl.Text = "Enter Amount";
lbl.Attributes.Add("style", "width:25%; vertical-align:middle;");
lbl.CssClass = "donate-label";
lbl.ID = "lblAmount";
TextBox tb = new TextBox();
tb.ID = "tbAmount";
tb.Attributes.Add("style", "padding-left:25px; width:50%;");
lbl.AssociatedControlID = tb.ID;
Button b = new Button();
b.ID = "btnDonate";
b.Text = "Make a Donation";
b.CssClass="block-btn right-arrow red-bg right";
b.Click += new EventHandler(btnDonate_Click);
b.Attributes.Add("style", "display:table-cell; margin-top:0; width:40% !important;");
Panel pnlForm = new Panel();
pnlForm.CssClass="form clearfix donate-form";
pnlForm.Attributes.Add("style", "width:70%");
pnlForm.Controls.Add(lbl);
pnlForm.Controls.Add(tb);
pnlForm.Controls.Add(b);
Now if I was to add the above panel to a panel that already existed on the page, it would work perfectly and as expected, but the control rendering is causing it to break somewhere..
TextWriter myTextWriter = new StringWriter();
HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter);
pnlForm.RenderControl(myWriter);
string body = p.Copy.Replace("##donate##", myTextWriter.ToString());
The error I get is as follows:
Unable to find control with id 'tbAmount' that is associated with the Label 'lblAmount'.
Line 146: pnlForm.RenderControl(myWriter);
If I removed the assoicatedControlID for my label it works fine, but unfortunately it renders my label as a span which isn't what I need, I need it rendered as a label with a 'for' attribute.
The easiest way is to use literal with html tags inside it
Literal lbl = new Literal();
lbl.Text = "<Label For='tbAmount' style='width:25%; vertical-align:middle;' class='donate-label' > Enter Amount </label>";
lbl.ID = "lblAmount";
by the way make the tbAmount id static so you dont get any unpredictable id for your textbox
tb.ClientIDMode= System.Web.UI.ClientIDMode.Static
Related
I am adding a parent asp.net label to my page in the code-behind like this:
Label lbl = new Label();
lbl.ID = "lblPrimary";
lbl.Text = "Testing";
placeholder.Controls.Add(lbl);
I need the end output to look as if I did the following in the aspx:
<asp:Label ID="lblPrimary" runat="server" Text="Testing">
<asp:Label runat="server" SkinID="Required"></asp:Label>
</asp:Label>
How can I add the required label in between from the code-behind like above?
You can try the following code. The Text property of the outer Label is set with a Literal control, otherwise the text is overwritten when the inner Label is added to the Controls collection. If you want the exact result specified in your question, you can forget the Literal control but "Testing" will not be displayed in the outer Label.
Literal literalInner = new Literal();
literalInner.Text = "Testing";
Label lblInner = new Label();
lblInner.Attributes.Add("SkinID", "Required");
Label lblOuter = new Label();
lblOuter.ID = "lblPrimary";
lblOuter.Controls.Add(literalInner);
lblOuter.Controls.Add(lblInner);
placeholder.Controls.Add(lblOuter);
I want to add span & inside that span I want to add control.
Both have to be added dynamically using code in c#.
I know how to add span but how to add control inside that span ?
Code to add span & control are
var a = new HtmlGenericControl("span");
a.InnerHtml = "Something";
a.Attributes["class"] = "validation-asterix";
pn.Controls.Add(a);
RequiredFieldValidator rfv = new RequiredFieldValidator {
ControlToValidate = "txt" + txField.ColumnName,
Display = ValidatorDisplay.Dynamic,
Text = "*",
ID = "val" + txField.ColumnName,
Visible = true,
};
pn.Controls.Add(rfv);
I want to add above span which will have this requiredfieldvalidator dynamically.
What I will do is to create a void ContentPlaceHolder in the markup code, in the place that you want to show the validation error message.
Then, from codebehind, you simply can add the validator as:
RequiredFieldValidator rfv = new RequiredFieldValidator {
ID = "val" + txField.ColumnName,
ControlToValidate = "txt" + txField.ColumnName,
Display = ValidatorDisplay.Dynamic,
Text = "*",
CssClass = "validation-asterix"
};
ContentPlaceHolderID.Controls.Add(rfv);
Not need to use any span here.
I'm trying to create a set of tabs at runtime and they were, for a time, working correctly- however now they just seem to display the contents of all tabs underneath the previously rendered tab. As silly as it seems, as far as I can tell, in terms of code nothing has changed since the code was working so I am a little confused as to what is going on...
For the purpose of this example I will not use a loop, although this seems irrespective as the loop doesn't appear to be the problem.
<asp:Panel runat="server" ID="pnlTabs">
<ul runat="server" id="ulSections" />
</asp:Panel>
I've come to the conclusion that my code must be at fault so here is the C#:
HtmlGenericControl liTab = new HtmlGenericControl("li");
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = Tab1;
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);
var pnl = new Panel();
pnl.ID = Tab1;
pnlTabs.Controls.Add(pnl);
I place my controls and what not on the "pnl". Can anyone please tell me what my mistake is?
//THIS ADDS THE <A> to the <LI>
liTab.Controls.Add(anTab);
//THIS ADDS THE <LI> to the <UL>
ulSections.Controls.Add(liTab);
//THIS CODE Appear to have nothing to do with anything.
var pnl = new Panel();
pnl.ID = Tab1;
pnlTabs.Controls.Add(pnl);
Can you confirm if the HTML is even being rendered in the html when you View Source.
Where in your code behind is this code... in a button click, on Page Load, On Init ?
I discovered what the problem was, it was basically that the InnerText of "anTab" cannot be the same as the href attribute of "anTab" as this causes confusion and results in the content from all tabs to appear only in the first (selected) tab, while the remaining tabs don't function.
For example, the following code works correctly as the InnerText of "anTab" is "Tab 1" and the href is looking for a control with the ID "Tab1", which is the ID of the panel.
HtmlGenericControl liTab = new HtmlGenericControl("li");
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = "Tab 1";
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);
var pnl = new Panel();
pnl.ID = "Tab1";
pnlTabs.Controls.Add(pnl);
Below we can see that the InnerText of "anTab" is the same as the href and the ID of the panel we wish to use as the tab, this causes the confusion as the href now assumes you want the "tab click" to point to itself instead of the actual panel.
HtmlGenericControl liTab = new HtmlGenericControl("li");
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = "Tab1";
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);
var pnl = new Panel();
pnl.ID = "Tab1";
pnlTabs.Controls.Add(pnl);
I am creating a simple application that keeps track of coins. I have a button that creates a new tab in a tabcontrol container. I am wanting to add some textboxes to the newly added tabs. The below code adds a textbox to my main tab called "Control". I have tried playing around with that field, but it always adds it to the main page called control. How would I do this? I have the following code:
string name = txtName.Text;
//validate information
try { }
catch { }
//create new tab
string title = name;
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
//Add text boxes
TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(250, 75);
tb.Name = "TextBoxName";
tb.Size = new System.Drawing.Size(184, 20);
Control.Controls.Add(tb);
//put data inside of textboxes
tb.Text = txtCoin.Text;
myTabPage.Controls.Add(tb);
You need to add the text box to the controls collection of the new tab:
//Control.Controls.Add(tb); //Replace this line
myTabPage.Controls.Add(tb);
change
Control.Controls.Add(tb)
to
tabControl1.TabPages.Last().Controls.Add(tb)
tabControl1.TabPages.OfType<TabPage>().Last().Controls.Add(tb)
I have three Tabs in my Windows Forms form. Depending on the selected RadioButton in the TabPages[0], I added few dynamic controls on the relevant TabPage. On the Button_Click event the controls are added, but the problem is I'm not able to remove the dynamically added controls from the other (irrelevant) TabPage.
Here's my code:
Label label235 = new Label();
TextBox tbMax = new TextBox();
label235.Name = "label235";
tbMax.Name = "txtBoxNoiseMax";
label235.Text = "Noise";
tbMax.ReadOnly = true;
label235.ForeColor = System.Drawing.Color.Blue;
tbMax.BackColor = System.Drawing.Color.White;
label235.Size = new Size(74, 13);
tbMax.Size = new Size(85, 20);
if (radioButton1.Checked)
{
label235.Location = new Point(8, 476);
tbMax.Location = new Point(138, 473);
tabControl.TabPages[1].Controls.Add(label235);
tabControl.TabPages[1].Controls.Add(tbMax);
tabControl.TabPages[2].Controls.RemoveByKey("label235");
tabControl.TabPages[2].Controls.RemoveByKey("tbMax");
}
else
{
label235.Location = new Point(8, 538);
tbMax.Location = new Point(138, 535);
tabControl.TabPages[1].Controls.RemoveByKey("label235");
tabControl.TabPages[1].Controls.RemoveByKey("tbMax");
tabControl.TabPages[2].Controls.Add(label235);
tabControl.TabPages[2].Controls.Add(tbMax);
}
Where am I making that mistake?
First of all, tbMax's name is not "tbMax", but "txtBoxNoiseMax". So for one, it won't be able to find the TextBox on RemoveByKey.
You're making new controls each time.
As lc already mentioned:
You named your TextBox variable tbMax, but you gave it the name txtBoxNoiseMax. If you take a look into the description of RemoveByKey, you'll see it works on the Name property. So you should change
tbMax.Name = "txtBoxNoiseMax";
into
tbMax.Name = "tbMax";