C# - How To Render Children Tags In A Custom Server Control
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
Similar Messages:
Feb 3, 2011
I am creating a custom control and was wondering if it is possible to render any JavaScript in the head tags of the page it is placed on? I am thinking I would need to use the FindControl method, but am not sure what I need to bind to. I am thinking the page object? Let me know if I am thinking in the right direction or if there is another option.
My only concern with the above mentioned idea is that how do I render all my content while placing some code in the head tags?
View 2 Replies
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
Feb 19, 2010
I'm new to writing custom ASP.NET server controls, and I'm encountering the following issue:
I have a control that inherits from System.Web.UI.HtmlControls.HtmlGenericControl. I override the control's Render method, use the HtmlTextWriter to emit some custom HTML (basically a TD tag with some custom attributes), and then call the case class' Render method.
Using the control:
<dc:Header id="header1" runat="Server" DataColumn="MemberNumber" Width="30%">Member Number</dc:Header >
The problem is that when I view my rendered HTML, the server tag is emitted to the client as well (right after the TD tag):
<dc:Header id="ctl00_ContentPlaceHolder_testData1_testData1_header1">Member Number</dc:Header>
How do I prevent this from happening?
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
Mar 5, 2010
Why custom Gridview control not render html properlies under <Columns> properly in Visual Studio 2005?
For example:
[code]....
View 1 Replies
Feb 18, 2010
I am trying to render control(ComboBox) programmatically, and i am getting error that tell me that i have to add ScriptManager to the page.
But, i did that, and still its not working:
[Code]....
And the render control html is:
protected override void Render( System.Web.UI.HtmlTextWriter writer )
View 3 Replies
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
Jan 22, 2010
I'm trying to develope a collapsible panel control with designer.
I have the following two classes:
[Code]....
and this one:
[Code]....
In fact, the designer class is a copy of a sample class in MSDN and I only tried to customize it to work for me. I'm not sure if all methods are necessary or not.
I have the following two questions:
1- When I use this contrl in design mode, I can add or delete controls to it, in the html view, tags are not updated. so when I close the page and open it again, all changes are lost.
2- I have added this control to the same asp.net project that is using this control. Isn't there any way to have this controil in tool box?
View 5 Replies
Nov 10, 2010
I'm working on a Navigation Menu. I've created below ServerControl and it works, but I want to allow users adding some standard ASP.NET controls within my ServerControl Tags like label, image and so on.
<MdsMenu:ServerControlMenu ID="ServerControlMenu1" runat="server">
<MdsMenu:animation AnimationSpeed="Normal" AnimationType="Opacity_Height" Delay="1000" DropShadow="true" />
<!-- HERE HAS TO HAVE SOME STANDARD ASP.NET CONTROLS -->
<!-- e.g <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> -->
</MdsMenu:MenuItem>
</MdsMenu:ServerControlMenu>
My problem is here that how I can get Child Controls within <MdsMenu:MenuItem> and show them like as they are in the output. P.S: I overwrite RenderContents method
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(OutPutStringBuilder.ToString());
}
View 2 Replies
Nov 10, 2010
I'm working on a Navigation Menu. I've created below ServerControl and it works, but I want to allow users adding some standard ASP.NET controls within my ServerControl Tags like label, image and so on.
<MdsMenu:ServerControlMenu ID="ServerControlMenu1" runat="server">
<MdsMenu:animation AnimationSpeed="Normal" AnimationType="Opacity_Height" Delay="1000" DropShadow="true" />
<!-- HERE HAS TO HAVE SOME STANDARD ASP.NET CONTROLS -->
<!-- e.g <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> -->
</MdsMenu:MenuItem>
</MdsMenu:ServerControlMenu>
My problem is here that how I can get Child Controls within <MdsMenu:MenuItem> and show them like as they are in the output.
P.S:
I overwrite RenderContents method
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(OutPutStringBuilder.ToString());
}
View 2 Replies
Mar 15, 2010
I've build a custom WebControl, which has the following structure:
<gws:ModalBox ID="ModalBox1" HeaderText="Title" runat="server">
<Contents>
<asp:Label ID="KeywordLabel" AssociatedControlID="KeywordTextBox" runat="server">Keyword: </asp:Label><br />
<asp:TextBox ID="KeywordTextBox" Text="" runat="server" />
[Code]....
However it seems to render properly, it doesn't work anymore if I add some asp.net labels and input controls inside the property (see above asp.net code). I'll get the HttpException:
Unable to find control with id 'KeywordTextBox' that is associated with the Label 'KeywordLabel'.
Somewhat understandable, because the label appears before the textbox in the controlcollection. However, with default asp.net controls it does work, so why doesn't this work? What am I doing wrong? Is it even possible to have two control collections in one control? Should I render it differently?
View 2 Replies
Aug 11, 2010
I am creating a custom server control in which I place a RequiredFieldValidator. In the the Render method and I want to dynamically create a RequiredFieldValidator and render it to the HtmlTextWriter.
The problem is, when I call RenderControl on the RequiredFieldValidator, it only generates a span for me with no javascript or client side code.
How can I completely render a RequiredFieldValidator to an HtmlTextWriter?
View 3 Replies
Mar 15, 2011
I am rendering custom control. I rendered the css file on the prerender. When i rendered more than one control in the aspx page, the css will loaded the number of times the controls in the page. I want to load the css file only one time. How to check the webresource css file on rendering whether it is loaded or not.
View 1 Replies
Mar 17, 2010
I have a control that displays a list of comments. The comments are created in a loop. The problem is that I want to put a LinkButton at the end of each comment and change the CommanArgument of each button to the ID of the current comment.
When I put the Linkbutton in the CreateChildControls method and then add the CommandArgument in the Render loop, the button works, but the argument is blank. When I put the code that's in the CreateChildControls method inside of the loop, I don't get an error, but nothing happens when the button is clicked.
Below is the relevent code ...
[Code]....
View 1 Replies
Jan 3, 2011
Hope this is the correct forum for this question. I am using VWD 2010 an have a web project and get the following error upon execution:
Parser Error Message: Unknown server tag 'custom:AjaxValidator'.
My code is as follows in the .cs file:
[Code]....
[Code]....
View 1 Replies
Sep 30, 2010
I have a server control which consists of a gridview with custom navigation&information capabilities and with javascript functions to highlight the selected grid items or highlight when mouse over event occurs.. (full tested outside asp:wizard)I'm trying to use that server control inside a template wizard step, when I drop such server control in the first step of the wizard everything goes ok.
When I insert even a blank step previous to the step which contains the server control, the javasript code of the server control is not rendered at all, the page doesn't fail during load but until I go with mouse over an item of the grid for instance.When I check the rendered HTML I can see no HTML, nor javascript events of this server control where rendered, neither the implementation nor calls to javascript, but even rarer what is there visible in the grid for ie, even the other components of the server control, are not in the "view source code" content. I seems it was sent to the client, even showed, but truncated in some phase..
View 1 Replies
Oct 22, 2010
I have extended a gridview to add an additional header with following two hyperlink controls "Select All" and "Clear All". These will operate on checkboxes in the data rows of the grid. I hide the column headers as I only want to show one column with checkboxes and "Select/Clear All" links. Everything is working as expected. Now, I wanted to add a scrollbar to my grid control, I did add the scroll bar using div but what it does is, it includes the "Select All and Clear All" links aswell. I only need to add the scrollbar to datarows and not to the header.I am trying to extend my control to include hte scrollbar by adding div during Render function. But how will I determine or loop through header rows? How will i get the header row with hyperlinks that I created above?? In Render if I do this.HeaderRow, it gets the original column header and not the custom header.
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
Dec 24, 2010
here's a situation and I would appreciate your response.
I have programmatically created the Wizard control:
Page_Load(obj s, evargs e)
{
Wizard ClaimDetailWizard = new Wizard();
foreach(int item in selectedItems)
{
//create new step
//added custom control to new step
//add step to wizard
}
//added wizard to a placeholder on a page
}
Based on List I get from Session i added new steps to my wizard To each step I had added a custom control
Each custom control in tern contains another custom Gridview Control in it.
So here's the problem when the page loads for example for two steps. All is good Wizard does what it's supposed to do.
But when I try to use sorting or paging in that custom Gridview. Somehow it displays the gridview I should see in the next step of the wizard.
Also what I'm noticing through debugging. Is that when I press next in the wizard I go back to the original page where I do all of the code specified above, and it recreates the wizzard. But it goes to the next step. Is this the way wizard supposed to work? Just doesn't seem very efficient.
View 1 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
Aug 20, 2010
i am new to technology so go easy on this post according to what i read from web there are 3 type of custom server controls 1 superclass2 composite 3 renderedi found video tutorial for superclass custom server control but couldnt find video tutials for other 2.lease forward me link for composite and rendered custom server controls video tutorial
View 1 Replies
Dec 20, 2010
created a Custom Textbox Control that is intended to be a "masked date" textbox, so that I can handle different ways a user might enter in a date and make it work no matter what.I did this by creating a custom control with 3 separate Textboxes one for month,one for day one for year,then do some handling for different things that could be entered and it would all work.
The problem I am having is,when I add this control to the page,if I do an alert to check the Text value of the custom control it just gives me "[ServerControl1]" even if I enter in a date.
I tried doing an AutoPostback after entering so that it would refesh the Viewstate to have the values of the textboxes but the control doesnt seem to actually postback, even though I have it enabled.accessing the Textbox value after someoen
has entered a date?first custom control I have created and have been using google.
My Server Control Code is as follows:
[Code]...
View 1 Replies
May 17, 2010
I am creating custom server control. I have two classes in project. One is main class that render control and another will be used to render content based on condition.I just want to know how to render content from other classes in main class.For example. This isjust an example.My main class code
[Code]....
My TopLeftPane class
[Code]....
My page code on page load event
[Code]....
When I run the page, Header title always getting null. This is a just sample code. I will have more than 20 classes so I dont want to write a code in main class to render whole control.I hope all of you understand my problem.
View 3 Replies
Jul 28, 2010
Case : a templatefield Text Box created by class TemplateHandler and added to the composite
control Grid .how to bind it to the DataSource Object of the Grid Class ?
View 2 Replies