Using a repeater with Entity Framework - c#

How can I use a repeater with Entity Framework
<%# Page Title="" Language="C#" MasterPageFile="~/Admin.master" AutoEventWireup="true" CodeBehind="TagOp.aspx.cs" Inherits="canta.TagOp" %>
<%# Import Namespace="canta.POCO.Objects" %>
<asp:Content ID="Content1" ContentPlaceHolderID="AdminContentPlaceHolder" runat="server">
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="EntityDataSource1">
<ItemTemplate>
<div>
<%#(Container.DataItem as TagObject).IconPath %>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=EnCantamEntities"
DefaultContainerName="EnCantamEntities" EnableFlattening="False"
EntitySetName="Tags">
</asp:EntityDataSource>
</asp:Content>
The code above throws a NullReferenceException
This was my bad.
TagObject was my business layer and i tried to load it like it's an entity object.
I first loaded my data to List<TagObject> then binded the list to repeater.Then my problem solved.Guess I was still sleeping when i wrote that lol :)

This was my bad. TagObject was my business layer and i tried to load it like it's an entity object. I first loaded my data to List<TagObject> then binded the list to repeater.Then my problem solved.Guess I was still sleeping when i wrote that lol :)

Related

Ordered list using repeater in ASP.net

The code below keeps giving me an error:
Repeater code in .ASPX file
<asp:Repeater ID="rptDescription" runat="server">
<HeaderTemplate><ol class="DescriptionRepeater"></HeaderTemplate>
<ItemTemplate>
<li>
<%= this %>
</li>
</ItemTemplate>
<FooterTemplate><ol/></FooterTemplate>
</asp:Repeater>
Code to populate the repeater in .ASPX.CS file
im using visual studio 2017 the code is causing the ordered list to be filled with the path to the .aspx file
List<string> lstDescription = new List<string>();
lstDescription.Add("this is the first description");
lstDescription.Add("this is the second description");
rptDescription.DataSource = lstDescription;
rptDescription.DataBind();
It is not a practical scenario. Anyhow, you just use Container.DataItem, since DataItem itself is a string. Please make sure you use binding syntax <%# %> instead of <%= %>.
<asp:Repeater ID="rptDescription" runat="server">
<HeaderTemplate>
<ol class="DescriptionRepeater">
</HeaderTemplate>
<ItemTemplate>
<li>
<%# Container.DataItem %>
</li>
</ItemTemplate>
<FooterTemplate>
<ol />
</FooterTemplate>
</asp:Repeater

UserControl in asp .net is not working?

I have created a two usercontrols in asp.net and one webform . Now i want these two usercontrols to show in form in webform but it say that there must be one head with runat="server"
this is webform where i am using UserControl!
<%# Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Light.master" CodeBehind="AdministrationPage.aspx.cs" Inherits="DXApplication5.AdministrationPage" %>
<%# Register src="~/AdminPage/AssignmentTab.ascx" tagname="AssignmentUC" tagprefix="uc1" %>
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
<table border="0">
<tr>
<td>
<div class="accountHeader">
<uc1:AssignmentUC ID="CalendarUserControl1" runat="server" />
</div>
</td>
</tr>
</table>
</asp:Content>
This is UserControl below:
<%# Control Language="C#" ClassName="AssignmentUC" AutoEventWireup="true" CodeBehind="AssignmentTab.ascx.cs" Inherits="DXApplication5.AdminPage.AssignmentTab" %>
I would add a single form to your masterpage, this may be the cause of your error.
I would also remove all other form server controls from your user controls and pages.
Try these steps:
Go to Light.master master page file and make sure that this is in there somewhere <form id="form1" runat="server"> and a closing tag, the id may be different.
Go to the following files AssignmentTab.ascx and AdministrationPage.aspx and remove any <form id="form1" runat="server"> and closing tags </form>
Check user control code if it contains a head element. Also check all dependencies to see if there are head elements present in them.
I think you have use <form id="formID" runat="server"> in both of your page and user Control .
Just remove runat="server" from your user Control Form tag .
Make sure you have in your user control cs file:
public partial class WebUserControl1 : System.Web.UI.UserControl
In ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebUserControl1" %>
In aspx parent:
<%# Register Src="WebUserControl1.ascx" TagPrefix="uc" TagName="WebUserControl1 " %>
<uc:WebUserControl1 runat="server" ID="mycontrol" />

Setting node.PropertiesAsList as Datasource for a repeater

I'm trying to set the PropertiesList of a node as the DataSource for my repeater.
rptDistributors.DataSource = node.PropertiesAsList;
rptDistributors.DataBind();
And in my repeater I try to get the umbDistributorCountry.
<asp:Repeater ID="rptDistributors" runat="server">
<%# Eval("umbDistributorCountry") %>
</asp:Repeater>
However I run into problems because it doesn't know any of the properties.
DataBinding: 'umbraco.NodeFactory.Property' does not contain a property with the name 'umbDistributorCountry'.
The content of the list looks like the following:
Any ideas?
Thanks,
Thomas
The clue is in the error ...
DataBinding: 'umbraco.NodeFactory.Property' does not contain a property with the name 'umbDistributorCountry'.
umbDistributorCountry is not a .NET property, but the value of the property called Alias. An Umbraco property contains the three .NET properties in your screenshot, so you only have access to these ...
<%# Eval("Alias") %>
<%# Eval("Value") %>
<%# Eval("Version") %>
Assuming you want to show all the Umbraco properties of that particular distributor (which is stored in node, I guess), you would need something like this ....
<asp:Repeater ID="rptDistributors" runat="server">
<ItemTemplate>
<%# Eval("Alias") %> : <%# Eval("Value") %> <br />
</ItemTemplate>
</asp:Repeater>

Getting unknown error regarding dll file

When I try to run my application, I am getting an unknown error. The error message is as below.
Error 1 The type
'ASP.usercontrols_21_0_unscheduled_angio_event_sheet_ascx'
exists in both
'c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET
Files\nanostentv02\f1f02035\c7751ed\App_Web_zxaxdb4a.dll' and
'c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET
Files\nanostentv02\f1f02035\c7751ed\App_Web_4odgbrwn.dll' C:\Inetpub\wwwroot\NanoStentV02_PL\CRO\21_0_UNSCHEDULED_ANGIO_EVENT_SHEET.aspx 12
The page code for 21_0_UNSCHEDULED_ANGIO_EVENT_SHEET.aspx 12 is as below.
<%# Page Title="" Language="C#" MasterPageFile="~/_PL/CRO/_CRO.master" AutoEventWireup="true"
CodeFile="21_0_UNSCHEDULED_ANGIO_EVENT_SHEET.aspx.cs" Inherits="_PL_CRO_21_0_UNSCHEDULED_ANGIO_EVENT_SHEET" %>
<%# Register Src="../../_UserControls/_21_0_UNSCHEDULED_ANGIO_EVENT_SHEET.ascx" TagName="_21_0_UNSCHEDULED_ANGIO_EVENT_SHEET"
TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ChildContent1" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ChildContent2" runat="Server">
<asp:HiddenField ID="hdnPatientID" runat="server" />
<asp:HiddenField ID="hdnPatientCode" runat="server" />
<asp:HiddenField ID="hdnCEType" runat="server" />
<uc1:_21_0_UNSCHEDULED_ANGIO_EVENT_SHEET ID="_21_0_UNSCHEDULED_ANGIO_EVENT_SHEET1"
runat="server" />
</asp:Content>
Earlier it was working fine but suddenly it stopped working.
Try to clean solution in Visual Studio and build project again. Libraries cached
check these
CodeFile="21_0_UNSCHEDULED_ANGIO_EVENT_SHEET.aspx.cs"
and
<%# Register
Src="../../_UserControls/_21_0_UNSCHEDULED_ANGIO_EVENT_SHEET.ascx"
may be there some assembly name conflict. Try with changing the name of one of them. again Rebuild the solution.

I am having an issue with ASP .NET user controls

I have an issue currently that I can't resolve. I have a user control called "Dashboard" which then has the following markup, containing several subcontrols.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Dashboard.ascx.cs" Inherits="BlueSEQ.Controls.Dashboard.Dashboard" %>
<%# Register src="Administrator.ascx" tagname="Administrator" tagprefix="uc1" %>
<%# Register src="Provider.ascx" tagname="Provider" tagprefix="uc2" %>
<%# Register src="User.ascx" tagname="User" tagprefix="uc3" %>
<% if (isAdministrator)
{ %>
<uc1:Administrator ID="Administrator1" runat="server" />
<% }
else if (isProvider)
{ %>
<uc2:Provider ID="Provider1" runat="server" />
<% }
else
{ %>
<uc3:User ID="User1" runat="server" />
<% } %>
As you can see, I want it to display some controls or other controls depending on some conditions. However, all of these controls' "Load" event get triggered, even if they are not used.
How can I prevent this?
If you can help it, try to avoid having conditional logic in your markup. It could make the views somewhat more difficult to understand for designers (if you're working with designers) and more difficult to find and refactor this code in the future.
You should also take a look at ASP.NET MVC: Avoiding Tag Soup. Although it's ASP.NET MVC, it's still a good example of how adding logic to your views can quickly make them very difficult and unpleasant to maintain (initial example).
You could use the technique described here: How to: Add Controls to an ASP.NET Web Page Programmatically.
Your markup would look something like this.
<asp:PlaceHolder id="MyPlaceholder" />
and your codebehind would have something along the lines of
private void InitSection()
{
Control c;
if( isAdministrator )
c = Page.LoadControl("~\Administrator.ascx")
else if( isProvider )
c = Page.LoadControl("~\Provider.ascx")
else
c = Page.LoadControl("~\User.ascx");
MyPlaceholder.Controlls.Add(c);
}
The ideal way to do this is to set up asp.net role provider and use a LoginView control, something along the lines of the code below. LoginView only loads the appropriate content.
<asp:LoginView runat="server">
<AnonymousTemplate>
<uc1:User ID="User" runat="server" />
</AnonymousTemplate>
<RoleGroups>
<asp:RoleGroup Roles="Administrator">
<ContentTemplate>
<uc1:Administrator ID="Administrator1" runat="server" />
</ContentTemplate>
</asp:RoleGroup>
<asp:RoleGroup Roles="Provider">
<ContentTemplate>
<uc1:Provider ID="Provider" runat="server" />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
You have to Load the control on a specific condition instead, so try to set visible/invisible with the usercontrol, that's a much better approach
<% if (isAdministrator)
{ %>
Page.LoadControl(("~\Administrator1.ascx");
<% }
How about using a MultiView control?
MultiView on MSDN

Categories

Resources