MVC :: Templated HTMLHelpers - How The View Works?
Oct 15, 2010
I've been reading about it and abstracted both M and C from MVC. My question here is just to help me understend how the View works, here it goes:
I've got 2 tables: List and Item, both of them have repositories and controllers, in the Lists View i want it to be possible for me to add and remove Items. The Item will not have a page of its own, everything will be done in the List Create and Edit page. I tried using EditorFor, DisplayFor, they dont seem right. Is it? How could I do that?
Curiosity: if I added another table: PossibleItems, which contains the possible values for the Items (o rly?) and will have its own page for including possible items, how would it go? This might seem like an exercise from a MVC Tutorial book .
View 4 Replies
Similar Messages:
Oct 26, 2010
I am not sure why this happens, but when I have an Html.TextBoxFor(model => model.SomeObject.SomeProperty), when I post, the property of that object is always null.
When I look at the markup generated, I see <input type="text" name="SomeObject_SomeProperty" id="SomeObject.SomeProperty" value = "" />
If I change the helper to Html.TextBoxFor(model => model.SomeObject.SomeProperty, new {id = "SomeProperty"}) the controller is then able to pick the value up. Why is this happening? I feel as though I shouldn't have to be specifiying the id for the textbox as it works when fine when not using child objects on a view model, IE <%: HtmlTextBoxFor(model => model.SomeProperty) %>.
View 8 Replies
Jun 16, 2010
How do you set the width, height etc of a strongly typed HtmlHelper? This is currently how I am using them :
[Code]....
View 3 Replies
Oct 20, 2010
I have Asp.Net MVC project where I have added area for admin. In the root I have ContentTemplates folder where I have .tt generators for views and they work fine. But when I add view in the ~/areas/admin the views are using default MVC content generators and not my. I decided to add folder with my custom ContentTemplates to ~/areas/admin folder but anyway it uses default MVC contentTamplates.Did anybody this before - I mean using contentTemplates in Areas? If yes then I will check all things again..
View 5 Replies
Sep 2, 2010
I have a master page into which I'm inserting a 'Search' partial view, which I've called 'pSearch.ascx'.
I need to pass in a model to make some data available to pSearch, so I am using RenderAction from the site.master page, which then should call a [ClientActionOnly] Action on the 'Home' controller, called SearchPartial(), which in turn returns the pSearch view.
Code from site.master: <% Html.RenderAction("SearchPartial", "Home"); %>
Code from HomeController.cs: (the method is virtual because I'm using T4MVC in the project)
View 2 Replies
Dec 22, 2010
I've been assigned changes to make to an ASP.NET project. The WebForm I'm working on needs to dynamically display and hide controls in response to user actions.
So far, so good. My approach would be to create a little JavaScript. However, on this page, the markup is part of a template for a Telerik control (which I know nothing about). I'm not sure the ramifications of this. Can I still use JavaScript for templated markup?
I tried to insert some existing JavaScript in the page. The first problem I have is my use of <%= ControlName.ClientID %>, which produces an error because the name of the control in the template is not seen to exist by ASP.NET.
Is there another way to do this, or am I just going to run into more problems?
View 1 Replies
Jan 3, 2011
I have a CheckBoxList that displays a list of category names. Its datasource fields contain the category name, rate, its HTML page identifier "pagea1234.html" among other selected fields.The CheckBoxList's DataTextField = "Name" and the DataValueField = "Rate". I would like the DataTextField - "Name" to display as a hyperlink to its respective html page ie = "www.abc.com/page_A1234.html"
<asp:HyperLink ID="HyperLink101" runat="server" Text='<%# Eval("Name") %>' NavigateUrl='<%#
http://www.abc.com/ + Eval("Page_ID") + ".html" %>' Target="_blank" Font-Size="11"></asp:HyperLink>
Can the CheckBoxList's DataTextField be templated with a hyperlink similar to the one above?
View 1 Replies
Jul 15, 2010
I wanted to disable a button after it is clicked and at the same time fire the post back event to generate a report. My first set of code did not work because soon after the button is disabled the page won't submit/post back. here's the first set of code which was not implemented. the onclientclick calls a javascript function which has these lines
document.getElementById('btnGenerateReport').disabled=true;
GetPostBackEventReference(btnGenerateReport,'');
since it was not posting back,i tried the following on page_load code behind
btnGenerateReport.Attributes.Add("onclick", "this.disabled=true;" + ClientScript.GetPostBackEventReference(btnGenerateReport, ""))
that worked well. but I tried to copy the javascript that got generated and pasted directly on design view
onclick="this.disabled=true;__doPostBack('btnDownloadClientsWithConviction','');"
its not working from client side alone after I disable the code behind attributes.add
but when I check the view source the 2 pages are the samewhy am I not able to move the code from code-behind to design view?
View 1 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
Mar 13, 2011
I want to read from database without using templated control like gridview, listview ...etc I am being able to read from db, but shows only the first row;
My .aspx pgae is;
<div>
<asp:DropDownList
ID="ddlCustomer"
runat="server"
AutoPostBack="false"
OnSelectedIndexChanged="ddlCustomer_SelectedIndexChanged"
DataTextField="CustomerName"
DataValueField="CustomerID">
</asp:DropDownList>
<div>
<asp:Button ID="btnGetCustomers" runat="server" Text="Button" OnClick="btnGetCustomers_Click" />
</div>
<div>
<asp:Label ID="lblOrderDate" runat="Server" style="border:1px solid #ccc;" width="100"/>
<asp:Label ID="lblOrderName" runat="Server" style="border:1px solid #ccc;" width="100"/><br />
</div>
and code-behind;
[Code]....
View 5 Replies
May 25, 2010
Can I seek some advice here if I want to incorporate some automated scaffolding in my MVC 2 web application what is the best way to go? Should I be considering: MVC 2 Templated Helpers, or Dynamic Data (with support for Entity Framework) Roughly speaking I want to layout my own website layout in MVC 2 but then:
for high usage use cases custom define the page, for CRUD maintenance pages hook the website menu to point to some scaffolded auto-generated pages (i.e. it is for this item that I ask the above question). I've had a look at the Dynamic Data pages where you can add/edit/delete all aspects of an EF entity, so it's this type of thing I'm after (e.g. have an Edit Person menu link that when click launches an auto maintenance page for Person)
View 1 Replies
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
Jan 30, 2010
In my web application I want to log certain user interactions etc, like "User ABC joined Group XYZ" Therefore I want to set up a logging mechanism that logs into a Mssql database. I'm now trying to find a good database design to achieve flexibility.
First of all, I don't want to log strings like "User ABC joined Group XYZ". I'd like to separate the content "ABC" and "XYZ" from the template. So I'm looking for a templated logging database.
Something like this:
table: UserLog
IDLog int primary key [code]......
I think this database structure would give me enough flexibility to add/remove/edit specific log types (like 'User ABC did something else'). For example I might want to add some information to a specific log entry for future entries. Therefore I create a new UserLogTemplate that references the same UserLogType. That is how I would achieve downward compatibility.
Is that a database design that will work?
View 2 Replies
Feb 14, 2011
I have a gridview and one of the fields is a template field. The item template is a label and the edit template is a drop down box that all works lovely.in the Gridview1_RowUpdating event I would like to compare the original value for this template field with the new value (see if it goes from Pending to Approved). but I am struggling to find the old value for this field. I could save it to a module level variable in the GridView1_RowDataBound when I set the row to 'edit' but I think there is a better idea out there. I have tried to access the datarowview but it keeps coming back as Nothing e.g. This retuns nothing:
[Code]....
Does anyone have any ideas other than recording the data in the RowDataBound event please?
View 3 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
Nov 2, 2010
I have a databound grid with a templated field column that is a checkbox. When the user checks the checkbox, it will autopostback and update the row to indicate the box's check state.
My first attempt was to simply use the OnCheckedChanged method, but when this gets called, I have no way to know which row the checkbox came from. Thus, I don't know which row to update.
Can anyone suggest a method to determine which row the checkbox that fired the oncheckedchanged event came from? Or can you suggest a better way to achieve what I need to do?
I cannot bind the checkbox to the data column because I do not want the checkbox disabled in select mode, and do not wish to require the user to enter edit mode to change the value. I also need all rows to be editable. So, the best route seems to be in a templated column.
View 2 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
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
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
Jul 14, 2010
I have a GridView and the second column (ie column 1) is:
[Code]....
On the GridView's SelectedIndexChanged event I have:
[Code]....
fullname is a Label alright, but its Text property is an empty string. How do I get the actual string that is shown in the cell?
View 9 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