Strong typed Container in WebForms - asp.net

Take following example of code: (ASP.NET WebForms)
<asp:Content ContentPlaceHolderID="Contents" runat="server">
<div class="blogpost-list">
<asp:Repeater ID="blogList" runat="server">
<ItemTemplate>
<h2 class="blogpost-title">
<%# (Container.DataItem as BlogPost).Title %>
</h2>
<p class="blogpost-meta">
</p>
<p class="blogpost-content">
<%# (Container.DataItem as BlogPost).ParsedContent %>
</p>
</ItemTemplate>
</asp:Repeater>
</div>
</asp:Content>
Now what I want to do, is to avoid the content casting of the DataItem, ie. this line:
<%# (Container.DataItem as BlogPost).Title %>
I'm feeling inspired of the ASP.NET MVC, and was wondering if I could create a strong typed, view, and define it like:
<%# Page
Language="C#" MasterPageFile="~/Blog.Master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="MyBlog.Default<MyStrongViewType>"
%>
Or any other way to avoid typecasting, and in general, have a strong typed view for ASP.NET WebForms.
Any good ideas?

.NET 4.5 has a nifty solution for this. You just set the datatype you want to use on the repeater itself.
<asp:Repeater ID="blogList" runat="server" ItemType="BlogPost">
<ItemTemplate>
<h2 class="blogpost-title">
<%# Item.Title %>
</h2>
<p class="blogpost-meta">
</p>
<p class="blogpost-content">
<%# Item.ParsedContent %>
</p>
</ItemTemplate>
</asp:Repeater>
See:
http://weblogs.asp.net/scottgu/archive/2011/09/02/strongly-typed-data-controls-asp-net-vnext-series.aspx
http://www.asp.net/web-forms/videos/aspnet-web-forms-vnext/aspnet-vnext-videos-strongly-typed-data-controls

This might be a bit late, but this seems a nice solution:
http://dotnetminute.blogspot.com/2012/04/creating-strongly-typed-repeater.html

I think you can inherit from Repeater control and make it generic (e.g. Repeater). But you'll need to rewrite/inherit RepeaterItem class too.
You can use this or this article as example.

Related

is it possible to see one user control on other user control?

I got one problem, I have three user controls say Control1,control2, control3.
I want to make Control1 into 3 divs and see contol1 UI in first part
control2-2nd part of div
Control3- in 3 Part of div
Is it possible?
I wanted to do this just for the sake i can maintain with less code .Is there any alternative .other this which will suit me best.
Please suggest
Yes, you can put multiple user control on a single user control. However your question is down voted the solution of your problem is as given below :
Create a sample website. Add Webusercontrol1.ascx ,Webusercontrol2.ascx, Webusercontrol3.ascx, Webusercontrol4.ascx in it and modify the code as given below.
Default.aspx page html
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="SO_1._Default" %>
<%# Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
</asp:Content>
UserControl-1 Html : This is container user control and this will have all required user control on it.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
Inherits="SO_1.WebUserControl1" %>
<%# Register Src="WebUserControl1.ascx" TagName="WebUserControl2" TagPrefix="uc1" %>
<%# Register Src="WebUserControl2.ascx" TagName="WebUserControl2" TagPrefix="uc2" %>
<%# Register Src="WebUserControl3.ascx" TagName="WebUserControl3" TagPrefix="uc3" %>
<%# Register Src="WebUserControl4.ascx" TagName="WebUserControl4" TagPrefix="uc4" %>
<p>
<b>DIV-1 Container User control</b></p>
<div>
<b>DIV1</b>
<br />
<uc2:WebUserControl2 ID="WebUserControl21" runat="server" />
</div>
<div>
<b>DIV2</b>
<br />
<uc3:WebUserControl3 ID="WebUserControl31" runat="server" />
</div>
<div>
<b>DIV3</b>
<br />
<uc4:WebUserControl4 ID="WebUserControl41" runat="server" />
</div>
UserControl-2 html
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="SO_1.WebUserControl2" %>
User Control-1
UserControl-3 html
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="SO_1.WebUserControl2" %>
User Control-2
UserControl-4 html
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="SO_1.WebUserControl2" %>
User Control-3

How to create a Asp.net Mvc3 application with TelerikExtension controls

I have develop small Asp.net MVC3 application using Telerik rad Controls with in that i have try to get the list of data for that i have create strong type view control,it is create all the controls and i run the application it showing error like Object Reference null Exception
in my database i have 10 records then why it shoeing the error please help me ...here i have post my code please refer this once.
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TelerikMvcApplication1.Models.tb1_post>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<fieldset>
<legend>tb1_post</legend>
<div class="display-label">post</div>
<div class="display-field"><%: Model.post %></div>
<div class="display-label">postdate</div>
<div class="display-field"><%: String.Format("{0:g}", Model.postdate) %></div>
<div class="display-label">username</div>
<div class="display-field"><%:Model.username %></div>
</fieldset>
<p>
<%: Html.ActionLink("Edit", "Edit", new { id=Model.postid }) %> |
<%: Html.ActionLink("Back to List", "Index") %>
</p>
</asp:Content>
The error is happening at the first reference to the model, therefore the Model is null. Make sure that the MVC controller is properly instantiating the model for the view.

ASP.NET Repeater Eval

I am trying to execute the following code in a .aspx page:
<asp:Repeater ID="rptComentarios" runat="server">
<ItemTemplate>
<% if (Convert.ToInt32(Eval("int_tipo")) == 1)
{ %>
<div class="resp">
<div class="top">
</div>
<div class="cont-resp">
<h3>
<%# Eval("txt_nome") %></h3>
<p>
<%# Eval("txt_comentario") %></p>
</div>
</div>
<% }
else
{%>
<div class="usuario">
<div class="top">
</div>
<div class="cont-usuario">
<h3>
<%# Eval("txt_nome") %></h3>
<p>
<%# Eval("txt_comentario") %></p>
</div>
</div>
<% } %>
</ItemTemplate>
</asp:Repeater>
It throws a runtime exception in the first line:
<% if (Convert.ToInt32(Eval("int_tipo")) == 1)
System.InvalidOperationException: Databinding methods such as Eval(), XPath() and Bind() can only be used in the context of a databound control.
What's wrong? Any ideas?
I had a similar problem and the following code worked for me:
<asp:Repeater ID="rptComentarios" runat="server">
<ItemTemplate>
<asp:PlaceHolder ID="placeholderBlaBlaBla" runat="server" Visible='<%# Convert.ToInt32(Eval("int_tipo")) == 1 %>'>
Your optional HTML
</asp:placeholder>
Other HTML
</ItemTemplate>
</asp:Repeater>
Some more comments:
Please note that single quotes are used to define the value Visible attribute of asp:placeholder. I tried double quotes too and they didn't work.
Anytime you want to get some optionally displayed HTML you should use a control to show/hide it. asp:placeholder works fine for that purpose. Don't ever do <% if(..) { %> - this is evil.
<%# ... %> is used to calculate or display expressions inside a repeater. These expressions can be displayed as HTML or passed as attributes of server side controls. You can't use if inside it.
I think there needs to be a # sign for the enclosure <%# ..Eval...%>
Or try the full Eval version
<%# if (Convert.ToInt32(DataBinder.Eval(Container.DataItem, "int_tipo"))
== 1) { %>

tab control item contain individual pages

My tab control items contain three different pages.Clicking on tabitem ,they are visible,but when want to perform javascript event on page item then problem arise.javascript well works for only first tab control item,rest of them are not work.Show me the bellow error.
Microsoft JScript runtime error: 'this.GetStateInput().value' is null or not an objec
tabcontrol
**tab item1 contain page1
tab item2 contain page2
tab item3 contain page3**
i write javascript on page1 control,it's work well but rest of pages javascript show the above error message. i work on devexpress control
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UCCharge.ascx.cs" Inherits="WebCore.UserControls.ChargeSettings.UCCharge" %>
<%# Register assembly="DevExpress.Web.v9.1, Version=9.1.2.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073" namespace="DevExpress.Web.ASPxTabControl" tagprefix="dxtc" %>
<%# Register assembly="DevExpress.Web.v9.1, Version=9.1.2.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073" namespace="DevExpress.Web.ASPxClasses" tagprefix="dxw" %>
<%# Register src="UCConfig_Charge_Company_Wise.ascx" tagname="UCConfig_Charge_Company_Wise" tagprefix="uc1" %>
<%# Register src="UCConfig_Charge_Depository_Company_Wise.ascx" tagname="UCConfig_Charge_Depository_Company_Wise" tagprefix="uc2" %>
<%# Register src="UCconfig_charge_operation_mode.ascx" tagname="UCconfig_charge_operation_mode" tagprefix="uc3" %>
<%# Register src="../InvestorAccount/UCconfig_Investor_Account_Wise_Charge.ascx" tagname="UCConfig_Investor_Account_Wise_Charge" tagprefix="uc4" %>
<table>
<tr>
<td>
<dxtc:ASPxPageControl Width="500px" ID="ASPxPageControl1" runat="server" ActiveTabIndex="0"
EnableCallbackCompression="True" EnableHierarchyRecreation="True"
AutoPostBack="True">
<TabPages>
<dxtc:TabPage Text="Charge Company">
<ContentCollection>
<dxw:ContentControl ID="ContentControl1" runat="server">
<uc1:UCConfig_Charge_Company_Wise ID="UCConfig_Charge_Company_Wise" runat="server" />
</dxw:ContentControl>
</ContentCollection>
</dxtc:TabPage>
<dxtc:TabPage Text="Charge Depository Company">
<ContentCollection>
<dxw:ContentControl ID="ContentControl3" runat="server">
<uc2:UCConfig_Charge_Depository_Company_Wise ID="UCConfig_Charge_Depository_Company_Wise"
runat="server" />
</dxw:ContentControl>
</ContentCollection>
</dxtc:TabPage>
<dxtc:TabPage Text="Investor Charge ">
<ContentCollection>
<dxw:ContentControl ID="ContentControl5" runat="server">
<uc4:UCConfig_Investor_Account_Wise_Charge ID="UCConfig_Investor_Account_Wise_Charge"
runat="server" />
</dxw:ContentControl>
</ContentCollection>
</dxtc:TabPage>
<dxtc:TabPage Text="Charge Operation Mode ">
<ContentCollection>
<dxw:ContentControl ID="ContentControl4" runat="server">
<uc3:UCconfig_charge_operation_mode ID="UCconfig_charge_operation_mode"
runat="server" />
</dxw:ContentControl>
</ContentCollection>
</dxtc:TabPage>
</TabPages>
</dxtc:ASPxPageControl>
</td>
</tr>
</table>
I read on a Support issue of a user getting this error, in his case it was because he was using value (myTextBox.value) when getting the value of a DX Control instead of the GetValue() (myTextBox.GetValue()) client method DX provides. Could this be the same thing happening in your code as well?

Giving a custom UserControl an ID in rendered HTML

When I use a ASP:Calendar control, and give it an ID:
<asp:Calendar runat="server" ID="MyCal" />
It looks like this in the rendered html:
<table id="NameMangled_MyCal"... />
And I can access the element by ID in javascript like this:
var cal= document.getElementById("<%= MyCal.ClientID%>")
However, When I make a custom user control that has-a calendar:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WeeklyEventsCalendar.ascx.cs"
Inherits="WeeklyEventsCalendar" %>
<div>
<asp:Calendar runat="server" ID="InnerCal" Width="100%" OnDayRender="RenderCell" OnVisibleMonthChanged="ChangeMonth" Height="480px" />
</div>
And then give it an ID when I add it to my page...
<mvs:WeeklyEventsCalendar ID="WeeklyCal" runat="server" />
That ID doesn't show up anywhere in the rendered HTML. all I get is
<div> stuff </div>
When I want
<div id="NameMangled_WeeklyCal"> stuff <div>
What am I doing wrong?
UserControls only render their contents, nothing else. What you could do is
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WeeklyEventsCalendar.ascx.cs"
Inherits="WeeklyEventsCalendar" %>
<div id="<%= this.ControlID %>">
<asp:Calendar runat="server" ID="InnerCal" Width="100%" OnDayRender="RenderCell" OnVisibleMonthChanged="ChangeMonth" Height="480px" />
</div>

Resources