我会尝试解释我的问题并尝试编写一个示例,但是在asp.net和C#中。
我有一个转发器,里面有一堆复选框。该表的布局类似于......
<ItemTemplate>
<tr>
<td><%#Eval("question")%></td>
<td><a href="edit.aspx?id=<%#Eval('id')%>">Edit</a></td>
<td>Applies to the following</td>
<td><asp:CheckBox id="chkModel_A" runat="server" Text="Model A" /></td>
<td><asp:CheckBox id="chkModel_B" runat="server" Text="Model B" /></td>
<td><asp:CheckBox id="chkModel_C" runat="server" Text="Model C" /></td>
</tr>
<ItemTemplate>
当它数据绑定时,它会收到问题,ID和第一次检查的模型列表......所以
问题1 ..(ID)10 ... A C ...
但是如何检查chkModel_A和chkModel_C,因为它们在转发器中,我可以在这个表中有多个问题,所以当它问题2时它不会得到A C并得到任何问题2。目前,它只检查最后一个或者它是第一个数据表问题。谢谢你的帮助!
我忘了添加数据绑定...
protected void rptQs_ItemDataBound(object sender, RepeaterItemEventArgs e) {
RepeaterItem ri = e.Item;
foreach(string model in modelsSplit) {
CheckBox cb = (CheckBox) ri.FindControl("chkModel_" + model);
if (cb != null) {
cb.Checked = true;
}
}
}
这是数据绑定后的结果。
<table>
<tr>
<td>This is question 1</td>
<td><a href="edit.aspx?id=1">Edit</a></td>
<td>Applies to the following</td>
<td><asp:CheckBox id="chkModel_A" runat="server" Text="Model A" Checked="true" /></td>
<td><asp:CheckBox id="chkModel_B" runat="server" Text="Model B" /></td>
<td><asp:CheckBox id="chkModel_C" runat="server" Text="Model C" Checked="true" /></td>
</tr>
<tr>
<td>This is question 2</td>
<td><a href="edit.aspx?id=2">Edit</a></td>
<td>Applies to the following</td>
<td><asp:CheckBox id="chkModel_A" runat="server" Text="Model A" Checked="true" /></td>
<td><asp:CheckBox id="chkModel_B" runat="server" Text="Model B" /></td>
<td><asp:CheckBox id="chkModel_C" runat="server" Text="Model C" Checked="true" /></td>
</tr>
<tr>
<td>This is question 3</td>
<td><a href="edit.aspx?id=3">Edit</a></td>
<td>Applies to the following</td>
<td><asp:CheckBox id="chkModel_A" runat="server" Text="Model A" Checked="true" /></td>
<td><asp:CheckBox id="chkModel_B" runat="server" Text="Model B" /></td>
<td><asp:CheckBox id="chkModel_C" runat="server" Text="Model C" Checked="true" /></td>
</tr>
</table>
我试图让表格中的每一行都选中正确的复选框。相反,所有这些都以相同的方式进行检查。我无法正确显示问题2或3。希望有助于解释。
答案 0 :(得分:0)
您遇到的问题是您无法访问并因此操纵代码隐藏中的复选框元素。正确?
要解决此问题,您必须使用FindControl
找到控件。如下所示:
你的aspx:
// Set the OnItemDataBound attribute of your repeater
<asp:Repeater ID="yourRepeaterId" runat="server" OnItemDataBound="SomeFunctionName">
<ItemTemplate>
<tr>
<td><%#Eval("question")%></td>
<td><a href="edit.aspx?id=<%#Eval('id')%>">Edit</a></td>
<td>Applies to the following</td>
<td><asp:CheckBox id="chkModel_A" runat="server" Text="Model A" /></td>
<td><asp:CheckBox id="chkModel_B" runat="server" Text="Model B" /></td>
<td><asp:CheckBox id="chkModel_C" runat="server" Text="Model C" /></td>
</tr>
<ItemTemplate>
</asp:Repeater>
在您的代码后面文件:
void SomeFunctionName(object sender, RepeaterItemEventArgs e)
{
Checkbox chkbox_A = (Checkbox)e.Item.FindControl("chkModel_A");
Checkbox chkbox_B = (Checkbox)e.Item.FindControl("chkModel_B");
Checkbox chkbox_C = (Checkbox)e.Item.FindControl("chkModel_C");
// Use or manipulate chkbox_A, chkbox_B, chkbox_C as per your requirements...
}
上述技术可用于指代转发器内的任何内容,甚至是另一个转发器(在嵌套转发器的情况下)。我希望这有帮助...
答案 1 :(得分:0)
您想将 e.Item.DataItem 投射到 ItemDataBound 事件中的相应对象。
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<table>
<tr>
<td><%#Eval("question")%></td>
<td><a href="edit.aspx?id=<%# Eval("id") %>">Edit</a></td>
<td>Applies to the following</td>
<td>
<asp:CheckBox ID="chkModel_A" runat="server" Text="Model A" /></td>
<td>
<asp:CheckBox ID="chkModel_B" runat="server" Text="Model B" /></td>
<td>
<asp:CheckBox ID="chkModel_C" runat="server" Text="Model C" /></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
public class Test
{
public int Id { get; set; }
public string Question { get; set; }
public bool A { get; set; }
public bool B { get; set; }
public bool C { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Repeater1.DataSource = new List<Test>()
{
new Test { Id = 1, Question = "One", A = true, B = false, C = true},
new Test { Id = 2, Question = "Two", A = false, B = true, C = true},
new Test { Id = 3, Question = "Three", A = false, B = true, C = true},
};
Repeater1.DataBind();
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
RepeaterItem item = e.Item;
var test = e.Item.DataItem as Test;
var a = item.FindControl("chkModel_A") as CheckBox;
a.Checked = test.A;
var b = item.FindControl("chkModel_B") as CheckBox;
b.Checked = test.B;
var c = item.FindControl("chkModel_C") as CheckBox;
c.Checked = test.C;
}
}