bind custom class array in repeater - c#

I want to bind the CourseFee[] array to repeater.
I want to bind Amount and CourseFeeType.Descr in my repeater.
How do I bind it?

Sample Class
public class Order
{
public CourceFeeType FeeType;
public int Amount;
public int CourseFee;
public void AddFeeTypeDetails(CourceFeeType Fees)
{
FeeType = new CourceFeeType();
FeeType.Code = Fees.Code;
FeeType.Desc = Fees.Desc;
}
// Nested class
public class CourceFeeType
{
public String Code;
public String Desc;
}
}
Sample Form Load Code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<Order> List = new List<Order>();
Order OrderObj = new Order();
Order.CourceFeeType Fees = new Order.CourceFeeType();
Fees.Code = "1";
Fees.Desc = "w2s";
OrderObj.Amount = 1;
OrderObj.AddFeeTypeDetails(Fees);
List.Add(OrderObj);
OrderObj = new Order();
OrderObj.Amount = 2;
Fees = new Order.CourceFeeType();
Fees.Code = "2";
Fees.Desc = "w22s";
OrderObj.AddFeeTypeDetails(Fees);
List.Add(OrderObj);
rpt.DataSource = List;
rpt.DataBind();
}
}
Repeater Item Bound Data Event Code
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lbl = (Label)e.Item.FindControl("lblDescription");
lbl.Text = ((Order)e.Item.DataItem).FeeType.Desc;
Label lblAmount = (Label)e.Item.FindControl("lblAmount");
lblAmount.Text = ((Order)e.Item.DataItem).Amount.ToString();
}
}
Sample HTML
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
<td>
Amount
</td>
<td>
Description
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblAmount" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="lblDescription" runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

Related

Why is validating two random numbers during submission in a form not working

I have the following code-behind:
Random rndNum;
int inFirstOne, inSecondOne;
protected void Page_Load(object sender, EventArgs e)
{
rndNum = new Random();
int inFirst = rndNum.Next(1, 51);
int inSecond = rndNum.Next(50, 101);
lblFirst.Text = inFirst.ToString();
inFirstOne = inFirst;
lblSecond.Text = inSecond.ToString();
inSecondOne = inSecond;
}
public void ValidateForm(object sender, EventArgs e)
{
string strNum = tbValidation.Text;
int inCalc = inFirstOne + inSecondOne;
if (inCalc.ToString() == strNum)
{
lblIsValid.Text = "Correct";
}
else
{
lblIsValid.Text = "Please enter the correct result";
lblIsValid.ForeColor = Color.DarkRed;
}
}
ASP.net code:
...
<tr>
<td><h2>What is <asp:Label ID="lblFirst" Text="" CssClass="numGen" ClientIDMode="Static" runat="server" /> + <asp:Label ID="lblSecond" Text="" CssClass="numGen" ClientIDMode="Static" runat="server" />?</h2></td>
<td><asp:TextBox ID="tbValidation" ClientIDMode="Static" CssClass="tbTech" runat="server"></asp:TextBox> <asp:Label ID="lblIsValid" runat="server" /></td>
</tr>
<tr>
<td colspan="2" class="setRight">
<asp:Button ID="SubmitForm" ClientIDMode="Static" runat="server" Text="Submit" OnClick="ValidateForm" CssClass="btn" UseSubmitBehavior="false" />
</td>
</tr>
...
I am always seeing Please enter the correct result message.
How can I modify to ensure when the result is entered it works to validate the calculation from code-behind.
It's not clear how you're filling tbValidation but in any case unless you execute your page_load logic only if request it's not postback, variables inFirstOne and inSecondOnewill be overwritten on button click.
It seems you need to have this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostback)
{
// do this only if it's the first request
rndNum = new Random();
int inFirst = rndNum.Next(1, 51);
int inSecond = rndNum.Next(50, 101);
lblFirst.Text = inFirst.ToString();
inFirstOne = inFirst;
lblSecond.Text = inSecond.ToString();
inSecondOne = inSecond
}
}
int inCalc = Convert.ToInt32(lblFirst.Text) + Convert.ToInt32(lblSecond.Text); worked.

ASP.NET WebForms Nested Repeater With LINQ Group Data Source

I have a LINQ grouping that I'd like to use to populate parent and child repeater controls.
<asp:Repeater ID="Parent" runat="server" OnItemDataBound="Parent_ItemDataBound">
<ItemTemplate>
<%# Eval("Key") %>
<asp:Repeater ID="Child" runat="server">
<ItemTemplate>
<%# Eval("Id") %>
<%# Eval("Name") %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
public class Dog
{
public int Id { get; set; }
public string Name { get; set; }
public string Breed { get; set; }
}
private IEnumerable<IGrouping<string, Dog>> GetDogs()
{
var dogs = new List<Dog>
{
new Dog
{
Id = 1,
Name = "Rex",
Breed = "Poodle",
},
new Dog
{
Id = 2,
Name = "Fido",
Breed = "Terrier",
},
new Dog
{
Id = 3,
Name = "Killer",
Breed = "Pit Bull",
}
};
return dogs.GroupBy(_ => _.Breed);
}
protected void Page_Load(object sender, EventArgs e)
{
Parent.DataSource = GetDogs();
Parent.DataBind();
}
protected void Parent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var item = e.Item;
if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
{
var repeater = (Repeater)item.FindControl("Child");
// I'm stuck on this code...
//repeater.DataSource = what to do?
//repeater.DataBind();
}
}
I'm trying to set the child repeater's data source in the Parent_ItemDataBound event. How do I do this? item.DataItem is type object and I can't figure out how to obtain the list of dogs that's inside the data item.
Expanding on Joshua's correct answer. If the 4.5 runtime is available to you, you can declare the types on the repeater itself so you will not have to bind the ItemDataBound event.
<ul>
<asp:Repeater runat="server" ID="Parent" ItemType="IGrouping<String, Dog>">
<ItemTemplate>
<li><%# Item.Key %><ul>
<asp:Repeater runat="server" ID="Child" ItemType="Dog" DataSource="<%#Item%>">
<ItemTemplate>
<li><%# Item.Id %></li>
<li><%# Item.Name %></li>
</ItemTemplate>
</asp:Repeater>
</ul></li>
</ItemTemplate>
</asp:Repeater>
</ul>
You can get the data from e.Item.DataItem. You should be able to cast this to IGrouping. Then you can get the inner repeater by looking for the control in the current repeaters repeateritem. Then bind the data from DataItem to the nested repeater.
This code below should work.
All this assumes you don't wrap the nested repeater in other controls.
protected void Parent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var item = e.Item.DataItem as IGrouping<string, Dog>;
if (item == null)
return;
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
var repeater = e.Item.FindControl("Child") as Repeater;
if (repeater != null)
{
repeater.DataSource = item;
repeater.DataBind();
}
}
}

Accessing DropDownlist placed inside repeater markup

below is the markup and codebehind.
I am trying to access dropdownlist in the markup from codebehind.
<asp:Repeater runat="server" ID="cataloguesRepeater">
<FooterTemplate>
<table>
<tbody>
<tr>
<td>
<asp:DropDownList runat="server" ID="dropDownList1" />
</td>
</tr>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
Codebehind
private void CatalogueItemCommand(object sender, RepeaterCommandEventArgs e)
{
DropDownList dd1 =(DropDownList)e.Item.FindControl("dropDownList1");
}
I always get dd1 as null for some reason. How can access this dropdownlist dd1?
You have to react on the ItemCreated Event:
protected void Page_Load(object sender, EventArgs e)
{
cataloguesRepeater.ItemCreated += cataloguesRepeater_ItemCreated;
cataloguesRepeater.DataSource = new [] { new { title = "item1"}, new { title = "item2" } };
cataloguesRepeater.DataBind();
}
void cataloguesRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
var ddl = e.Item.FindControl("dropDownList1");
}
}

Get Updated Value of a UserControl TextBox within a ListView

I have a page SendResults.aspx that holds a button and a ListView with ItemTemplate set to a user control (3 labels and 2 textboxes) that gets it's data from a matching object.
On Page_Load I fill the List with data (this works well).
When the button is clicked I want to take the user input in the user-control's textboxes and do something with it.
However I always get the initial value and not the updated one.
Here is the code:
The user-control "MatchControl.ascx"
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MatchControl.ascx.cs" Inherits="TotoMondeal.Controls.MatchControl" %>
<div>
<asp:Image ID="Team1FlagImage" runat="server" />
<asp:Label ID="Team1Label" runat="server" Width="150px"></asp:Label>
<asp:TextBox ID="Team1TextBox" runat="server" MaxLength="2" TextMode="Number" Width="50px" AutoPostBack="true" OnTextChanged="Team1TextBox_TextChanged"></asp:TextBox>
<asp:Label ID="Colon" runat="server" Font-Size="XX-Large" Text=":"></asp:Label>
<asp:TextBox ID="Team2TextBox" runat="server" MaxLength="2" TextMode="Number" Width="50px"></asp:TextBox>
<asp:Label ID="Team2Label" runat="server" Width="150px"></asp:Label>
<asp:Image ID="Team2FlagImage" runat="server" />
</div>
The user-control code-behind:
public partial class MatchControl : System.Web.UI.UserControl
{
public Match Match
{
get
{
object obj = ViewState["Match"];
return (obj == null) ? new Match() : (Match)obj;
}
set
{
ViewState["Match"] = value;
}
}
public string Team1Score
{
get { return Team1TextBox.Text; }
set { Team1TextBox.Text = value; }
}
public string Team2Score
{
get { return Team2TextBox.Text; }
set { Team2TextBox.Text = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
Team1Label.Text = Match.Team1Name;
Team2Label.Text = Match.Team2Name;
Team1TextBox.Text = Match.Team1Score.ToString();
Team2TextBox.Text = Match.Team2Score.ToString();
Team1TextBox.Enabled = Match.EnableTextBox;
Team2TextBox.Enabled = Match.EnableTextBox;
Team1FlagImage.ImageUrl = #"~/FlagImages/" +Match.Team1Name + ".png";
Team2FlagImage.ImageUrl = #"~/FlagImages/" + Match.Team2Name + ".png";
}
protected void Team1TextBox_TextChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
try
{
Match updatedMatch = new Match()
{
MatchId = Match.MatchId,
MatchDate = Match.MatchDate,
Result = Match.Result,
Team1Name = Match.Team1Name,
Team1Score = Convert.ToInt32(textBox.Text),
Team2Name = Match.Team2Name,
Team2Score = Match.Team2Score,
EnableTextBox = Match.EnableTextBox
};
Match = updatedMatch;
}
catch (Exception ex)
{
throw ex;
}
}
}
The SendResults.aspx:
<%# Page Title="שלח תוצאות" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="SendResults.aspx.cs" Inherits="TotoMondeal.SendResults" %>
<%# Register TagPrefix="TOTO" TagName="MatchControl" Src="~/Controls/MatchControl.ascx" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %>.</h2>
<div class="jumbotron">
<asp:ListView ID="TodayMatchesList" runat="server">
<ItemTemplate>
<TOTO:MatchControl ID="MatchControl" Match="<%# Container.DataItem %>" runat="server" />
</ItemTemplate>
</asp:ListView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</asp:Content>
the SendResults code-behind:
public partial class SendResults : Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Match> matches = new List<Match>();
matches = Queries.GetTodayMatches(DateTime.Now);
foreach (Match match in matches)
{
match.EnableTextBox = true;
}
this.TodayMatchesList.DataSource = matches;
this.TodayMatchesList.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < TodayMatchesList.Items.Count; i++)
{
MatchControl match = (MatchControl)TodayMatchesList.Items[i].FindControl("MatchControl");
TextBox textBox = (TextBox)match.FindControl("Team1TextBox");
string txt = textBox.Text;
}
}
}
The problem is that in this line:
TextBox textBox = (TextBox)match.FindControl("Team1TextBox");
string txt = textBox.Text;
I always get the initial value from the database, and not the user updated input.
Please help I'm new at this.
Your List is getting overwritten every time you post back. Add this in Page_Load for SendResults
if ( !Page.IsPostBack )
{
List<Match> matches = new List<Match>();
matches = Queries.GetTodayMatches(DateTime.Now);
...etc...
}
In addition to checking IsPostBack you need to handle saving your control properties in the ViewState. As suggested here: User control (ascx) and properties
Example from post:
public string Title {
get { return Convert.ToString(ViewState["Title"]); }
set { ViewState["Title"] = value; }
}
You would do this in your control class.

Repeater control not updating with ItemCommand

I have a Repeater control, that I have now reduced to just changing text in a text box when clicking the associated button.
However, this is not happening.
Here is my code so far:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<asp:Repeater ID="rptPdfList" runat="server" OnItemCommand="rptPdfList_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<td>File Name</td>
<td></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblName" runat="server" Text=<%#Eval("FileName") %>></asp:Label>
</td>
<td>
<asp:Button ID="btnLoad" runat="server" Text="Load" CommandName="LoadDoc"/>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<asp:Button ID="btnLoad" runat="server" Text="Load" OnClick="btnLoad_Click" /><br />
<iframe runat="server" id="pdfHolder"></iframe>
<br />
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Code Behind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetFiles();
}
private void GetFiles()
{
rptPdfList.DataSource = Pdf();
rptPdfList.DataBind();
}
protected void rptPdfList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
Label lblName = (Label)e.Item.FindControl("lblName");
switch (e.CommandName)
{
case "LoadDoc":
//xpdfHolder.Attributes.Add("src", "PDF/" + lblName.Text);
lblTest.Text = "test";
lblName.Text = "oops";
break;
}
}
public static List<PdfList> Pdf()
{
string pdfDir = HostingEnvironment.MapPath("~") + #"PDF\";
DirectoryInfo directory = new DirectoryInfo(pdfDir);
FileInfo[] pdfFiles = directory.GetFiles("*.pdf", SearchOption.AllDirectories);
List<PdfList> pdfLists = pdfFiles.Select(pdfFile => new PdfList
{
FileName = pdfFile.Name
}).ToList();
return pdfLists;
}
}
public class PdfList
{
public string FileName { get; set; }
}
Ca anyone see where I went wrong?
Edit, added all the code
Change this:
protected void Page_Load(object sender, EventArgs e)
{
GetFiles();
}
to this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GetFiles();
}
You are calling the GetFiles() each time so it is always returning to the initial state.
I am binding your repeater like this and it works fine for me, Just place your binding function in
if (!Page.IsPostBack)
condition :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (DataClassesDataContext dc = new DataClassesDataContext())
{
var v = (from s in dc.t_employees select s).ToList();
rptPdfList.DataSource = v;
rptPdfList.DataBind();
}
}
}
protected void rptPdfList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
Label lblName = (Label)e.Item.FindControl("lblName");
switch (e.CommandName)
{
case "LoadDoc":
//xpdfHolder.Attributes.Add("src", "PDF/" + lblName.Text);
lblTest.Text = "test";
lblName.Text = "oops";
break;
}
}

Categories