.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Making your own controls - how to make them powerfully modifiable so anyone can use a control templa

Posted By:      Posted Date: October 24, 2010    Points: 0   Category :WPF

If I look at a template for an expander, it always has to have a ContentPresenter in its control template and the control knows to use that for the content shown when the expander is expanded.  From there you can template out the control in all sorts of cool ways. 

A textbox's control templates need a scrollviewer with x:Name=PART_ContentHost for example.

The expander is fine with an unnamed control template, but the textbox requires a named scrollviewer.  I haven't messed around with putting multiple content presenters in an expander's control template and I currently have no idea what that would do.  This aspect of building my own controls is still quite foggy to me.  If I were to build a control, how do I determine if it looks for a named element in the control template (ala the textbox) or if it just finds a content presenter etc?

What if I wanted my new control to always contain several elements?

For example, with my current set of questions I am working on a resizable element.  I want that to always contain a draggable left and right boundary, as well as a togglable UI element for each of those boundaries that will toggle certain behaviors when I drag a specifc size and resize the control.  So ideally, I would want to define all of those things in a control template.  I would like it to

View Complete Post

More Related Resource Links

Accessing the different controls inside a GridView control

how we can access a particular control which resides inside a GridView control. In this article I will show you how you can access different controls inside a GridView control. We will see how we can access a TextBox control, a DropDownList control and a ListBox control. If you are working with ASP.NET 1.X then you might want to check out my article Accessing Different Controls Inside a DataGrid.

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)


Hey guys

I came across this bug using while trying to implement a sitemap

Line 147:						Panel panel = new Panel();
Line 148:						panel.Controls.Add(_viewState);
Line 149:						Page.Form.Controls.Add(panel);
Line 150:						string script = "document.getElementById('" + _viewState.ClientID + "').value = GetViewState__AspNetTreeView('" + Extender.MakeChildId("UL") + "');";
Line 151:						Page.ClientScript.RegisterOnSubmitStatement(typeof(TreeViewAdapter), _viewState.ClientID, script);

[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
   System.Web.UI.ControlCollection.Add(Control child) +8696951
   CSSFriendly.TreeViewAdapter.SaveAdapterViewState() in E:\wwwroot\ThirdParty\CSSFriendly\CSSFriendly_24242\CSSFriendly\TreeViewAdapter.cs:149
   System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +8901848
   System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +148
   System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +148
   System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +148

Extreme ASP.NET: A Little Bit of Control for Your Controls


Having worked for so many years designing and developing ASP. NET while at Microsoft, it's exciting now to have a venue in which to talk about it. In this new column, Extreme ASP. NET, I'll discuss and demonstrate time-tested techniques and approaches to implementing high-performance, reliable, secure, and user-friendly Web applications with ASP.

Rob Howard

MSDN Magazine May 2005

User Controls and Control Extenders


Two separate questions, but sufficiently intertwined that I'm going to try the two together....

Firstly - User Controls and Behaviours

I have a User Control which contains a few visual controls.  I am attaching some behaviours by using GetScriptDescriptors and am passing some JavaScript to the browser using GetScriptReferences.  That is all rock solid and working well.  However, when developing, I did discover that I couldn't attach the behaviours to the User Control itself - not surprising as rendering a User Control does not create a DOM element which represents the actual control.  So, I have attached my behaviours to a "random" TextBox within the User Control. 

So - first question, is there a better way to do that?  For example, should I always be rendering a SPAN as an element to surround the control?  Not sure what that would do to the robustness of the rendering.  What is the right way to attach a behaviour to a User Control.

Secondly - Accessing those same Behaviours later

Assume I have placed a User Control on the page.  That User Control exposes a Property (actually a Javascript Function of course) which does "something" to the control - for the sake of example we'll say that the function is set_Border(colour). 

Login control - Controls do not exist in current context

Hi all,Trying to set up the Login control as a User control, but none of the controls exist in the current context?None of my code below is recognised? What's going on here please? I've rebuilt the solution many times.if (Membership.ValidateUser(this.UserName.Text, this.Password.Text))        {            FormsAuthentication.RedirectFromLoginPage(UserName.Text, RememberMe.Checked);        }        else        {             Msg.Text = "Login failed. Please check your user name and password and try again.";        }

How can i make a WPF transparent group box control that is in the toolbox?

How can i make a WPF transparent Groupbox control that will show a picturebox image behind it? Thanks -civilwarrock

Controls within Base Control are Not initialized

I have an ASP.net control that contains a grid (protected member variable). Grid is populated from a function in the control. I have derived a new user control from this control without any control in it. I added additional functionality to populate grid and called base class function to do further operation on the grid.When I run my code, I get the null reference exception on grid object in the base control.Any idea why this might happen? Am I missing something in control inheritance? at what point do inner controls get created? 

Controls Disappear in Custom Control Design View

I have a site using eight (so far) custom web user controls  and two of them have started misbehaving in design view.  When designing the control itself some (in one control) or all (in the other) regular controls (labels, text boxes, etc.) disappear from the design view surface.  They can still be seen in the source view and if the control is hosted on a regular .aspx page the control displays properly.  The code window recognizes all controls in the objects dropdown list and I can write appropriate code.  The controls run properly on their hosted pages.  If I switch to Split view and select controls in the source view panel a very small area od the design surface becomes selected (like one pixel for any of us who remember programming old DOS applications with 80-column screens) but no controls can be seen. In short, the controls work in every respect except that I cannot see all or part of the design surface in Design view when editing the controls directly.  I've double-tripple checked the source code for anything that may be causing this and just don't see anything.  (The controls aren't that complicated.)

Custom Control Controls Collection

I have an ASP.NET custom control with several asp:panel controls that have various questions for users in them. When the control loads I need to set a Session object to the default panel (first question). This worked when I had the objects and code in a web form, but I moved them to a custom web control, and now when I try to access that control in the Load event of the custom control it says the default panel control is null. It seems that the controls are not created when the Load event is firing. Evidently the order of loading controls is different between web forms and custom controls. Can someone explain to me where the controls inside the custom control will become available to use? Thanks for the help! 

How to make a yes/no ComboBox user control

How to make a yes/no ComboBox user control

How to make cause validation false for Show Edit buttons in Gridview control

  HI. I am having 2 (dept name, City)dropdownlist, 1 Save button once if we click the save then record will be displayed in grid (Gridview with ShowEditbutton =true and I have Edit logic in my page). But if I click on Edit button then if I try to update the value then it is firing a message "Select dept" because dept dropdown present in the page so I want to make cause validation false for Show Edit buttons (update and cancel). Pleaase tell me Thanks

Making the program make text boxes according to a num of a variabe

i have a text abox that the user need to enter a number then according to this number the program need to show text boxes

Select multiple controls on tabpage, Drag & Drop control(s) (WinForms)


I'm wondering, if somebody got some code or thoughts for selecting multiple control displayed on tab page and drag & drop control on tab page it self. Something like on windows desktop, where you can draw a rectangel with mouse cursor to select folder and files. Also, drag folder or files to different location.



DDL selection is making control jump to the 1st tab?



I have the folllowing DDL on a 2 tabbed Ajax tabbed container. The DDL is on the 2nd tab, the problem I am finding is that; when a user selects a value from the DDL the focus is jumpint to the 1st tab and not remaining in position as requested. 



<cc1:TabPanel runat="server" HeaderText="Current Membership Stats" ID="CurrentMembership">


DataAnnotation on multiple user controls displaying on wrong control


I have a table that displays a user control on each row. The user controls are strongly typed to a viewmodel which contains DataAnnotation style validation. If one of the rows has invalid data entered (lets say the 3rd row) the data annotation validates display agains the first row/user control.

How do I get the data annotation to show against the relevant user control?

I'd post a pic to show what I mean except it will not display it!.:/

make a control required field using code in infopath


Hello guys,

How do i make a field required field using code. Specifically, how to make attachment control mandatory.

I know there is an option to select cannot be blank in properties but i want to do it conditionally.

Please help me.



Here is my new control code i couldnt make it so if i change the control size on form1 designer so i


I have two major problems.

1. After dragged the control to form1 designer when im on form1 designer i dont see anything in the control only when i run my program. I thought ill some kind of preview or what i painted in the control ill see it on the form1 designer before i run my application.

2. On form1 when i manualy stretch the size of the new control on form1 designer to be bigger or smaller the things i painted dosent fit i thought the way i created the code in the control code it will keep its size arroding to the size changed ill do in form1 designer but it dosent.


So here is the code of the control and form1 can someone help me with this two problems show me the correct code?


First the control code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DannyGeneral;

namespace My_Diet_Control
    public partial class My_Diet_Graph_Control : UserControl
        int x_axis_lenght;
        int y_axis_lenght;

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend