Displaying collection in Repeater control - asp.net

I'm beginner in Asp.Net and Im trying to display "gc" on reapet control.
Here's the code-behind:
public partial class _Default : System.Web.UI.Page
{
List<GlassesCollection> gc= BL.Example.GetCategory() ;
protected void Page_Load(object sender, EventArgs e)
{
rpt1.DataSource = gc;
rpt1.DataBind();
}
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
}
Im using the following ASP code:
<asp:Repeater ID="rpt1" runat="server" onitemdatabound="rpt1_ItemDataBound">
<ItemTemplate>
<%# Eval("gc") %>
</ItemTemplate>
</asp:Repeater>
But in run time i get this Exception:
Exception Details: System.Web.HttpException: DataBinding: 'ISeeOptic.DataType.GlassesCollection' does not contain a property with the name 'gc'.
Why i get this Excption and idea how to solve this?
Thank you in advance!

Try eval the specific property rather than the object
<asp:Repeater ID="rpt1" runat="server" onitemdatabound="rpt1_ItemDataBound">
<ItemTemplate>
<%# Eval("gcProperty") %>
</ItemTemplate>
</asp:Repeater>
The SO Question gives more details about binding a repeater to a generic list.

You can generally take advantage of DataBinder class which has the most power and flexibility the same as code listing here...
<asp:Repeater ID="rpt1" runat="server" onitemdatabound="rpt1_ItemDataBound">
<ItemTemplate>
<%# DataBinder.Eval("gcProperty") %>
</ItemTemplate>
</asp:Repeater>

Related

Binding a control property to an expression

With ASP.NET WebForms, is it possible to bind a control's property to an expression?
For example
Visible="<% myProp.Value == otherProp.Value %>"
I realize I can do this in the code-behind, but I'm trying to keep UI display details confined to the markup as much as possible.
Yes, and this is the most efficient way to do ASP.Net WebForms development.
You want to call DataBind() to make the <%# myProp.Value == otherProp.Value %> expression to be evaluated. I prefer calling DataBind() in Page_PreRender
Example:
protected void Page_PreRender(object sender, EventArgs e)
{
DataBind();
}
<asp:PlaceHolder runat="server" Visible='<%# myProp.Value == otherProp.Value %>'>
Hello
</asp:PlaceHolder>
In fact, you can put your databinding logic either in your base class for your page, or in a <script runat="server"> block.
Complete page example:
<%# Page Language="C#" %>
<script runat="server">
void Page_PreRender(object sender, EventArgs e)
{
DataBind();
}
</script>
<html>
<body>
<asp:Placeholder runat="server" Visible='<%# DateTime.Now.Seconds % 2 == 1 %>'>
It is now an odd second
</asp:Placeholder>
</body>
</html>

How to get Reference to the label in repeater item in code behind

<asp:repeater id="rpt" run="server">
<ItemTemplate>
<asp:LinkButton id="Delete" runat="server" OnCommand="Delete_Command"></asp:linkButton>
<asp:label id="lblMessage" run="server">
</ItemTemplate>
</asp:repeater>
Code Behind:
protected void Delete_Command(object sender, CommandEventArgument e)
{
}
how i get the reference to the "lblMessage" in Delete_Command.
Try this:
protected void Delete_Command(object sender, CommandEventArgs e)
{
LinkButton button = (LinkButton)sender;
Label label = (Label)button.NamingContainer.FindControl("lblMessage");
// do something with the label
}
If you:
Have bound the repeater
Have ViewState enabled
Do not re-bind the repeater earlier in the post back
this should work. If not, please verify that the id of the label is indeed exactly the same as in the ...FindControl("lblMessage");. Also make sure that runat="server" is set on all the controls involved.
Edit: One more thing to check: Search the markup file (the .aspx file) and check if there are any other controls that also use the same event in the code behind. If another control is using the same event handler and that control is not in the repeater, the label will not be found.
means are you want find a lable in Delete_Command event?
in aspx
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:LinkButton ID="Delete" runat="server" OnCommand="Delete_Command"></asp:LinkButton>
<asp:Label ID="lblMessage" run="server">
</ItemTemplate>
</asp:Repeater>
in aspx.cs
protected void Delete_Command(object sender, CommandEventArgs e)
{
foreach (RepeaterItem item in rpt.Items)
{
Label lblMessage = item.FindControl("lblMessage") as Label;
if (lblMessage != null)
{
lblMessage.Text = "";
}
}
}
If You want to make it in your way use following code in
protected void Repeater1_ItemCommand(object source, CommandEventArgs e)
{
(((LinkButton)source).NamingContainer).FindControl("lblName")
}
Another approach.. But something that you can buy
aspx
<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%=Eval("Name") %>' ></asp:Label>
<asp:LinkButton runat="server" CommandName="Delete_Command" Text="sd"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
.cs
protected void Delete_Command(object sender, CommandEventArgument e)
{
if(e.CommandName != null)// Conditional Check
{
Label label = e.Item.FindControl("lblMessage");
// do something with the label
}
}

Set visible of a label from code behind

In asp.net I have this label:
<asp:Label ID="Label3" runat="server" Text="0" visible='<%# visibleCredits() %>'></asp:Label>
In code behind I have:
protected bool visibleCredits()
{
return false;
}
But the label is always shown, it should be invisible I think. Please don't ask why I did not set:
Label3.Visible = visibleCredits();
from the code behind.
Add this to your page:
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
It will bind your page to the server control and allow you to use data bindings like this.
As Vache suggeseted, you need to call DataBind() since you're using the data binding syntax <%# visibleCredits() %>. Alternatively, you can also use <%= visibleCredits() %> and not need to call DataBind().

I want to get property value from code behind

I have a case that I need to set the Text property for an asp label in the aspx page not from code behind. More exactly, I need to set a value to asp control in aspx page and this value is set by a property in the same page code behind.
so I need to use an expression to do that like:
<asp:Label Text="<%= MyProperty %>" ..../>
I use:
<%= MyProperty %> doesn't work.
<%# MyProperty %> doesn't also.
Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
public string CustomTitle = "This Is Title";
protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
}
}
Default.aspx
<asp:Label Text='<%#CustomTitle %>' runat="server" />
You have to treat regular HTML and WebControls differently:
regular HTML:
Using <%= ... %> is sufficient:
<span><%= MyProperty %></span>
WebControls (stuff starting with <asp:...>):
<asp:Label Text='<%# MyProperty %>' />
In this case, you also have to call Me.DataBind() (VB) or this.DataBind(); (C#) in your codebehind, since <%# ... %> are data binding expressions.
Page.DataBind();
Do you call this in your code? It binds all variables set in code to your page.

How do I retrieve the control contents in a dynamic table?

I have a page where I would like to collect information about x number of users. I have a control where you enter in the number of users and based off of that number, I create a dynamic table with a row for each user. Each table row has textbox controls that I would like to retrieve the value from on postback. How can this be accomplished?
What particular style of ASP.Net are you targeting? ASP.Net MVC? Webforms?
The quickest and easiest way to do something like this in webforms (which Im way more familiar with) would be to drop a GridView control on the page, and bind a generic collection to it that you set the size of based on the number entered in the control.
Here's a quick 10m piece of code. Created a default WebForms Web project in Visual Studio 2010.
Web Page Source:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<p>
<table>
<tr>
<td>Rows:</td>
<td><asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</td>
</tr>
<tr>
<td colspan=2>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
</td>
</tr>
</table>
</p>
</asp:Content>
Code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
List<string> users = new List<string>(Enumerable.Repeat(string.Empty, Int32.Parse(TextBox1.Text)));
GridView1.DataSource = users;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
var list = from GridViewRow row in GridView1.Rows
where row.RowType == DataControlRowType.DataRow
select (row.FindControl("TextBox2") as TextBox).Text;
// now do something with this list of strings
}
}
}
You may find it much easier to create an asp:GridView instead. You can then iterate through the rows on postback and inspect the controls. Lots of example code out there.

Resources