Dynamic data in Repeater - c#

in my webpage different users can visit same profile, but users can be ither Owner of a profile, or Visitors, and content have to change for those two conditions.
if visitor check profile he see this editor:
<asp:Repeater id="UserLoginRepeater" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server">label Text</asp:Label> <asp:Label runat="server">label text 2</asp:Label>
</br>
<asp:Button runat="server" Text="TestButton"/>
</br>
<asp:Label ID="Label3" runat="server">Test</asp:Label> <textarea><%# Eval("Content") %></textarea>
</br>
</br>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
codebehinde:
if User is Owner of a profile TextArea have to be replaced by a label and an other label with textbox have to be added.
I found couple of possible implementations of this on internet, but they are not so easy to maintain. for example
http://tinyurl.com/9764eys
What would be the best way to Load controls dynamicly into Repeater?

Use a <asp:ContentPlaceholder> then in your code behind add a handler for the OnItemDataBound event. If you want those controls to handle events, make sure you bind your repeater before OnPreRender for the page.

Why not just create two repeaters for two conditions?
Just bind the repeater for the specific user and hide the other one.

Made Dynamic load of Controllers in code-behinde.

Related

Accessing child control in ASP.NET

I am trying to find out if it's possible to have a control inside another control in ASP, like this:
<asp:FormView ID="FormView1" runat="server" Width="630px" Height="496px">
<ItemTemplate>
<asp:Literal ID="ID" runat="server">Idnumber: </asp:Literal><%#Eval("ID") %>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:HyperLink ID="ID" runat="server"> <%# Eval("FILE") %> </asp:HyperLink>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:FormView>
Can I access DataList1 control? I have been trying, but I can't figure it out, I should be able to access nested controls, but I cant get it to do it.
Controls inside Template tags cannot be directly accessed in code behind. Instead you should use FindControl method:
var dataList1 = (DataList)FormView1.FindControl("DataList1");
Note that this might not work in early stages of page life cycle (not until Page_Load I believe).

Displaying "Empty" record templates in ASP.NET Listview

I'm currently coding a web application using ASP.NET. So far my use of ASP.NET and the application has been going great, however there is one feature I would love to have, but I cannot for the life of me figure out how to do it.
I want to use some sort of list/form view, which will show both data records and empty blank templates. For example, I want to use this as an "event booking" system, where the user would click an open slot and this would then be registered to our database. This open slot is the empty template I would like.
I've done a quick paint picture to illustrate what I mean:
I assume that I'm in the right direction using a ListView, but I would like some feedback/help on a better method to achieve this.
This is my listview code (it is somewhat basic, barebones almost):
<asp:ListView ID="LV_AvailableUpdateSlots" runat="server" ItemPlaceholderID="itemPlaceholder">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<EmptyDataTemplate>No data for this date.</EmptyDataTemplate>
<ItemTemplate>
<asp:Panel ID="Pnl_AvailableSlot" runat="server" CssClass="UpdateDiarySlotPanel">
<div class="FloatLeftInline">
<asp:Label ID="Lbl_ClientName" runat="server" Text="Name: "></asp:Label>
<asp:Label ID="Lbl_ClientNameEval" runat="server" Text='<%# Eval("RefID") %>'></asp:Label>
</div>
<asp:Label ID="Lbl_ClientProductEval" runat="server" Text='<%# Eval("ClientMainProductID") %>'></asp:Label>
<div class="FloatRightInline">
<asp:Label ID="Lbl_Version" runat="server" Text="Version: "></asp:Label>
<asp:Label ID="Lbl_VersionEval" runat="server" Text= '<%# Eval("Version") %>'></asp:Label>
</div>
</asp:Panel>
</ItemTemplate>
<InsertItemTemplate>
Insert here.
</InsertItemTemplate>
<EmptyItemTemplate>No data found.</EmptyItemTemplate>
</asp:ListView>
A ListView sounds good, but I does not hurt to take a look at Repeater
Repeater Web Server Control Declarative Syntax
If you have the time do a simple test implementing using a ListView and a Repeater to compare the benefits and see which one fits better your needs and appearance

Design Advice using Master Pages

Master Page
<div id="header" style="height: 150px; width: 750px;">
<asp:Label ID="Label3" runat="server" Text="LoggedInUser:"></asp:Label>
<asp:Label ID="lblLoggedInUser" runat="server" Text=""></asp:Label>
</div>
<div id="leftMenu" class="leftmenu">
<br />
<asp:DropDownList ID="ddlFamilyMembers" runat="server"
style="height: 25px; width: 125px" DataTextField="FullName"
DataValueField="MembershipGen"
onselectedindexchanged="ddlFamilyMembers_SelectedIndexChanged"
AutoPostBack="True" >
</asp:DropDownList>
<br /><br />
<asp:Image ID="imageMember" class="space" runat="server" Height="150px" Width="125px" />
<br /><br /><br />
<asp:Label ID="Label1" runat="server" class="space" Text="MembershipID:"></asp:Label>
<asp:Label ID="lblMembershipID" runat="server" Text=""></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" class="space" Text="Name:"></asp:Label>
<asp:Label ID="lblMemberName" runat="server" Text=""></asp:Label>
<br /><br /><br />
<asp:LinkButton ID="lbInformation" class="space" runat="server" onclick="lbInformation_Click">Member Information</asp:LinkButton><br />
<asp:LinkButton ID="lbAddress" class="space" runat="server"
onclick="lbAddress_Click">Member Address</asp:LinkButton>
</div>
<div id="divRight" class="divright">
<asp:ContentPlaceHolder ID="CPHMain" runat="server">
</asp:ContentPlaceHolder>
</div>
I posted my masterpage code so you can better understand my issue. Since these controls are in my master page I needed to create public properties for all the controls in my masterpage to databind my content pages. Maybe I am approaching this wrong but this is how I was planning on achieving this.
ContentPage
Default.aspx page_load event would call my method to retrieve the data and set the dropdownlist, image, 2 label fields. On selected index change of dropdownlist it would grab the new values from another method and populate those controls accordingly.
To access these controls in my master page i read you can do it a couple of different ways one is to <%#MasterType VirtualPath="~/Member.Master" %> and then create a strongly typed connection. Or you could create a loosely type connection.
My problem with these ways as I am going to have over 15 content pages and I would hate to have to rebind the DropDownList every time one of the content pages is called. Also I would have to reinstantiate the controls in every method of my content pages which I assume I am doing this wrong.
Can someone give me some advice on what is the proper way to achieve this without so much repetitive code?
You've got the right idea, trying to decouple the parent from the child, but I think the way you want to do it may be clunky.
Assuming you can do this, what I'd do is as follows:
Make a public method BindFamilyMembers(string parameter) on the Master page's code-behind.
From the child page, pass necessary unique parameters to this BindFamilyMembers Master method.
This way you don't need to expose the controls themselves, you expose a method that modifies those controls based on parameters.
edit
You want to notify the child page that the DDL SelectedIndex has changed. This is a great case for custom events.
You can configure an event on the Master page that the child page listens for. When DDL.SelectedIndex changes, you can fire the custom event (and pass the selected information) via the custom event, and any child page that is listening can handle that event.
There are a lot of examples of custom events online. Here's one you can start with: http://www.marten-online.com/csharp/simple-custom-event-handling.html

Looking for suitable DataBound control to implement questions page

I looking forward to find a suitable asp.net DataBound control (C#) to implement in my application.
I wanted to create a exam page where each page show 10 questions, each question got a Label control and a radiobutton control to display the choices, the data to be bind into the DataBound control might be having multiple rows where each rows represent each question.
I found that DetailView control was quite suit with my requirement but I not able to set the page size.
Please help to give some suggestion and advises, thank you in advanced.
I would use a DataList or a ListView, because it will allow you to enter a template for each item. The reason I would choose these over a repeater is because you can use data keys, which will probably come in handy.
Here's a simple example of how you could implement a list of questions:
<asp:DataList ID="DataList1" runat="server" DataKeyField="QuestionID" ...>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Question")%>' />
<asp:RadioButton ID="RadioButton1" runat="server" Text="Yes" GroupName="QuestionAnswer" ... />
<asp:RadioButton ID="RadioButton2" runat="server" Text="No" GroupName="QuestionAnswer" ... />
</ItemTemplate>
</asp:DataList>
I would recommend to you to use the Repeater control since you can customize it's design very easily to suit your needs.
Here are two tutorials on how to use it:
http://www.w3schools.com/aspnet/aspnet_repeater.asp
http://www.learn-asp.net/asptutorials/Repeater.aspx
Update:
Repeater doesn't have pagination included so you would have to add it:
http://blog.divergencehosting.com/2009/03/25/adding-paging-repeater/
Other option would be to just use a GridView which has pagination included.

How to bind a classes property to a TextBox?

I have a Customer class with a string property comments and I am trying to bind it like this:
<asp:TextBox ID="txtComments"
runat="server"
TextMode="MultiLine" Text=<%=customer.Comments %>>
</asp:TextBox>
However, it gives me the error:
Server tags cannot contain <% ... %> constructs.
I also have a method in the class called GetCreatedDate and in the aspx page, I am doing
<%=GetCreatedDate()%> and <%GetCreatedDate();%>. What is the difference?
Alternatively you can set the value in the Page_Load event of the code behind file:
txtComments.Text = customer.Comments;
you should use "<%# %>" for data binding
<asp:TextBox ID="txtComments"
runat="server"
TextMode="MultiLine" Text="<%# customer.Comments %>">
</asp:TextBox>
Try this instead.
<asp:TextBox ID="txtComments"
runat="server"
TextMode="MultiLine" Text=<%# customer.Comments %>>
</asp:TextBox>
Notice the = to #
Use the DataBinding syntax as stated, <%# customer.Comments %>. This syntax is only evaluated when the TextBox is databound. You would usually use it in a DataBound list. In this case you need to databind the control manually. Override the page's OnDataBinding method and call txtComments.DataBind();
The databinding syntax is the only way to declaratively set ServerControl properties from the aspx page. The Response.Write of the other syntax happens at a time that the ServerControl properties cannot be accessed. If the control is not inside a databound control, you have to databind it.
If you were looking to go all declarative in your page, you don't gain to much using this method because you still need to write code in the code behind.
An alternative if you want to use the TextBox on its own without a parent DataBound control would be to subclass the TextBox, add an AutoBind property, and in the subclassed control call its DataBind method if it is true. That would let you bind the values without writing databinding code in the code behind.
You could also add the TextBox and other form controls to a FormView control and bind it to your object. You can still use the DataBinding syntax in that case.
try this
<asp:TextBox ID="txtComments"
runat="server"
TextMode="MultiLine" Text='<%# customer.Comments %>'>
</asp:TextBox>

Categories