我是这个网站的新手。我需要asp.net C#的帮助。在我的例子中,我有一个数据列表控件,我显示一些书籍记录,如书名,书籍作者,出版商和书的图像。所以当我从这些记录中购买一两本书然后我选择一个选中复选框后,我点击一个按钮,通过该按钮我可以获得有关转发器中所选记录的信息。所以我的问题是,当我点击按钮时,我如何获得所选复选框的控件和信息显示在转发器??
谢谢
HD
答案 0 :(得分:0)
以下是如何使用Repeater
复选框的完整示例。
在检查用户选择时,项目ID存储在隐藏字段中以获取其他信息。
此示例可能与您的代码不符,因为您没有提供任何示例。
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Repeater Example</title>
<script language="C#" runat="server">
public class PositionData
{
private int id;
private string name;
private string _data;
private bool selected;
public PositionData(int id, string name, string data)
{
this.name = name;
this._data = data;
}
public int Id
{
get { return id; }
}
public string Name
{
get { return name; }
}
public string data
{
get { return _data; }
}
public bool Selected
{
get { return selected; }
}
}
void Page_Load(Object Sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList values = new ArrayList();
values.Add(new PositionData(1, "Prod1", "Info1"));
values.Add(new PositionData(2, "Prod2", "Info1"));
values.Add(new PositionData(3, "Prod3", "Info1"));
Repeater1.DataSource = values;
Repeater1.DataBind();
}
if (IsPostBack)
{
foreach (RepeaterItem i in Repeater1.Items)
{
//Retrieve the state of the CheckBox
CheckBox cb = (CheckBox)i.FindControl("selectUser");
HtmlInputHidden info = (HtmlInputHidden)i.FindControl("fieldName");
String itemInfo = info.Value;
if (cb.Checked)
{
// do whatever you want with selected item
// having it's ID
}
}
}
}
private void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var myHidden = (HtmlInputHidden)e.Item.FindControl("fieldName");
myHidden.Value = ((PositionData)e.Item.DataItem).Id.ToString();
}
}
</script>
</head>
<body>
<h3>
Repeater Example</h3>
<form id="form1" runat="server">
<b>Repeater1:</b>
<br />
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_OnItemDataBound">
<HeaderTemplate>
<table border="1">
<tr>
<td>
<b>Select this one</b>
</td>
<td>
<b>Name</b>
</td>
<td>
<b>Data</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
Select
<asp:CheckBox ID="selectUser" runat="server" />
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "name") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "data") %>
<input id="fieldName" runat="server" type="hidden" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<button id="submitButton" runat="server">Submit!</button>
</form>
</body>
</html>