C# Custom control - class hierarchy and markup - c#

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.

Related

Create Gridview Template field and dropdownlist dynamically for AutoGenerateColumns ="true" for 300 columns

I want to enable inline header search in gridview , for this I need Template fields which I cannot write on design page as I am generating columns from database.
I need functionality like this......
here
Please, suggest me any other way if this is not possible.
Create a custom class that inherits the ITemplate interface. Override the InstantiateIn method and create controls required for this template as described here.
Assing an instance of your class to TemplateField.HeaderTemplate property of your grid in the GridView.Init event handler. Alternatively, try using a third-party component that has such a feature.

Repeat an user control in user interface

I am using ASP.NET Web Forms and i created a custom .ascx user control which represents a Car having properties like Type, Picture and Color.
Now i try to add on user interface this custom control in a repetitive way. Lets say if i have in database 5 cars, then 5 user controls will be render on UI preferable inside an asp control.
I was looking for such a control, studying the grid view, and repeater but they seems to have as data source only lists of objects which are build from "primitive" types not objects like a list of my custom ascx control.
My question is if i can somehow to render those user controls inside a repetitive cycle inside an asp or html control which gives the option to format it (supports css) ? (and if so please provide me an example)
Create a ListView with your user control inside of it, and then bind a List<CarObject> to that ListView
<asp:ListView ID="listView1" runat="server">
<ItemTemplate>
<myUC:MyUserControl ID="myUserControl" runat="server" />
</ItemTemplate>
</asp:ListView>
Bind a list of your object, such as List<Car>, to that list view, listView1.DataSource = myListOfCars;. Then in a ItemDataBound event bind your object to your user control.
You could also, alternatively, place the markup from your user control inside of the ListView, and then bind the information inside of an ItemDataBound event and sidestep the whole user control issue altogether. This still allows you to reuse the markup.

Editing Custom Controls or Panels in the Designer environment

I have created a custom control that inherits System.Windows.Forms.Panel, and adds a few extra properties. I then intend to create child classes that inherit this custom-panel class, and add content to them.
The custom-panel class will be passed to a "Wizard" framework (with back/next buttons) as the content for the various steps. I intend to make extensive use of this, creating 40-50 different pages for Wizards to handle various things in my project.
Question: Is there a way to view just the panel in the Designer, and modify its layout and design from there? I could code everything the hard way, but I really don't want to.
I did some searching and found this article, but that discusses creating a custom control and adding it to a library. I don't need to do this, I just want to view/edit the control in Designer directly, without adding it to a Form.
Obvious Answer to the rescue again.
Create a custom control, add the layout/split panel as desired, and change it's property to DockStyle.Fill.
This makes your custom control "behave" like the layout control, as long as you add all other controls to the layout control.
add first this name space
using System.ComponentModel.Design;
Apply the System.ComponentModel.DesignerAttribute attribute to the control as follows:
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public class UserControl1 : System.Windows.Forms.UserControl
{
...
}
now you can edit your custom user control in designer environment

ASP.NET web control only allow specific contents?

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.

Set CMS content in .NET

What would be the best approach to set dynamic content from the database to controls in an aspx page?
My database consists of pages (index.aspx, home.aspx and so on) which consists of controls (DivStart, LabelDescription, and so forth).
The first technique that came to my mind was looping through all the controls in the page, looking for controls that have a certain class, e.g. "Cms_DivStart", and would then set the inner html for that control from the database control called "DivStart". The problem is only runat server controls shows up, and I don't want to make all controls server side.
I could store all the dynamic texts for a page in hidden variables and set it with jQuery when the page has loaded, but that would make the text not show up directly..
Any other ideas would be greatly appreciated.
Thanks
/Andreas
You could use asp:PlaceHolder tags, these don't add any html unless they are utilised.
You can write a custom class, I'll call it "CustomPage", that inherits the System.Web.UI.Page class. Then your pages can inherit "CustomPage". In this class, add methods to retrieve data, set custom properties and display your content in the available controls.

Categories

Resources