I am new to ASP .NET web controls, but not ASP .NET in general or C#.
I am wondering how I can limit the allowed content types to a specific class.
I have made a custom web control called TabPanel, and I want it to only be able to contain TabPages.
As an example, the following markup should be illegal, since it contains a checkbox.
<cc1:TabPanel ID="TabPanel1" runat="server">
<cc1:TabPage runat="server">
this is a simple test
</cc1:TabPage>
<cc1:TabPage runat="server">
this is another simple test
</cc1:TabPage>
<asp:CheckBox runat="server" />
</cc1:TabPanel>
In this case, I wouldn't want the checkbox to be there. How can I block this from happening?
I have not tried exactly what you are after but based on other things I have done I would try this:
Create a property in TabPannel that is a collection of TabPages (call it Tabs for demonstration purposes). This can be an array, a list, or a custom collection class, the key is to have typed to only accept TabPages as members.
Give the property the [PersistenceMode(PersistenceMode.InnerProperty)] atribute.
Override CreateChildControls to add the contents of the collection to the control.
If you do it this way then your mark up should end up looking something like this:
<cc1:TabPanel ID="TabPanel1" runat="server">
<Tabs>
<cc1:TabPage runat="server">this is a simple test</cc1:TabPage>
<cc1:TabPage runat="server">this is another simple test</cc1:TabPage>
</Tabs>
</cc1:TabPanel>
and it should not allow anything that is not a TabPage to be nested inside of the Tabs property.
http://msdn.microsoft.com/en-us/library/9txe1d4x(v=VS.90).aspx is a walk through demonstrating this technique in detail.
I figured it out.
Had to throw an exception under AddedControl procedure that I overrided from the WebControl if the type of the control being added was not of the type I wanted.
Now the designer shows a beautiful red error-message on the control itself, preventing me from doing such a foolish thing.
Awesome!
I'm going to take a guess here, but based on some quick googling I think you're looking for the ControlBuilder. The demo limits their control to an object called "mycell", but I don't see any reason why this couldn't be limited to your own objects, or build-in ASP.NET controls (i.e. Panels but not TextBoxes, etc.)
As a last resort, I'm sure you could hijack the rendering method and only render controls within the pre-determined class set, but this seems hack-ish at best.
Related
I have the following asp.net div setup on my page:
<div runat="server" id="ImageContainer">
<img src="images/<%# Eval("Image") %>" class="ArticleImage"/>
</div>
and I know this is being created at the server as when I view it in developer tools its id is:
ctl00_body_ArticleInfoRepeater_ctl00_ImageContainer
which is being created by the server.
I now want to edit the Css under certain conditions by doing the following:
ImageContainer.CssClass = "invisibleClass";
My problem is, in the C# code I am getting the following error:
The name 'Image container' does not exist in its current context.
I have no idea why this is happening, as it clearly does exist.
Any ideas?
Are you using some databinding control such as GridView, Repeater or some other?
The syntax <%#....%> represents data binding which works if it is placed inside some data binding control.
In such case you cannot access "ImageContainer" control directly. You have to search through parent control.
Since you haven't mentioned what is parent control of "ImageContainer" it's hard to give code sample here... Though here is example how it can be done in GridView
Incase, if you haven't used DataBindingControl then I would recommand to check yourpage.aspx.designer.cs and you should be able to find control name there!
Hope this will be helpful.
That's because you're trying to reference a dynamically created object (obiviously inside a repeater). Firstly, you shouldn't set ID for dynamic objects created during runtime. IDs have to be unique, or else you'll run into problems.
Secondly, you need to get the parent object (probably the repeater itself or the container?) and then traverse the collection of child controls to find the one you're after. There are numerous answers about how to find a control in an asp.net webforms, so just google for a while and I'm sure you'll get the code.
For instance find control
Controls placed on data list,repeater,grid-view are not accessed directly like other server controls placed on the page. If you want to access it through code behind you can access it on Data-bound or Item_command event of the repeater control because these controls itself act as containers for other controls placed on them.
you can use
e.Items.findControl("controlID") to access a particular row controls.
I recommend you to study these two events of repeater control.
if you want to change class of all div with name imagecontainer , you can use javascript or jquery for doing it with few lines of code.
I have struggled to find a clear and concise answer to this simple question.
Given an object property in the page named "SomeObj", is it possible for two way data binding like this:
<asp:TextBox ID="TextBox1" runat="server" Text="<%# SomeObj.SomeProperty %>" />
This will correctly display SomeProperty in the textbox if we call "this.DataBind()" in Page_Load. However, my object property is never written to during postback - have I done something wrong or is this actually not possible?
Should I put this control inside some container like FormView or DetailsView? At what point in the page life cycle should I be able to see my object property being written to?
All the thousands of copy-and-paste tutorials out there focus on lists and grids and binding to Sql Data Sources, which I do use successfully. But for a simple, single object surely there is a way that does not require extraneous containers, data sources and code-behind?
Of course the code to do this in code-behind for one text field is trivial, but no longer when multiplied by loads of properties and loads of pages. I have instead written two simple routines that use reflection to automatically load controls with property values and then save the control values back to the properties. With these routines we can then fill the page with simple markup like this:
<asp:TextBox ID="SomeObj_SomeProperty" runat="server" />
By simply naming the controls the same as the object property and calling my two methods at the appropriate times I get two-way data binding and the code-behind has very little code in it other than creating the main object, and I do not have to wrap the markup inside other controls just to get the data binding to work.
I am not very experienced with ASP.NET and it seems like I am doing something that should already be done for me.
UPDATE:
Note that if I try to use Bind() instead I get the usual data binding exception because there is no special data-binding control like a list or grid view:
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SomeObj.SomeProperty") %>' />
Gives this error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used
in the context of a databound control.
Hello Everyone I am a newbie in asp.net,
I have a checkbox inside a repeater which is inside an update panel,the page flickers whenever I click the checkbox, finally found that adding the ClientIDMode="AutoID" controls the flicker and everything works fine.
I am wondering about ClientIDMode what this has to do with the flicker,
went through msdn and found it generate the value of the ClientID property.
First distict the asp.net controls, from the final rendered html controls.
When you make asp.net controls you give them an ID, with this id you can address them and make change on them on code behind and with programming in general.
For example with this code
<asp:CheckBox id="check1" Text="option a" runat="server" />
you can use the check1 to get their value, change the text, and many others.
Now, asp.net must render this check box on the html page. When you make any control on an html page you must set to him a unique ID and name, and heres come the automatic id assignment.
asp.net take care to avoid conficts between final rendered controls and make automatic ids base on the structure that you have use on asp.net side.
For example, if this control is inside a custom control, and this control is inside a master page, asp.net will add also this names on the final id, to avoid conflicts.
Conflicts can exist for example on a repeated control, where you render the same control many times, so there you need to change each rendered id.
Conflicts can exist when you use many times the same user control.
Conflicts can exist when you use the same id on different user controls on the same page.
All that and many others asp.net comes and solve with the automatic generated id on client side render.
Yes, it does have something to do with the flicker.
See this related question for another example of the fact that Repeaters don't handle client ids well, causing exactly this problem because they end up doing a full postback instead of a partial postback.
Setting ClientIDMode="AutoID" is the workaround and allows the Repeater to succeed in doing the partial postack it is supposed to be doing from within the UpdatePanel.
Because of this Repeater bug, without ClientIDMode="AutoID" you were experiencing a full postback and full page load. A full page load does cause a flicker compared to a partial page load.
I am trying to create the custom grid control from the scratch. So I have created the class library project for it and started with
public class ServerControl1 : WebControl
{
}
Now, I have added this reference in my project. So in my aspx page it shows something like
<cc1:ServerControl1 runat="server" id="grid1" />
Now i want to defined the columns of this grid and also want to make it familier like asp gridview
<asp:GridView ....>
<Columns>
<TemplateFields>
.....
Now to make such hierarchical structure GridView >> Columns >> TemplateFields, what class structure should I implement?
I am not much in to OOPS so any help will be much appreciated. I don't want to inherit the GridView control in my class as I am not going to use it and I don't know what over burden it will bring to my control.
Check this link with a answer already posted. Create custom control with nested tag like GridView >> Columns >> Paging
Basically what you need is not Custom control but a composite control which should implement CompositeControl, IScriptControl IScriptcontrol is optional if you need AJAX support.
And then all your properties you will be able assign values in markup. Here is a MSDN link to create one as well.
http://msdn.microsoft.com/en-us/library/12yydcke.aspx
Also you have to understand how to override CreateChildControls method in CompositeControl and how to create your control Hirearchy.
Frankly, there is quiet a bit of learning to do actually.
I'm very new in .net c#, just a question here to about the following code. I have wrote a code to display a text box on the screen as below:
<asp:TextBox ID="sUnit" runat="server" MaxLength="12" Width="3em" />
But somehow I failed to using jqueryto assign a value into this textbox:
$('#sUnit').val('test');
Surprisingly when I view the source code, and it show me the ID for the text box is like below:
<input name="ctl00$cplh$sUnit" type="text" maxlength="12" id="ctl00_cplh_sUnit" style="width:3em;" />
Does anyone know how can I get the textbox ID so that I can assigned the value into the textbox?
Hope my question is not sound stupid.
If you are using ASP.NET 4.0, set the ClientIDMode to static.
For older versions, use
$('#<%= sUnit.ClientID %>')
This is due to the fact that UI elements containing these controls are being rendered by ASP.NET, those containers that implement INamingContainer generate the prefix that should be prefixed as per their scope.
From the MSDN line mentioned above:
Any control that implements this
interface creates a new namespace in
which all child control ID attributes
are guaranteed to be unique within an
entire application. The marker
provided by this interface allows
unique naming of the dynamically
generated server control instances
within the Web server controls that
support data binding.
One suggestion in order to find the control itself might be to use the first selector, where you controls are contained within a regular div. An example to find elements of type input might look something like:
$('#MyDiv').first('input[type="text"]')