Difference between revisions of "Repeater"
(→Sample2) |
|||
(One intermediate revision by the same user not shown) | |||
Line 99: | Line 99: | ||
</source> | </source> | ||
− | <source lang="csharp> | + | |
+ | <source lang="csharp"> | ||
using System; | using System; | ||
using System.Collections; | using System.Collections; | ||
Line 155: | Line 156: | ||
Console.WriteLine("Quantity: {0}", quantity); | Console.WriteLine("Quantity: {0}", quantity); | ||
} | } | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | == Sample 3 == | ||
+ | Bei jedem Item hat es zwei Buttons. Im Code wird evaluiert, velcher Button gedrückt worden ist. | ||
+ | <source lang="asp"> | ||
+ | <%@ Page Language="C#" | ||
+ | AutoEventWireup="true" | ||
+ | CodeBehind="WebForm1.aspx.cs" | ||
+ | Inherits="WebApplication2.WebForm1"%> | ||
+ | |||
+ | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
+ | |||
+ | <html xmlns="http://www.w3.org/1999/xhtml"> | ||
+ | <head runat="server"> | ||
+ | <title></title> | ||
+ | </head> | ||
+ | <body> | ||
+ | <form id="form1" runat="server"> | ||
+ | <div> | ||
+ | <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="ItemBound" OnItemCommand="ItemCommand"> | ||
+ | <ItemTemplate> | ||
+ | <asp:Button ID="ButtonEdit" runat="server" /> | ||
+ | <asp:Button ID="ButtonDelete" runat="server" /> | ||
+ | <br /> | ||
+ | </ItemTemplate> | ||
+ | </asp:Repeater> | ||
+ | </div> | ||
+ | <asp:Label ID="LabelFeedback" runat="server" Text="Nothing clicked yet"></asp:Label> | ||
+ | </form> | ||
+ | </body> | ||
+ | </html> | ||
+ | |||
+ | </source> | ||
+ | |||
+ | Das Code-Behind: | ||
+ | |||
+ | <source lang="csharp"> | ||
+ | using System; | ||
+ | using System.Collections.Generic; | ||
+ | using System.Linq; | ||
+ | using System.Web; | ||
+ | using System.Web.UI; | ||
+ | using System.Web.UI.WebControls; | ||
+ | |||
+ | namespace WebApplication2 | ||
+ | { | ||
+ | public partial class WebForm1 : System.Web.UI.Page | ||
+ | { | ||
+ | protected void Page_Load(object sender, EventArgs e) | ||
+ | { | ||
+ | if (!IsPostBack) | ||
+ | { | ||
+ | IList<int> list = new List<int>(); | ||
+ | list.Add(1); | ||
+ | list.Add(2); | ||
+ | list.Add(3); | ||
+ | list.Add(4); | ||
+ | list.Add(5); | ||
+ | this.Repeater1.DataSource = list; | ||
+ | this.Repeater1.DataBind(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | public void ItemBound(Object sender, RepeaterItemEventArgs e) | ||
+ | { | ||
+ | if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) | ||
+ | { | ||
+ | int dataitem = (int) e.Item.DataItem; | ||
+ | |||
+ | Button buttonEdit = (Button) e.Item.FindControl("ButtonEdit"); | ||
+ | buttonEdit.CommandName = "Edit"; | ||
+ | buttonEdit.CommandArgument = "" + dataitem; | ||
+ | buttonEdit.Text = "Edit " + dataitem; | ||
+ | |||
+ | Button buttonDelete = (Button)e.Item.FindControl("ButtonDelete"); | ||
+ | buttonDelete.CommandName = "Delete"; | ||
+ | buttonDelete.CommandArgument = "" + dataitem; | ||
+ | buttonDelete.Text = "Delete " + dataitem; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public void ItemCommand(Object Sender, RepeaterCommandEventArgs e) | ||
+ | { | ||
+ | this.LabelFeedback.Text = string.Format("commandname: {0}, commandarg: {1}", e.CommandName, e.CommandArgument); | ||
+ | } | ||
+ | |||
+ | |||
} | } | ||
} | } | ||
</source> | </source> |
Latest revision as of 14:05, 22 July 2010
Sample1
Leider fehlt dieses Beispiel über einen Repeater in vielen ASP.NET Büchern. Benutzt man das ItemBound Event, macht es den Code viel lesbarer.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="ItemBound">
<ItemTemplate>
<h2>hello <asp:Label ID="LabelItem" runat="server" Text="Label"/></h2>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IList<String> myList = new List<String>();
myList.Add("hugo");
myList.Add("bossi");
myList.Add("aikobus");
this.Repeater1.DataSource = myList;
this.Repeater1.DataBind();
}
public void ItemBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string item = (string) e.Item.DataItem;
Label label = (Label) e.Item.FindControl("LabelItem");
label.Text = item;
}
}
}
Sample2
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Fruit Quantities</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="ItemBound">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Key")%>'/>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="0" Value="0"/>
<asp:ListItem Text="1" Value="1"/>
<asp:ListItem Text="2" Value="2"/>
</asp:DropDownList>
<br />
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
IDictionary<string, ushort> data = this.FruitQuantities();
this.Repeater1.DataSource = data;
this.Repeater1.DataBind();
}
}
private IDictionary<string, ushort> FruitQuantities()
{
Dictionary<string, ushort> result = new Dictionary<string, ushort>();
result.Add("Bananas", 0);
result.Add("Apples", 1);
result.Add("Pears", 1);
return result;
}
public void ItemBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
KeyValuePair<string, ushort> item = (KeyValuePair<string, ushort>)e.Item.DataItem;
DropDownList dropDownList = (DropDownList)e.Item.FindControl("dropDownList1");
dropDownList.SelectedValue = item.Value.ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach(RepeaterItem item in this.Repeater1.Items)
{
DropDownList dropDownList = (DropDownList)item.FindControl("dropDownList1");
ushort quantity = ushort.Parse(dropDownList.SelectedValue);
Console.WriteLine("Quantity: {0}", quantity);
}
}
}
Sample 3
Bei jedem Item hat es zwei Buttons. Im Code wird evaluiert, velcher Button gedrückt worden ist.
<%@ Page Language="C#"
AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication2.WebForm1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="ItemBound" OnItemCommand="ItemCommand">
<ItemTemplate>
<asp:Button ID="ButtonEdit" runat="server" />
<asp:Button ID="ButtonDelete" runat="server" />
<br />
</ItemTemplate>
</asp:Repeater>
</div>
<asp:Label ID="LabelFeedback" runat="server" Text="Nothing clicked yet"></asp:Label>
</form>
</body>
</html>
Das Code-Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IList<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
list.Add(5);
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
}
public void ItemBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int dataitem = (int) e.Item.DataItem;
Button buttonEdit = (Button) e.Item.FindControl("ButtonEdit");
buttonEdit.CommandName = "Edit";
buttonEdit.CommandArgument = "" + dataitem;
buttonEdit.Text = "Edit " + dataitem;
Button buttonDelete = (Button)e.Item.FindControl("ButtonDelete");
buttonDelete.CommandName = "Delete";
buttonDelete.CommandArgument = "" + dataitem;
buttonDelete.Text = "Delete " + dataitem;
}
}
public void ItemCommand(Object Sender, RepeaterCommandEventArgs e)
{
this.LabelFeedback.Text = string.Format("commandname: {0}, commandarg: {1}", e.CommandName, e.CommandArgument);
}
}
}