How to create the div, dynamically in asp.net - c#

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>");

Related

Update text of a control in another user-control when the text of a label in another user-control changes

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>

Load User Control dynamically in C# and assign the Public properties of that control

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;

Create div tag dynamically

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.

Problem with ScriptManager when trying to email rendered contents of ASP.NET page

I recently added a Telerik control to an ascx that is included in an aspx page. This page has a "Send email" button, which when clicked will email the user the rendered output of the page. The Telerik control I added requires a ScriptManager, so I added that to the ascx. However, now the email button won't work. I get the following error:
The control with ID 'myIdHere' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.
I know the script manager exists because the page works fine when I go that url, it is only failing when it tries to email the rendered output.
Here's a code snippet, any ideas as to whether there is a problem with scriptmanager when doing this sort of thing?
Page EmailPage = new EmailBasePage();
System.Web.UI.HtmlControls.HtmlForm EmailForm = new System.Web.UI.HtmlControls.HtmlForm();
EmailPage.Controls.Add(EmailForm);
EmailForm.Controls.Add(contentTable); //this is the container with all the controls I want to email
StringBuilder SB = new StringBuilder();
StringWriter html = new StringWriter(SB);
HtmlTextWriter mhtmlWriter = new HtmlTextWriter(html);
EmailPage.DesignerInitialize();
EmailPage.RenderControl(mhtmlWriter);
mhtmlWriter.Close();
Update
The control "myIdHere" referenced in the error message is the Telerik RadComboBox control that requires a ScriptManager. It is a child control of the contentTable control that is added to EmailForm. The control hierarchy is like this:
contentTable
|__UserControl1
|__ScriptManager1
|__UserControl2
|__Control "myIdHere", a Telerik RadComboBox requiring a ScriptManager
If you are sure that the page output contains the ASP.NET AJAX scripts (rendered by the ScriptManager control) then you can remove the server exception by setting
myIdHere.RegisterWithScriptManager = false;
This will remove the combobox dependency on a script manager control on the server. Note that the control will still need the ASP.NET AJAX client scripts in the browser, which is why you need to make sure that there is a ScriptManager on the page in the first place.

Masterpage + updatepanel on child page

So I have a masterpage with a login that is in an update panel. I have a child page that has a literal control that should update when the login updates. What it doesn't do is reload the method I use to generate the content for that literal when it posts back. I tried to call the method on the child page from the master page once you click log in, but I get an error that the literal control cannot be found (because it exists on the child page not the master page). How would I reference that control in the masterpage to pass it to my method?
The article below shows how the control tree works with MasterPages and how to reference different controls at different levels of the control tree.
ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
So the scenario is that you have an update panel on the child page that when triggered doesn't update/refresh your lets say, label which is in your header on your master page.
What you do is, in the code behind of your master page create a function that changes the value of the label.
Include an update panel on the master page for the label, which is triggered by the textchanged event etc.
Now, in your child page code behind or your let say, button click event, call upon the function that exists in the master page and send the needed value in the parenthesis.
C#:
((MyMaster)this.Page.Master).ShowMessage(text);
VB.NET:
DirectCast(Me.Page.Master, MyMaster).ShowMessage(text)
This should update the label with the correct value whilst triggering the update panel on the master page and thus refreshing your label as well.
I'm about to try this now for myself, wish me luck. :D

Categories