How to construct a control like the Asp.Net ScriptManager - asp.net

Maybe this is not the best title to it, but this is what I am trying to find out. I need a way to construct a control in ASP.Net that mimics the ScriptManager in that it should look like this:
<uc:MyControl Id="" ...>
<MyListOfItems>
<uc:Item someProperty="value1" someOtherProperty="value2" />
<uc:Item someProperty="value3" someOtherProperty="value4" />
</MyListOfItems>
</uc:MyControl>
How to make the Control have a List of items that you can add items to from the Source of a page.
Thanks in advance for your help.

Basically you need to instruct your control to parse any inner content as subcontrols. This is done using a couple attributes - ParseChildrenAttribute and PersistChildrenAttribute (if you want child controls to be maintained in ViewState).
Create a custom control that inherits from WebControl.
Add a [ParseChildren(true, "MyListOfItems")] attribute to your class.
Create a MyListOfItems property.

Related

Dynamically change ASP.Net WebForms Control IDs While Maintaining State

We're working with a third party system and we need to modify some ASP.Net controls so that they have a specific token added to their HTML id attribute (so the third party system can identify them from the request), as well as HTML comments before and after the controls' output.
Basically, we need to be able to take say a TextBox (say for "Employee First Name") and dynamically change its output to something like this:
<!-- FIZZBOT_START -->
<input id="EmployeeFirstName_FIZZBOT_" ... />
<!-- FIZZBOT_END -->
We would ideally like to implement this so that the developers only have to do something like this:
<xxx:FizzbotWrapper ID="MyWrapper" runat="server">
<asp:TextBox ID="EmployeeFirstName" runat="server" />
</xxx:FizzbotWrapper>
Is there any way to get such an interface to produce such output, while still being able to:
maintain control state across requests
work properly with Validators
work properly with UpdatePanels
allow developers to refer to the original ID in the codebehind for readability (instead of having to refer to "EmployeeFirstName_FIZZBOT_"
work with out-of-the box ASP.Net controls like TextBox and not just controls we've derived.
I've tried a few ways of doing this (the technique that was the most promising was this, but I ran into some trouble getting it to work with validators, and it wouldn't work at all with controls we don't have the source code to).

Custom ASP binding mechanism problem

know ASP since about 6 months, and I've go hard problem. I've created my own DataBinding mechanism and I need some solution to do somthing like this:
I have this example code in my ASPX
<myButton id="someID" runat="server" Text="SomeText" BackColor="{SomeContext}" />
and I have in this control my property Context of type Object. I want to bind property which is inside Context to BackColor property. Name of the property from Context is SomeContext (in brackets). All I need is to use some TypeConverter or other technology to identify that there is a name inside {} and remember that name inside instance of myButton control. Any ideas???
I thought that I can use my own class inherited from TypeConverter and catch the moment of converting value {SomeContext} to Color (the BackColor property). I can catch this moment, but I have no info about target control, only empty context of String value. If anyone know how to get target property somehow, will be very!! helpful.
I've been searching web, and nothing...
You can set this in your code behind directly:
someID.BackColor = context.Color;
Ya might give ASP.NET Custom Expression builders a try.
I won't use codebehind in my project. Website will be rendered from compiled customized aspx from database. I have special approach for my system, and my binding is a main part of it. Regular databinding expression won't help because it use ASP binding, which I don't want to use.
Unfortunately I didn't find any solution of my problem, so I decided to use inner properties. I'll use ASPX code like this:
<myButton runat="server" ID="someID">
<Binding>
<Bind Target="BackColor" Source="ColorOfBackgroundFromContext" />
<Bind Target="ForColor" Source="ColorOfForegroundFromContext" />
</Binding>
</myButton>

Nested ASP.Net controls not defined

I have a couple of controls that are set to runat="server", but are showing up as "not declared" in the vb code behind. They are not being setup in the designer.vb file at all, even if the design.vb is re-created.
The only thing that I can think might be causing this is that the controls are inside of a custom control. The code looks something like this (it has been modified because of NDA):
<abc:MyCustomControl>
<additionalItems>
<asp:CheckBox id="coolCheckboxOfPower" runat="server" Text="Triple Rainbow!">
</asp:CheckBox>
</additionalItems>
</abc:MyCustomControl>
So using the example above, if I try to use coolCheckboxOfPower in my vb page, it says it is not declared.
It has been suggested to me that asp controls cannot be nested. Is this true, and if so, how do I get around that?
Asp controls can certainly be nested. Just look at asp:Panel, asp:ListView etc. You have to do some extra work when creating your control to enable this to happen. Namely you have to make have an ITemplate property on your control. Check out the following Building Templated Custom ASP.NET Server Controls to get you started

Creating an ASP.Net Templated Server control

I want to be able to do something like this
<test:TabControl id="" runat="server"....>
<ItemTemplate>
<tabItem label="tab1" />
<tabItem label="tab2" />
</ItemTemplate>
</test>
The idea being here is that the only acceptable items in "ItemTemplates" are the tabitem types. There are many asp.net controls that use this, for example the ScriptManager class only allows you to specify certain types of objects under its various collections. Maybe thats the key to this.. I want to add a collection as opposed to a template.
The idea is that in code I will then iterate over each "tabItem" and create the tab as I want it to look (probably rendering div's etc).
Ive had a look at most of MSDN link on how to create templated controls but it doesnt seem to do exactly what I want it to.
Would be grateful for some assistance.
You need either a templated control or a custom control that can parse its content (Read about ParseChildrenAttribute(typeof())). Take a look at this article. Although not exactly your case it can inspire you.

How to control usercontrol from javascript

I have an usercontrol with an attribute targetUrl. I add this user control to a page and write targetUrl attribute from this page like below:
<PBG:Modal ID="Modal1"
runat="server"
Height="180"
Width="500"
src="pop_adres_giris.aspx"/>
This worked properly, but I want to change the targetUrl attribute from javascript. And I can't do it. I write code like below, but it didn't work.
var frm = document.getElementById('Modal1');
frm.targetUrl = 'pop_adres_giris.aspx';
How can I do it?
The UserControl object, which generates HTML on the client side, are not accessible as the rich objects which are available when handling server side calls.
Depending what the UserControl is, you will need to use a different method to get it and set the "targetUrl".
In addition, to ease your accessing of elements within the DOM you may want to consider using a library such as jQuery or prototype
Once you have declared your control, for instance, if you were using an asp:Hyperlink control:
<div id="hyperlink_holder">
<asp:Hyperlink ... NavigateUrl="http://someurl" />
</div>
You know that asp:Hyperlink generates html like <a href="http://someurl" ... />
So we can access the element and change the link like:
$('#hyperlink_holder a').attr("href", "http://newurl");
In addition, note that the ID you give an item in ASP.NET is not necessarily the ID which will render in the id element in the HTML; it is instead a concatenation of a number of ids; therefore use selectors based on non runat="server" controls where possible, or pass the ClientID of the UserControl through to the client to use for selection if absolutely necessary.

Resources