OnCheckedChanged嵌套时,ListView控件 [英] OnCheckedChanged Nested Listview Controls
问题描述
我有以下的嵌套的ListView ...
I have the following nested listview...
<asp:ListView ID="lvwRiskQuestions" runat="server" ItemPlaceholderID="QuestionItemPlaceholder">
<LayoutTemplate>
<asp:PlaceHolder ID="QuestionItemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<%# Eval("DESCRIPTION")%>
<asp:ListView ID="lvwAnswers" runat="server" ItemPlaceholderID="AnswerItemPlaceholder" DataSource='<%# Eval("Answers")%>'>
<LayoutTemplate>
<asp:PlaceHolder ID="AnswerItemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:RadioButton ID="rdbSelect" runat="server" AutoPostBack="true" OnCheckedChanged="rdbSelectChanged"/>
<%# Eval("Description")%>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
我得到OnCheckedChanged像这样的单选按钮保持...
I get hold of the radio buttons OnCheckedChanged like so...
Protected Sub rdbSelectChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim rb1 As RadioButton = CType(sender, RadioButton)
Dim lvwAnswers = DirectCast(lvwRiskQuestions.FindControl("lvwAnswers"), ListView)
For Each row As ListViewItem In lvwAnswers.Items
Dim rb As RadioButton = row.FindControl("rdbSelect")
If rb IsNot Nothing AndAlso rb.Checked Then
rb.Checked = False
End If
Next
rb1.Checked = True
End Sub
我的问题是'lvwAnswers什么都不是。我猜我不是正确的做我的FindControl。
The problem i have is 'lvwAnswers' is Nothing. I'm guessing im not doing my findcontrol correctly.
任何帮助非常AP preciated。
Any help greatly appreciated.
推荐答案
如果你只是生成的答案单选按钮的列表,你可以使用的 单选按钮列表
控制。这将产生正确的HTML,以便只有一个答案可以每个问题进行选择,而不必回后取消选择其他选项。
If you're just generating a list of radio-buttons for the answers, you could use the RadioButtonList
control. This would generate the correct HTML so that only one answer could be selected per question without having to post-back to de-select the other options.
如果你的答案模板包含不止一个单选
越多,事情变得更加复杂。当它不是在一个单选按钮列表
的单选
使用的UniqueID $ C $托管C>父
NamingContainer
来打造其独特的组名称。不幸的是,在你的例子中, NamingContainer
将是 ListViewDataItem
从 lvwAnswers 列表中,和每个答案都会有不同的ID。
If your answer template contains more than a single RadioButton
, things get more complicated. When it's not hosted in a RadioButtonList
, the RadioButton
uses the UniqueID
of the parent NamingContainer
to build its unique group name. Unfortunately, in your example, the NamingContainer
will be the ListViewDataItem
from the lvwAnswers list, and each answer will have a different ID.
您需要的是单选
将看的 NamingContainer
NamingContainer
来产生它的组名。你既可以重新实施单选
控制,或使用反射的一点点更新私人 _uniqueGroupName
字段:
What you need is a RadioButton
which will look at the NamingContainer
's NamingContainer
to generate its group name. You could either re-implement the RadioButton
control, or use a little bit of reflection to update the private _uniqueGroupName
field:
[ToolboxData("<{0}:ListRadioButton runat=\"server\" />")]
public class ListRadioButton : RadioButton
{
private static readonly FieldInfo UniqueGroupNameField = FindUniqueGroupNameField();
private string _uniqueGroupName;
private static FieldInfo FindUniqueGroupNameField()
{
return typeof(RadioButton).GetField("_uniqueGroupName",
BindingFlags.NonPublic | BindingFlags.Instance);
}
protected virtual string CreateUniqueGroupName()
{
string result = GroupName;
if (string.IsNullOrEmpty(result))
{
result = ID;
}
if (string.IsNullOrEmpty(result))
{
result = UniqueID;
}
else
{
Control container = NamingContainer;
if (container != null)
{
if (container is IDataItemContainer)
{
container = container.NamingContainer ?? container;
}
result = container.UniqueID + base.IdSeparator + result;
}
else
{
string uniqueID = UniqueID;
if (!string.IsNullOrEmpty(uniqueID))
{
int index = uniqueID.LastIndexOf(base.IdSeparator);
if (index != -1)
{
result = uniqueID.Substring(0, 1 + index) + result;
}
}
}
}
return result;
}
private void EnsureUniqueGroupName()
{
if (_uniqueGroupName == null)
{
string value = CreateUniqueGroupName();
if (UniqueGroupNameField != null) UniqueGroupNameField.SetValue(this, value);
_uniqueGroupName = value;
value = base.Attributes["value"];
if (string.IsNullOrEmpty(value))
{
base.Attributes["value"] = UniqueID;
}
}
}
protected override bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
EnsureUniqueGroupName();
return base.LoadPostData(postDataKey, postCollection);
}
protected override void Render(HtmlTextWriter writer)
{
EnsureUniqueGroupName();
base.Render(writer);
}
}
随着地方控制和使用注册的网站
preFIX,你可以改变你的code为:
With that control in place and registered using the site
prefix, you can change your code to:
<asp:ListView ID="lvwRiskQuestions" runat="server" ItemPlaceholderID="QuestionItemPlaceholder">
<LayoutTemplate>
<asp:PlaceHolder ID="QuestionItemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<%# Eval("DESCRIPTION") %>
<asp:ListView ID="lvwAnswers" runat="server" ItemPlaceholderID="AnswerItemPlaceholder" DataSource='<%# Eval("Answers")%>'>
<LayoutTemplate>
<asp:PlaceHolder ID="AnswerItemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<site:ListRadioButton ID="rdbSelect" runat="server"
Text='<%# Eval("Description") %>'
/>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
在呈现的HTML中,单选按钮,每题届时将有相同的名称
,你将只能选择每个问题一个答案,不其张贴在每个选择整个页面。
In the rendered HTML, the radio-buttons for each question will then have the same name
, and you will only be able to select a single answer per question, without having to post the entire page on each selection.
这篇关于OnCheckedChanged嵌套时,ListView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!