Add an Html Control in multiple parents in asp.net - asp.net

I'm creating a new unordered list as follows:
Using htmlUL As New HtmlGenericControl("ul")
End Using
Before closing the 'Using' clause, the list has to be added in a parent control:
Using htmlUL As New HtmlGenericControl("ul")
'Some Code Here
parent1.Controls.Add(htmlUl)
End Using
Everything works fine until i add my control to more parents :
Using htmlUL As New HtmlGenericControl("ul")
'Some Code Here
parent1.Controls.Add(htmlUl)
parent2.Controls.Add(htmlUl) '<--- Only gets added in the last parent's collection
End Using
This behaviour is expected because every control is unique. But i need to add this list to more than one parents (it does not include any static ids in order to avoid conflicts)

ASP.NET does not support scenario like the very same control object used in several places. The reason here I believe is the problem with identification of the control later in the page life cycle - this control would have to render under different parents, and with different IDs, etc. Looks like a very complicated and dangerous use case.
However there are several workarounds written here and there to clone controls on the page. For example checkout these threads: Copy/Clone Control, A Custom Copy Control - Copies Any ASP.NET Control.
All that beign said, in your case the easiest thing is just to create new list for each parent control. You do not need to copy any specific properties or data whatsoever, so no need for cloning overhead done in the solutions above.

Related

Enterprise Architect how to exclude elements on report

I want to generate report for my diagram, I've created a simple template:
I want to have only activity task elements listed in my report table, without events, pools and other fields.
I have tried to set elements as non-printable, I was using "Hide annonymous elements" flag, and changed this field in element details, and also I was using Exclude filters. I don't know what is going on but at the end my generated report contains either all elements or none. Is there any magic flag or something? No matter what I change, at the end it does'nt matter, because report is always the same....
You can set a filter in the properties of your template to include only Activity elements
This definitely works as I use that all the time, so if something goes wrong with this there must be something else playing.

Is there a better way to access deeply nested controls?

I've started work at a company and don't have full access to their previously built objects/frameworks/etc and I've been tasked with creating a new system, using their libraries / objects / etc. I can override most of their libraries, but without seeing what exactly is going on I'm finding it challenging to work with.
It is all in .net webforms, which I'm really not all that familiar with as well.
They have a customs Model-View-Controller sort of system built on top of webforms, which generates full control ascx pages, however of course the ascx file is simply one line, the code behind is empty, so all I can really see is the markup dynamically generated.
I will have markup written like:
<div id="id1">
<div id="id2">
<div id="id3">
<table id="tt_id1">
<input id="myModelNameID">
etc, etc.
The objects as well are named differently in .net vs what's generated in the markup, so this makes things even more challenging - I have to always examine the objects for a while to find the full path of where my control is located.
What is the easiest way to access a deeply nested control?
I've tried to use references to different ID's (like ClientID vs ID), etc. I have found examples here and there but usually people are doing something like what I'm doing.
I'm trying to avoid writing 30 findControls , etc. I could write a method I suppose that would do this, but I'm hoping there's an easier way even or something already available.
When I am trying to access anything in code-behind to access objects I am examining them in the debugger or using a dumper class or reflection, and then writing something like this:
//This is an example that would add a button in a table.
Button MyButt = new Button();
MyButt.Text = "Click Me";
//Split into sections for readability...
Control c = Page.FindControl("ctl00").FindControl("Form1");
Control c2 = c.FindControl("MainPage").FindControl("id1");
Control MyTable = c2.FindControl("id2").FindControl("myTable1");
MyTable.Controls.Add(MyButt);

VS2008/ASP.NET 3.5 - How to create dynamic webforms

The question in a nutshell: Is there a way to add forms dynamically to a aspx-page? I know how to add controls to an existing form, but how to add a whole form?
Bckground:
I am "forced" to work in Visual Studio 2008 and I want to create a controller, which builds a page depending from the list of elements a model gives it (e.g.: the list may contain a paragraph, an image, another paragraph, a parapgraph again, a form and so on).
This works fine for the first examples as I am able to add them to the inner-html of a div-container.
Thinking about ways to generate a form like this (innerHTML += form), I feel I'd be throwing the few possible advantages ASP I can see (compared to PHP) out of the window in terms of input validation and so on. But I can't find a way to generate a "real, server-run" form. The same goes for gridviews, but I guess the solution may be similar.
A possible workaround would be to create an empty form
<form runat="server" id="dummyForm">...
and add controls to it dynamically. The obvious(?) downside to this would be, that I couldn't change its position (at least I wouldn't know how) in relation to the other content elements.
Another downside would be that I would be limited to one form per page and that may or may not be sufficient. If it wasn't I would starting to add several empty dummy-forms and would start indexing them and all of that doesn't look very cool to me.
Is there a more elegant way to a solution?
TIA
Simon
You can't add more than once server side Form tag in single aspx file
(in run time or design time)
maybe this article help you to generate dynamic forms :
https://msdn.microsoft.com/en-us/library/aa479330.aspx

Cloning usercontrol using Jquery

Page contains four grid with same no of columns.so, i have created a usercontrol which i am dragging four times on my page so it has created. uc1:grid1,uc1:grid2,uc1:grid3,uc1:grid4.
This means after rendering it will fetch four grids from the server. So If i create a grid & using Jquery .clone() i.e $(grid).clone() If i create 3 clones I can reduce server overhead. Is there any problem using this method?
If after cloning you need other data manipulation like changing headers and CSS and stuff like that, then sticking to your server code is more reasonable. But if you have 4 identical grids, int both structure and data, and they all have the same styles, then choose clone() method.

How can you move ASP.Net controls to different places on the Web form at runtime?

Is there an accepted way to "move" a control.
My client wants to place a certain chunk of markup (representing some visual element) in one of several different places on the page. The locations are different to the point that I can't effect the change on CSS along (by floating it or something).
I considered just putting the control in multiple spots with Visible set to "false," then displaying the one in the place they wanted for that particular page.
However, the code for this control is not trivial -- there's a couple template sections, for instance. Having to dupe this in multiple places would get unwieldy. Also, I don't want to have to work with this control strictly from the code-behind for the same reason.
So, I'd like to put it in one place on the Web form, the move it around based on where I want it. Could I put Placeholders in different spots, have the control in one spot, then remove and add it to the right spot? I suspect this would work.
Does someone have a better idea? Is there a best practice for this?
I'd recommend using a placeholder control, moving your markup into a separate user control, then loading this at runtime and adding it to the relevant placeholder.
Eg.
// Load a user control
MyControl userCtrl = (MyControl) LoadControl("~/Controls/MyControl.ascx");
// Or create an instance of your control
SubclassedControl subclassedCtrl = new SubclassedControl();
// Do stuff with controls here
userCtrl.LoadData();
subclassedCtrl.Text = "Hello World";
// Check which placeholder to add controls to
PlaceHolder placeHolder = (foo=="bar") ? placeHolder1 : placeHolder2;
// Add the controls
placeHolder.Controls.Add(userCtrl);
placeHolder.Controls.Add(subclassedCtrl);
This will avoid cluttering up your page with unnecessary markup, and loading it at runtime will also avoid unnecessary confusion later, when another developer looks at the code and can't immediately see why a control is in one place in the markup, but renders on a completely different part of the page.
An alternative (and one I've seen done many times before) is through javascript and the DOM. Render your control inside a hidden div tag. So you would render your content here:
<div id='rendercontent' style='display:none'>
.. control here ..
</div>
Then, lets say you wanted to move it all here (the span tag is inside because that's what we're going to replace):
<div id='newlocation1'><span></span></div>
You would define the following javascript:
<script language="JavaScript">
function replaceNode(newElementID, targetElementID)
{
var targetElement=document.getElementById(targetElementID);
var newElement=document.getElementById(newElementID);
targetElement.replaceChild(newElement, targetElement.firstChild);
}
</script>
And when you want to move the content to the new location, call:
<script language="JavaScript">
replaceNode('rendercontent','newlocation1');
</script>
Do Web Parts do what you want to do?
Or, you can change the parent programmatically of your controls to move them into a separate area.
You can override the Render method and place the controls wherever you want in the html.
You only need to add controls to the Controls collection that must interact on the server. The rest of your HTML can just be written to the response stream. If you override Render you can create the html anyway you see fit, placing the controls in any order.
Below is an example of how to write out your html.
protected override void Render(HtmlTextWriter writer)
{
AddAttributesToRender(writer);
writer.RenderBeginTag(TagKey);
writer.RenderBeginTag(HtmlTextWriterTag.Div);
_control.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
}
You could always put panels in the pre-defined locations and add the control to the specific panel at runtime.. Here's an example adding a label (the label could be replaced with any control).
Dim lblDisplay As Label = New Label()
lblDisplay.ID = "myLabel"
lblDisplay.Text = "Some Text"
pnlDisplay.Controls.Add(lblDisplay)
As far as...
"Also, I don't want to have to work
with this control strictly from the
code-behind for the same reason."
I think you're going to have to do most of your work in the code behind.
PS.. a good example of the whole usercontrol setup can be downloaded here..
http://www.asp.net/downloads/starter-kits/time-tracker/

Resources