C# - How To Programmatically Create A Default Template For A Templated Control
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?
implementing login control programmatically using sql server 2005. can anyone give me good web reference about the topic? i am new to this development.
development tool i am using:
>visual studio 2008 > sql server management studio 2005 >windows 7 ultimate(32 bit)
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
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.
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(); }
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.).
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:
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:
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:
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?
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.
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
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...
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.
I have datagrid and inside this grid have template field. I add Header text progr. like this
gvData.HeaderRow.Cells(8).Text = "Hi" But when I do this I can not sort this column when add SortExpression="Hi", i not have clickable header. How can I do this
Does anyone have an example for how to create a Dynamically Templated Listview with prefernces page to specify which columns & column order)? Also the listview would also have Edit, delete and insert options if possible. And uses the n-tier approach with Bus Layer and does NOT use LINQ.
i want to write a page contain an datalist and several ObjectDataSource. The datalist will chose ObjectDataSource according to QuerryString passed to that page.
I have an ASP.NET website application, and there is a home page for my web site. I need to be able to change the default document of my website programmatically (C#) so that I can make another web page take priority above the one that already exists. I would then like to revert back to the previous default document order.
Example :
I have two home pages - Home1.aspx and Home2.aspx. In the IIS default document settings I have added the two pages and made Home1.aspx be the first default document then Home2.aspx the second. I need in some cases to be able to change the order of the two default documents so that Home2.aspx is the first default document then Home1.aspx the second.
Where can I find the default template used by EditorForModel? I'd like to use it as a starting point for customization. All I could find about it wasthis post from last year, but something must have changed since that was written because the Object.ascx template given doesn't work with client side validation.
In a controller action function, we can add a create/edit/list/details view by right clicking. The content of aspx/ascx will be generated according to selected model.
my question is how to change the template in order to generate my customized create/edit/list/details view.
I'm playing around and created a new Web Form project and trying to make way with the default template. I've created the pages I need but trying to leverage the existing membership pages and ASPNETDB. I'm having trouble grasping how the asp pages are connecting to the database(and thus make my own changes). I cannot see any SQL syntax in the aspx pages or the code behind? I can tell the project is making use of the stored procedures but where is the actual connection configured or the command that is being run, for example when the "create" button is pressed on the CreateUserWizard control. Looking at the createUserWizrd I cannot see any adapters it might be using?
Perhaps my mindset is trying to apply, tableadapter and datasets way of working to asp controls. All the information and tutorials I'm looking at, seem to bypass the connection to the database and somehow there controls are 'taking care' of the I/O to the database. So if I wanted to add an additional field to the createwizard process, what do I need to do? Alter the db and the stored procedure, then?
I have a gridview to which I'm adding template fields programmatically. Each of the template fields have a textbox. I would like to make this text box have 2-way binding to a database column. see below code.
[Code]....
I'm calling the above class as follows
[Code]....
How can I make the text box such that when I edit the text, this change is reflected in the database as well?