Servercontrols - Creating Templated Server Control?
May 25, 2010
I want to be able to do something like this
<test:TabControl id="" runat="server"....>
<ItemTemplate>
<tabItem label="tab1" />
<tabItem label="tab2" />
</ItemTemplate>
</test>
The idea being here is that the only acceptable items in "ItemTemplates" are the tabitem types. There are many asp.net controls that use this, for example the ScriptManager class only allows you to specify certain types of objects under its various collections. Maybe thats the key to this.. I want to add a collection as opposed to a template. The idea is that in code I will then iterate over each "tabItem" and create the tab as I want it to look (probably rendering div's etc). Ive had a look at most of MSDN link on how to create templated controls but it doesnt seem to do exactly what I want it to. Would be grateful for some assistance.
View 1 Replies
Similar Messages:
Sep 10, 2010
There are no errors with my Javascript. The Javascript returns false, and the form is submitted. Additionally when setting UseSubmitBehavior to false the form still submits. What am I missing to prevent the form from being submitted?
// DerivedButton.cs
public class DerivedButton : System.Web.UI.WebControls.Button
{
public DerivedButton2()
: base()
{
this.OnClientClick = "DerivedButton_OnClick()";
//this.UseSubmitBehavior = false;
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string resourceName = "MyControls.DerivedButton.js";
[Code]...
View 1 Replies
Apr 13, 2010
I am trying to develop a very simple templated custom server control that resembles GridView. Basically, I want the control to be added in the .aspx page like this:
[Code]....
I know that what I want to achieve is pointless and that I can use DataGrid to achieve it, but I am giving this very simple example because if I know how to do this I would be able to develop the control that I need.
View 1 Replies
Aug 11, 2010
I have a templated user control with single instance template attribute. It works great for everything except the following scenario:
[Code]....
The folder selector uses a tree view that allows people to select a folder, its FolderSelected event works perfectly and the file selector updates with the files. The file selector has a repeater that lists the files in the selected directory. It has a linkbutton for each of the files so that the user can select a file. The problem is that the ItemCommand event of the repeater doesn't fire when the file name is clicked on. The page performs the async postback like it should, but in debug, the method to handle the itemcommand event is never hit. On the same user control to select files there is a button that the user can add files to the folder. When it is clicked, the event DOES fire and the file is added and the control refreshes in the update panel, everything perfect. It seems that because the linkbutton is already in a templated control (which is in a user control, in a templated control), the event isn't firing correctly. I am confused as to why this should be a problem, especially since all the other events (even the custom ones) fire as long as they aren't in another templated control. Since the code is complicated, I didn't want to post it all here.
View 6 Replies
Jul 21, 2010
Is it possible to set a specific ID on an ASP.NET server control? Everytime I assign an ID and run the web form the ID changes.
For Example:
<asp:TextBox ID="txtName" runat="server"></asp.TextBox>
Gets translated into this:
<input id="ct100_ContentPlaceHolder1_txtName" type="text" />
I think this is do to me using master pages, but if so how can I be sure a control will have a certain ID(for javascript purposes). I placed the auto-generated id in my javascript and it is working, but I would prefer to have used the id's that I originally assigned them. Is this possible? (This is for version:ASP.NET 3.5)
View 3 Replies
Nov 3, 2010
I normally use server controls i.e Textbox so i can get access to the server side event.
But what if i don't need access to the server side event and i am going to place some jquery or javascript on the textbox for example.
Can i use a standard HTML (client controls) ?
View 3 Replies
Apr 15, 2010
I want to access a label in templated user control.
I find this code in internet, but it doesn't work.
Error:Object reference not set to an instance of an object.
public static Control FindControl(Control parent, string id)
{
Control recurse;
if (parent.ID == id)
[Code]....
View 4 Replies
Nov 11, 2010
I am fairly new to asp.net programming. I found this custom control that uses ajax to update the contents of the control and it is working perfectly. However, I need to access some of the asp.net labels and images in the databound event so I can set their values/properties appropriately, and am not sure how to go about adding additional code or accessing these in my custom control (I have been googling endlessly with no results). My code for the custom control is below.
Imports System
Imports System.Text
Imports System.IO
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.VisualBasic
Namespace myControls
Public Class AjaxDivView
Inherits CompositeDataBoundControl
Implements ICallbackEventHandler
Private _itemTemplate As ITemplate
''' <summary>
''' The ItemTemplate is used to format each item from the data source.
''' </summary>
<TemplateContainer(GetType(DivViewItem))> _
<PersistenceMode(PersistenceMode.InnerProperty)> _
Public Property ItemTemplate() As ITemplate
Get
Return _itemTemplate
End Get
Set(ByVal value As ITemplate)
_itemTemplate = value
End Set
End Property
''' <summary>
''' Register Javascript
''' </summary>
Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
'Register Refresh Function
Dim eRef As String = Page.ClientScript.GetCallbackEventReference(Me, Nothing, "AjaxDivView_Result", "'" & Me.ClientID & "'", "AjaxDivView_Error", False)
Dim refreshFunc As String = "function AjaxDivView_Refresh() {" & eRef & "}"
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), Me.UniqueID, refreshFunc, True)
MyBase.OnPreRender(e)
End Sub
''' <summary>
''' Iterate through the data items and instantiate each data item in a template.
''' </summary>
Protected Overloads Overrides Function CreateChildControls(ByVal dataSource As System.Collections.IEnumerable, ByVal dataBinding As Boolean) As Integer
Dim counter As Integer = 0
For Each dataItem As Object In dataSource
Dim contentItem As New DivViewItem(dataItem, counter)
_itemTemplate.InstantiateIn(contentItem)
Controls.Add(contentItem)
counter = counter + 1
Next
DataBind(False)
Return counter
End Function
''' <summary>
''' Render this control's contents in a Ul tag
''' </summary>
Protected Overrides ReadOnly Property TagKey() As System.Web.UI.HtmlTextWriterTag
Get
Return HtmlTextWriterTag.Div
End Get
End Property
''' <summary>
''' Render my contents to a string and send the result back to the client
''' </summary>
Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
Dim builder As New StringBuilder()
Dim sWriter As New StringWriter(builder)
Dim hWriter As New HtmlTextWriter(sWriter)
Me.RenderContents(hWriter)
Return builder.ToString()
End Function
''' <summary>
''' Whenever I get called through AJAX, rebind my data.
''' </summary>
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
Me.DataBind()
End Sub
Public Class DivViewItem
Inherits WebControl
Implements IDataItemContainer
Private _dataItem As Object
Private _index As Integer
Public ReadOnly Property DataItem() As Object Implements System.Web.UI.IDataItemContainer.DataItem
Get
Return _dataItem
End Get
End Property
Public ReadOnly Property DataItemIndex() As Integer Implements System.Web.UI.IDataItemContainer.DataItemIndex
Get
Return _index
End Get
End Property
Public ReadOnly Property DisplayIndex() As Integer Implements System.Web.UI.IDataItemContainer.DisplayIndex
Get
Return _index
End Get
End Property
Protected Overrides ReadOnly Property TagKey() As System.Web.UI.HtmlTextWriterTag
Get
Return HtmlTextWriterTag.Div
End Get
End Property
Public Sub New(ByVal dataItem As Object, ByVal index As Integer)
_dataItem = dataItem
_index = index
End Sub
End Class
End Class
End Namespace
View 5 Replies
Sep 29, 2010
Context
I'm putting together a templated, databound control. Presently it works with the following syntax...
<cc:ItemChooserControl ID="ItemChooser" runat="server">
<TitleTemplate>
<h4><%# DataBinder.Eval(Container.DataItem, "DisplayName") %></h4>
</TitleTemplate>
</cc:ItemChooserControl>
Problem
What I would like though is that the shorter, simpler Eval would work instead.
<h4><%# Eval("DisplayName") %></h4>
What I get however when using straight Eval is an error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Code
I'm databinding to a custom HtmlTableCell...
public class TitleTemplateTableCell: HtmlTableCell, INamingContainer
{
private object m_DataItem;
public virtual object DataItem
{
get { return m_DataItem; }
set { m_DataItem = value; }
}
}
With the following custom DataBind (non-related code removed)...
foreach (object dataItem in dataSource) {
TitleTemplateTableCell title = new TitleTemplateTableCell();
TitleTemplate.InstantiateIn(title); // TitleTemplate is the template property
title.DataItem = dataItem;
title.DataBind();
}
View 1 Replies
May 28, 2010
I'm creating a custom templated composite control. If there is no 'ItemTemplate' specified in the mark-up, how do I create a default template programmatically?
View 1 Replies
Jan 27, 2010
I've created a templated user control by following the example on the microsoft support pages. I won't post it all but the important part of the CS file looks as follows:
[Code]....
I'm not sure if the whole _CurrentDataItem business is required, I don't think so.The problem I've got, is that I've tried nesting it inside a FormView control so I can use it to display a record from a database. My ASPX looks like this:
[Code]....
Now, the bound properties (TitleText and TitleSubText) bind correctly, but the nested / template content is blank when the page loads. I'm not sure if this is something to do with the order in which everything is loaded or because I'm missing some code in the SimpleTemplate to get it to bind (etc.).
View 2 Replies
Dec 20, 2010
I am trying to access a control from the code behind in a tmeplated user control and and keep getting an object reference exception.
Is it possible to access these controls. I have tried the find control and just referencing the object i.e. this.someID, non of which worked.
Here is my templated control:
[Code]....
Here is the template ascx code:
[Code]....
Here is the test user control that is using the template:
[Code]....
Here is the codebehind for the test user control:
[Code]....
I have also tried this which throws the same error:
Label label = (Label)this.Page.FindControl( "DateTimeLabel2" );
label.Text = DateTime.Today.ToString();
View 2 Replies
Aug 31, 2010
Basically, I have a custom templated control with a custom data container class. When a developer adds an instance of my control to a page, they can define the controls in the LayoutTemplate however they like, as follows:
<ml:MyControl id="MyControl1" runat="server">
<LayoutTemplate>
<span><%#Container.ErrorMessage%></span>[code]...
I would like my control to automatically move the "ml_errormessage" (databound content from Container.ErrorMessage - hard coded token in the property) to be a class of its containing element to make it easier for jQuery to use a selector to find the element and dynamically insert the error message client side. More importantly, I would like it moved out of the way so the class name doesn't get replaced by the content and jQuery can find it as many times as it needs to during the page lifecycle. In other words, I would like the output to look like this without the developer changing the input template or resort to using custom controls in the template:
<span id="MyControl1"><span>
<span class="ml_errormessage"></span>
</span></span>
I would like to move the value using the controls collection rather than resorting to string parsing of the output, if possible. However, when I interrogate the collection in an override of OnPreRender, the control in the template looks like this in the debugger:
{System.Web.UI.DataBoundLiteralControl}
System.Web.UI.DataBoundLiteralControl: {System.Web.UI.DataBoundLiteralControl} [code]...
As you can see the "ml_errormessage" value is nowhere to be found. Upon analysis of the DataBind event of the Control class using Reflector, I see that it delegates the binding behavior to each control. In other words, each control handles its own databinding. However, since I have no way of knowing in advance what control types will be in the template, how can this change be done?
Note: an acceptable alternative would be to add a new HtmlGenericControl (span) in the exact spot where the "ml_errormessage" is and add it as a class to this new control.On a side note, is there an easy way to get the control's output to indent for easy reading during debugging?
View 1 Replies
Apr 5, 2010
I dont want to use asp.net's TreeView control. I want to create a custom template databound control with multi template support like -
<asp:MtNavigationControl>
<ItemTemplate>
...
...
</ItemTemplate>
<SelectedItemTemplate>
...
...
</SelectedItemTemplate>
<ParentItemTemplate>
...
...
</ParentSelectedItemTemplate>
<SelectedParentItemTemplate>
...
...
</SelectedParentSelectedItemTemplate>
</asp:MtNavigationControl>
My data is like -
class Employee
{
string EmployeeName
List<Employee> Employees
}
Does anyone know how to accomplish it?
View 1 Replies
Sep 9, 2010
I'm trying to run the templated user control example provided by MSDN. Code is as follows:
So according to MSDN this should implement as follows:
[code]...
Designer complains that content is not allowed between the opening and closing tags of TemplatedFirstControl and that FirstTemplate is not supported. So what's missing? I duplicated MSDN's code verbatim
MSDN Article: [URL]
View 1 Replies
Feb 8, 2011
I'm working on a user control that renders couple nested divs (a rounded corner box to be frank). This is a templated user control, which means that user can put any control he wants into the header, body or footer of this box. Everything is working fine, except the fact that server-side controls can't be retrived from my box. Take a look at this code:
[Code]....
Now codebehind of that control:
[Code]....
I have cut out all the unimportant stuff.Now when I use this control:
[Code]....
I want to access controls in page codebehind file (on Page Load): LiteralNewses.Text="";
but compiler returns an error that LiteralNewses doesn't exist. Also FindControl method can't find this literal.Any tips on what's wrong with this code? According to MSDN, setting TemplateInstance to Single ensures that controls from template will be accessible but not in my case...
View 1 Replies
Mar 22, 2011
I am trying to build a custom composite control, which allows me to add custom content to each child control. It's a similar concept to what you have with a GridView and TemplateColumn. The markup used to place the control on the page would end up being something like this:
[Code]....
I have the code below, which allows me to put that markup on the page without throwing any errors, and it renders all the correct HTML, except it doesn't render the contents of the ColumnTemplate. I have replaced the Render() code with a comment because it's quite long winded and doesn't add anything important here:
[Code]....
I have tried to follow examples on MSDN and other forums but I can't make this work. I think I'm missing the code to render the contents of the template, but I don't know how to hook that up.
View 1 Replies
Feb 14, 2011
I am attempting to extract all of my reusable user/server controls from my web application and convert them into an external dll, but I'm running into tons of problems right from the start:Project type - Do I make an ASP.NET Web Application, or a Class Library?External references - How can I contain all external references within the dll itself, so that I can just copy + paste and Add Reference to just 1 DLL instead of 5 or 6?File types - Class Library projects are lacking certain options in the Add New Item window, such as asmx Web Services.
View 2 Replies
Jan 24, 2013
I need to use radio buttons in a datarepeater. The problem is that radio buttons do not get the same ID in repeaters, so cannot be mutually exclusive. One way I read about getting around this is to create my own radio button and just render it differently.
I am trying to create a server control that inherits from the radio button. It needs to support postback and async post backs and have event handlers. The code I have works fine, in the sense each radio button is mutually exclusive but events are not being fired.server control:
Code:
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> Public Class NewRadioButton
Inherits System.Web.UI.WebControls.WebControl
Implements System.Web.UI.IPostBackEventHandler
[code]...
View 1 Replies
Jun 7, 2010
I'm creating a web server control that has an image button on it.The html for the control is done in the RenderControls of the code, the control devrives from WebControl, IScriptControl, INamingContainer.The button is coded as follow in the RenderControls:
System.Web.UI.WebControls.ImageButton img = new System.Web.UI.WebControls.ImageButton();
img.ImageUrl = "Url of the image";
img.Click += new ImageClickEventHandler(img_Click);
img.ID = this.ClientID + "_img";
img.CausesValidation = false;
imgLock.RenderControl(output);
The button apreas in the browser but when i click on it, the page postsback but the event handler for the button doesn't get fired, from what i can figure out, since the control goes throught RenderControls eachtime the page is posted back, the button gets redrawn and the event handling disapears.
View 3 Replies
Jun 4, 2010
Is it possible to create Event (for example Button click event) from Source View? I mean without going to design view ?
View 3 Replies
Feb 21, 2010
I want to create a map control which will be a server side control.I want to have it as a tool in the toolbox. The above control will contain providers that will fetch maps from the following sources:
1) WMS
2) Oracle Spatial DB
3) Google
4) MS Virtual earth.
How to create the providers for the different map sources? Then I hav to create different screens which will take the parameters and send them to the map preoviders. The map providers will send the generated map to a map control (could be a active X control) or something else. The map control should also be capable of performing standard functions like pan,zoom etc. Pls give some idea as well as source code on how to start with it.
View 3 Replies
Dec 31, 2010
i'm developing a webusercontrol, that has very properties, properties are of custome types (not primitive), so i want user can edit properties in grid property. i want something like gridview here is a portion of my code:
[Code]....
[Code]....
View 4 Replies
Jan 18, 2011
Within an existing ASP.NET page I've created the following layout, which is a tabbed header and content area. When the tabs are clicked (using JQuery) I show the relevant content, and hide all the other content for that respective tab, like so...
I'd like to expand this functionality to make it into a User Control, so that I could re-use the code more easily. Once the control is created, I'd like to be able to use it like this:[Code]....
where I can start learning how to do this, or offer direct help on the code? I've read many tutorials for creating controls using existing controls (e.g. custom labels etc) but they are all too simple. With the above, do I need to create a ContentArea control first, then work onto the TabbedControl? How do I get ASP.NET to render out the contents of my ContentArea (including ASP.NET controls that are contained within it)?
View 4 Replies
Aug 11, 2010
I want to create a completely new control instead of extending an existing one. I have found a few articles on the net, but it's not exactly what I'm looking for. I've created Custom User controls in Win Forms before and thought it would be somewhat the same in ASP.NET, but it does not seem so. Many articles talk about a Web Control Library template which doesn't seem to be available when creating a New Project, but is available when adding a New item to an existing ASP.NET forms project in VS2008.
I have extended control by using the ASP.NET Server Control template. But all you get with is a .cs file. How do design a brand new control with that?What I want is to create a control from scratch, with postback functionality, and that compiles to a DLL. Am I going about this all wrong? Should I rather be looking at the ASP.NET Server Control Project Template? If so, How do I create a brand new control with the ASP.NET Server Control?I'm working with VS2008 SP1, .net 3.5, and C#.
View 2 Replies