How To Create Custom/User Control With Children
Jun 15, 2010
I want a create a custom/user control that has children.
For Example, I want my control to have the following markup:
<div runat="server" id="div">
<label runat="server" id="label"></label>
<div class="field">
<!-- INSERT CHILDREN HERE -->
</div>
</div>
and when I want to use it on a page I simply:
<ctr:MyUserControl runat="server" ID="myControl">
<span>This is a child</span>
<div runat="server" id="myChild">And another <b>child</b>
</ctr:MyUserControl>
The child controls inside my user control will be inserted into my user control somewhere. What is the best way to accomplish this?
The functionality is similar to a asp:PlaceHolder but I want to add a couple more options as well as additional markup and the such. Also the child controls still need to be able to be accessed by the page. (in the example above the page should have the myChild Control on it)
EDIT ------
It can be a template control as long as it allows me to reference the children on the page.
View 2 Replies
Similar Messages:
Feb 25, 2011
I have created a really simple custom control to render panels into a page. The code is below. When I use my control and add any webcontrol to it, after each page postback the contents of these child controls vanishes.
I must have missed something really simple but run the example to reproduce this. Can anyone please enlighten me as to why the controls are being 'emptied' of their contents, and how I stop this occuring?
Here is the code for the control
[Code]....
And here's how I am using it in a sample page:
[Code]....
[Code]....
View 16 Replies
Nov 17, 2010
I'm working on a custom ServerControl, I've created it like below :
[ParseChildren(true), PersistChildren(true)]
[ToolboxData("<{0}:Menu runat="server"></{0}:Menu>")]
public class Menu : WebControl
{
.....
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public MenuItem MenuItems { get; set; }
}
[ParseChildren(true), PersistChildren(true)]
public class MenuItem : WebControl
{
......
[PersistenceMode(PersistenceMode.InnerProperty)]
public MenuItem SubMenuItems
{
get
{
if (_SubMenuItems == null) return new MenuItem();
return _SubMenuItems;
}
set
{
_SubMenuItems = value;
}
}
private MenuItem _SubMenuItems;
[TemplateContainer(typeof(MenuItem))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate Template { get; set; }
}
<%@ Register Assembly="JQueryMenu" Namespace="JQueryMenu" TagPrefix="MdsMenu" %>
<MdsMenu:Menu ID="Menu1" runat="server">
<AnimationItems AnimationSpeed="Fast" AnimationType="Opacity_Height" DropShadow="true"
Delay="1000" />
<MdsMenu:MenuItem ID="MenuItem1" runat="server" Text="MenuItem 01">
<MdsMenu:MenuItem runat="server">
<Template>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CheckBox ID="CheckBox1" runat="server" />
</Template>
</MdsMenu:MenuItem>
</MdsMenu:MenuItem>
<MdsMenu:MenuItem ID="MenuItem2" runat="server" Text="MenuItem 01">
<MdsMenu:MenuItem ID="MenuItem3" runat="server">
<Template>
<asp:Button ID="Button2" runat="server" Text="Button" />
<asp:CheckBox ID="CheckBox2" runat="server" />
</Template>
<MdsMenu:MenuItem ID="MenuItem5" runat="server" Text="MenuItem 05">
</MdsMenu:MenuItem>
<MdsMenu:MenuItem ID="MenuItem6" runat="server" Text="MenuItem 06">
</MdsMenu:MenuItem>
<MdsMenu:MenuItem ID="MenuItem4" runat="server">
<Template>
<asp:Image ID="Image1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</Template>
</MdsMenu:MenuItem>
</MdsMenu:MenuItem>
</MdsMenu:MenuItem>
</MdsMenu:Menu>
Now, How can I Parse it and render it in RenderContent method ? !!!
The following method is always throw the first Exception, it means this.Controls is always empty !!! How can I do it and how I can access to the nested children in RenderControl method ?
public class Menu : WebControl
{
....
public override void RenderControl(HtmlTextWriter output)
{
if (!this.HasControls())
throw new Exception("Controls are empty");
....
}
}
View 1 Replies
Feb 18, 2011
I'm attempting to create a simple menu user control just as outlined here.
The attached code results in an "Object reference not set to an instance of an object" error, but I can't figure out why.
<%@ Master Language="VB" CodeFile="MySite.master.vb" Inherits="MySite" %>
<%@ Register src="Controls/Menu.ascx" tagname="Menu" tagprefix="my" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">......
View 1 Replies
Jan 7, 2010
I have a very simplistic user control that looks like this:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="wfWindow.ascx.cs" Inherits="webfanatix.co.za.wfWindow" %>
<div>Just a test...[x]</div>
with this code behind:
[ParseChildren(false)]
[PersistChildren(true)]
public partial class wfWindow : System.Web.UI.UserControl
{
protected override void Render(HtmlTextWriter writer)
{
RenderChildren(writer);
}
}
And the usage thereof looks like this:
<wf:wfWindow runat="server">This content should go where [x] is.</wf:wfWindow>
I'm no ASP.NET pro, so how do I get the content to render exactly where the [x] appears in my user control?
RenderChildren is rendering my content, but it is only appended to the end of the UserControl output. I need it to go and sit right where [x] marks the spot.
View 2 Replies
Oct 30, 2010
I want to create user control that can be added on other site.
here is the scenario.In my web site reservations can be done for restaurants. I want to provide user interface for reservation in restaurant owner's web site. When user goes to restauarants site he can reserve the slot through my site.
View 2 Replies
Mar 6, 2010
I'd like to create a user control which takes XML as input, applies an XSL transform on it, and displays the results of the transform in the web form or windows form in place of the user control. The transform should happen automagically when the windows form or web form is first loaded. I'd think this would be pretty easy to do. I just don't know how. point to a guide/tutorial on how to do this?
View 1 Replies
Mar 4, 2010
I have a custom control which inherit from the Table class and in the constructor, it takes a an integer as an argument. There is no empty constructor.
Is there a way for the user to set that variable in the properties window after they drag the control onto a form.
I know some .NET controls, you can set the source for the parameter to different things like another control's property, QueryString using just the properties window.
Right now, I have to create the control dynamically. I read the query string and then created the object.
View 1 Replies
Oct 27, 2010
I have created a user control and am ready to convert it into a custom control. I am trying to follow:
[URL] the steps mentioned in the link.
I am not seeing Build / Publish Web Site option.
I am using VS 2008. I only see Publish User Control but it doesn't give all the settings mentioned below. When I do publish user control, it just copies the ascx file to the desired location. how can I convert the user control to custom control.
View 2 Replies
Mar 20, 2010
Can a custom user control embed another custom user control?is this "bad practice"?eg if i have a custom user control named "Forums" then 2 more custom controls that are "Read_Forums" and "Write_Forums"and all i need to call is "forums" in the page asking - and then it will put in both functionalities..
View 2 Replies
Oct 22, 2010
I want to create a custom control where I define several specifically named DIV sections. The code-behind logic of the custom control will perform operations on these named DIV sections. The page where the custom control is hosted needs to be able to add named controls to the DIV sections and perform operations on these named controls added to the DIV sections.
Let me explain a little further. In the custom control, I want to have code behind logic that fires the following Page Event and uses a condition to make one or another specifically named DIV section visible or not:
< _
ToolboxData("<{0}:BaseDetails runat=""server""></{0}:BaseDetails>") _
> _
Public Class BaseDetails
[code]...
how can I create a custom control which is able to generate the named DIV sections "DetailsNameDiv1" and "DetailsNameDiv2" where my custom control can check a condition and make one or the other visible. Also, I need to be able to add controls to these named DIV sections when I host the custom control and the control that I add to the named DIV section needs to let me set values of these controls from the code behind logic of the page (or usercontrol) where the custom control is being hosted (in my example, I am adding TextBox controls that I want to set the Text property from a SQL Data Reader).
View 3 Replies
Feb 24, 2011
I have currently on multiple pages a modal popup that displays the information below, we are getting to the point where we need to call this popup from multiple locations, currently i have duplicate setups on each page that i want to use it on, but its becoming a mess to manage when a change is needed.. so im looking to see if what i have within the modal popup can be turned into a control that i can call anywhere thru out the site and have just 1 setup to maintain going forward..If its possible to create a control that can then be placed within the modal popup and server the same purpose that would be great any links here is what i have within the modal popup that i have on atleast 3 pages currently..
[Code]....
View 2 Replies
Feb 15, 2010
I am trying to create a web custom control but I am unable to find the templete that should be in my projects window. I am using Visual Studio Pro and it should be there. Doc's say standard version doesn't have it but thats all I can find.
View 4 Replies
Mar 10, 2011
I have a web user control that represents a simple message box. It is used to display simple messages like "Item has been deleted", or "The item was saved successfully".
On the other hand, I have another web user control that represents the item in the form of an editable form (I made this a user control because this is used in two different pages). I want to embed an instance of the message box user control inside the editable form. I am writing this right after the @Control directive:
[Code]....
Instead of using @Register directives, I register the user controls in web.config and so far this has worked just fine.
With the above markup, the project compiles, but whenever I try to navigate to a page that contains this construct, I get an HttpParseException exception. Furthermore, Visual Studio states that the tack w7rc9:MessageBox doesn't represent a known control.
What am I doing wrong?
View 2 Replies
Oct 12, 2010
I have 3 pages in each page there is a portion of page where it is common in all these three pages.Thought of making the common stuff as a separate control either User control/Custom control.Any suggestions on which one should i use custom/user control
View 2 Replies
Feb 15, 2010
I'm new to all of this, so go easy on me. This should be an easy one. I have created a custom SmartContentBlock user control for a CMS.The control works, but only if I invoke the Fill() function in the Page_init event in the codebehind of the ASPX page. My goal is to modify the class file so that the Fill() in the codebehind is no longer neccesary. So, for reference, here's the gist of the current situtation.Controls.cs
[Code]....
ASPX Page
[Code]....
View 3 Replies
Nov 14, 2010
I want to remove parent control (which is span in this case) without deleting its children controls from container. how can I accomplish this in asp.net c#?
you can see the code here: http://pastebin.com/9NiriWXN
Note: I can easily find the "newsright" control and return its parent (which is span in this case)
View 1 Replies
Apr 4, 2011
I want to create a control just like a Panel.
I want my control to accept some controls as childs without typing the template name, just like the Panel, as shown here:
<asp:Panel runat="server">
My content
<div>Content</div>
</asp:Panel>
I have controls with content inside without telling what is the ITemplate.
I basically want to convert this
<my:MyControl runat="server">
<ContentTemplate>
My content
<div>Content</div>
[Code]....
The above works with <Content></Content> tags inside the control, but without it doesn't work. And the attribute isn't doing anything at all (I guess). What's missing?
View 6 Replies
Mar 28, 2011
I have a user control (ascx file) that contains an interface for users to change the data retrieved from the db. So, I am displaying data and most of this data can be edited by the user. When the user enters text into a textbox, and clicks "Save Changes", the value they typed in is lost and not persisted to the database. I realize now this is because the SaveChanges button does a postback, and the user control gets reloaded with its original valuesbefore the code within my SaveChanges button gets executed. I'm not sure if there are properties I need to set to save the input data, or if I need to write my own code to do it. What is the generally accepted method to do this? (Also, all of the controls in the ascx file are wrapped in an Update Panel.)
View 8 Replies
Sep 21, 2010
My project needs to let users to create a ASPX page where the page will hard code with all the web part zone and control. Does anyone know how can I do that? Remember that in our share point, it has a function that let a user create a new website or adding a new page into the website. I want to get this functionality works like share point.
View 3 Replies
Jul 22, 2010
I need to develop control (template or user) which must have subtags with some items.
For example :
<SomeControl runat="server" id="scTest">
<column1>TestColumn1</column1>
<column1>TestColumn1</column1>
<column1>TestColumn1</column1>
</SomeControl>
This control may contain some other web server controls such as GridView.I need explanation of experienced developers, how to do that - take me to the right way
View 3 Replies
Jul 19, 2010
I have searched everywhere and can't find a great solution. All I want to do is have about 40 extra fields that save to a SQL database when creating a new user. Will also need to keep the functionality of roles so I don't want to completely override the built in functions.
I have seen the ability to put the data into a profile, but as I understand that will not save to the database in a form that I could join/select against.
There's the ability to tie onto the end of the CreateUserWizard1_CreatedUser, but then it's an extra select/insert into a separate table. Would be nice if I could keep everything in 1 function and in 1 table.
So what is the best way to add a bunch of extra fields into the database when creating a new user?
View 3 Replies
Apr 4, 2011
I am trying to build an ASP.NET site that allows each user to set their own session timeout value. Basically from the user's perspective if they set their timeout value to 30 then that means that after 30 minutes of inactivity they are required to re-authenticate. If they set their timeout value to 0 then that means that they can leave the browser open as long as they want and their session never expires - they never have to re-authenticate. This is different than "remember me" functionality. If they close their browser they will have to re-authenticate.
Currently I am using Forms Authentication in my application and I have one object that I am storing in ASP.NET session. This object is a custom User object that has a number of different pieces of information about the current user.
What code would I need to write so that after successful login, the system has the proper authentication ticket and the proper session expiration value? I keep thinking I have this working and then I notice weird time outs where the system seems to think I am authenticated but my session has expired.
Currently on my login page I am using an ASP.NET login control. i have custom code in the Login_Authenticate event to authorize the user. Then I have the following code in the Login_LoggedIn event to set the auth ticket.
[code]....
View 1 Replies
Apr 24, 2010
I use asp.net default membership provider. in my register.aspx page i use a CreateUserWizard to create new user. in this wizard i want to create a custom step in first step to show the user some roles with a checkBox if user check it mean he agree with the roles and can create account.
i create the step and i put the some text and a checkBox in it. the problem is i don't know how to get the value of checkBox and how active the next button in first Step of my wizard.
View 1 Replies
Aug 19, 2010
I am new to asp.net programming. While working with windows form(windows application) i have created a window for editing purpose(just like popup window ) which contains three bounded dropdown list a button and a gridview. After making selections from the drop down when we press button the corresponding result should display in a grid.After that when you click on the grid row it will load the corresponding datas in my form. i want to create such control in asp.net . i have to use jquery or ajax. And this pop up window i have to use more than twenty times in my application.am using VS2008 ASP.NET and C# 3.5.
View 3 Replies