I want to create a DIV dynamically and place it into another DIV when a button is clicked.
So far I've managed to create a new DIV on button_click, however I don't know how to place the new DIV object into a specific DIV tag as a child.
Here's my code which is within the button's click event.
System.Web.UI.HtmlControls.HtmlGenericControl dynDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
dynDiv.ID = "dynDivCode" + i.ToString();
dynDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Gray");
dynDiv.Style.Add(HtmlTextWriterStyle.Height, "20px");
dynDiv.Style.Add(HtmlTextWriterStyle.Width, "300px");
dynDiv.InnerHtml = "I was created using Code Behind";
this.Controls.Add(dynDiv);
Any suggestions please?
thanks
Your specific existing div should be server control. For example: Panel. Then you can add as a child to the Controls collection of the panel.
Related
I am using a usercontrol to display label text on the left side of a page and another usercontrol to display the content page, when the text of a field on the content page changes, I need to update the same status on a label on the menu usercontrol on the left.
I have used:
ScriptManager.RegisterClientScriptBlock(this, this.GetType(),"ChangeLabelText", script, false)
to change the text on the left control, the javascript associated with this gets executed but the label still shows the old text even when the text on the content page is updated after page refresh. However, it shows the new updated text when the whole page refreshes.
Please suggest an approach for resolving this issue.
If you have two UserControls on a page, and want to update a Label in the second Control from code behind of the first, you'll need to use FindControl.
//find the other control in the parent
Control control = Parent.FindControl("WebUserControl2") as Control;
//find the label in that control
Label label = control.FindControl("Label1") as Label;
//update the label
label.Text = "Updated from another control";
Update
If you want to update with javascript you just have to make sure you send the right ID.
ScriptManager.RegisterStartupScript(Page, GetType(), "changeLabel", "changeLabel('WebUserControl2_Label1', 'Updated with JS')", true);
And the javscript function
<script type="text/javascript">
function changeLabel(id, text) {
document.getElementById(id).innerHTML = text;
}
</script>
I programatically create li elements, however I need some of them disabled, so far I have:
HtmlGenericControl htmlLi = new HtmlGenericControl("li");
htmlLi.InnerText = row["name"].ToString();
tab_content.FindControl("tab_content_" + row["stars"].ToString()).Controls.Add(htmlLi);
Is it possible to disable them? If not, is their any alternatives?
An <li> element can't be disabled, as there's no input to prevent.
You can however hide the elements you don't want to be displayed.
You can either hide them from the code-behind:
htmlLi.Visible = false;
or you can add some styling to it so that it is put into the DOM, but made invisible by the CSS (so that you can make it visible again with javascript, should you want to):
htmlLi.Attributes.Add("style", "display: none");
or
htmlLi.Attributes.Add("class", "SomeInvisibleCssClass");
I am adding a literal control to my web page using the following lines of code:
LiteralControl myObject = new LiteralControl();
myObject.Text =
#"<OBJECT ID='MediaPlayer' WIDTH='640' HEIGHT='480' CLASSID='clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921' STANDBY='Loading Windows Media Player components...' TYPE='application/x-vlc-plugin'>
<PARAM NAME='FileName' VALUE='" + "file:///" + path + "'> ... </OBJECT>';";
Page.Controls.Add(myObject);
but this is getting added at the bottom of the page while I want it in the middle,How can I specify the location where I need to place this control?
From experience, I would place the Literal declaration on your page (so you can choose its exact location), then set the control's Text property via code. If you do not enter any text, the element will be empty and will not render anything on the page.
On your aspx page, in the place where you want it:
<asp:Literal ID="myLiteral" runat="server" />
In your code:
myLiteral.Text = "...";
If you don't want anything to be visible, just ignore it. The control won't hinder the rest of the application's flow.
Beside #Flater answer that is better to follow if you only have some text to add, one way to specify the position of the added controls is to add a PlaceHolder control.
<asp:PlaceHolder runat="server" id="phPlaceOnMe" />
and on code behind you add your controls as:
phPlaceOnMe.Controls.Add(myObject);
Suppose if controlA is the control before which I want to place my literal control myObject
then,
Control parent = controlA.Parent;
parent.Controls.AddAt(parent.Controls.IndexOf(controlA), myObject);
I found this on stackoverflow itself and it worked for me.
I want to load the user controls dynamically on my page. I can load the control dynamically with the help of following code:
UserControl ctrl =(UserControl) Page.LoadControl(ControlPath);
dvUserControls.Controls.Clear();
dvUserControls.Controls.Add(ctrl);
dvUserControls is just a div with runat = "server"
My problem is that I have to assign the values to the public properties of the controls also .I can't register my control to the aspx page.
Please advise.
Thanks
Rohit
it the type of usercontrol is not available(you say which is your case) you can (and have to) use reflection
This is how is it working with me:
MyListControl myListControl = (MyListControl)page.LoadControl("~/Controls/MyListControl.ascx");
myListControl.SourceId = 1;
//Further Processing
on top of my page, i have:
using MyProject.UI.Controls;
I have some textbox and submit button.
When ever I click the submit button the div should be added dynamically in my aspx page.
Also the information which is available in the text box also displayed in the page default.
Use:Without using database i need to display the user suggestion in my page...
You have 2 possibilities:
You can add dynamically an asp.net panel which generates div tag.
// Create dynamic controls here.
// Use "using System.Web.UI.WebControls;"
Panel panel1 = new Panel();
panel1.ID = "MyPanel";
Form1.Controls.Add(panel1);
Create the div using jQuery
Select the parent element with
$("#id"), $("<element>") or $(".class") and then $(theElement).append("<div>Your content</div>");