Asp.net 中继器项前插入的行阻止单选按钮。是否仅为此中继器项选中回发?
我有一个中继器,用1个文本和多个Asp.net 中继器项前插入的行阻止单选按钮。是否仅为此中继器项选中回发?,asp.net,radio-button,postback,repeater,Asp.net,Radio Button,Postback,Repeater,我有一个中继器,用1个文本和多个HtmlRadioButtons填充表行,用户应该检查这些内容。 因为列表可能变得相当大,所以必须只检查前N行。 为了向用户显示这一点,我添加了一个额外的TableRow,其中一个TableCell(colspan=4),在第一个可选行之前添加了一些说明文本。 我将这一行添加到中继器ItemDataBound 在回发时,额外行前后的所有选中单选按钮都会返回其选中值,但我插入额外行之前的项目除外。 我尝试改变额外行的类型(Html/RadioButton、Html/
HtmlRadioButtons
填充表行,用户应该检查这些内容。
因为列表可能变得相当大,所以必须只检查前N行。
为了向用户显示这一点,我添加了一个额外的TableRow
,其中一个TableCell
(colspan=4),在第一个可选行之前添加了一些说明文本。
我将这一行添加到中继器ItemDataBound
在回发时,额外行前后的所有选中单选按钮都会返回其选中值,但我插入额外行之前的项目除外。
我尝试改变额外行的类型(Html/RadioButton
、Html/TableRow
和Html/TableCell
),并尝试改变额外行的EnableViewState
)。
但是,我无法使用此行/单选按钮回发选中的值,它始终为false
有人知道为什么该值不回发吗?有人知道如何在不阻止单选按钮回发的情况下添加额外的行吗 附加说明:
- 我不想在单选按钮中添加一个
,因为我喜欢一次处理并存储所有值OnClick
- 我在
页面加载了原始数据,但对于其他行/单选按钮来说,这似乎不是问题
<ItemTemplate>
<asp:TableRow runat="server">
<asp:TableCell ID="CategoryName" CssClass="td_1" Text='<%# Eval("Name") %>'/>
<asp:TableCell CssClass="td_radio" runat="server">
<asp:HtmlInputRadioButton type="radio" runat="server"
ID="rdBelonging"
Name='<%# Eval("Id") %>'
value='<%# (int)Enum_BelongsToCategory.Belonging %>'
data-value='<%# Convert.ToBoolean(Eval("ShouldJudge")) ? "mandatory" : "optional" %>'/>
</asp:TableCell>
<asp:TableCell CssClass="td_radio" runat="server">
<asp:HtmlInputRadioButton type="radio" runat="server"
ID="rdNotBelonging"
Name='<%# Eval("Id") %>'
value='<%# (int)Enum_BelongsToCategory.NotBelonging %>'
data-value='<%# Convert.ToBoolean(Eval("ShouldJudge")) ? "mandatory" : "optional" %>'/>
</asp:TableCell>
<%-- more Enum_BelongsToCategory-ReadioButtons... --%>
</asp:TableRow>
</ItemTemplate>
查找选中单选按钮的代码(受系统负责人博客的启发)
虽然你的总体方法是正确的,但在我看来,你把整个事情复杂化了
。但简而言之,正如我前面所说的:当ViewState恢复时,控件的确切数量必须存在,当服务器控件的可见性设置为false时,当页面发回并且ViewState恢复时,它们确实存在。当您通过代码添加它们时,必须显式地重新添加它们。我希望这个解释有道理
代码示例:
<ItemTemplate>
<tr>
<td>'<%# Eval("Name") %>'</td>
<td><asp:RadioButton ID="rdBelonging" ...</td>
<!-- more Enum_BelongsToCategory-RadioButtons... -->
</tr>
<asp:Panel ID="pnlExplanationRow" runat="server" Visible="false">
<tr>
<td colspan=?>....</td>
</tr>
</asp:Panel>
</ItemTemplate>
''
谢谢你的回复。我有多行,每行有一个文本和多个单选按钮。每行只能选中一个单选按钮。但我在单击“下一步”按钮后,一次处理所有行。你的第二点帮助了我。现在,我在单选按钮行之前添加了一个服务器端表行(不是面板),其visible=false
,并将其设置为第一个可选项可见。我猜动态添加的控件不知怎么搞砸了回发,但我不确定(in)可见行如何更好地工作。因此,我会将此标记为答案,但可能首先等待更多关于原因的输入。关于您的第一点:我一开始只使用html控件,但是“我”在回发时找不到我的选中控件。当我将runat=“server”
添加到我的
中时,我收到了一条错误消息:“加载viewstate的控件树必须与用于保存viewstate的控件树匹配”。当我仅将输入
更改为asp控件
时,我得到了相同的错误。所以我也改变了行和单元格,它成功了。但也许你指的是一种不同的方法,然后一个代码示例可能会有所帮助!a) 我猜在页面加载中添加一行只会弄乱这个RepeaterItem的ViewState比较。它可能在Page\u Init
中工作,但出于其他原因,我使用Page\u Load
。中继器中的不可见控件阻止了额外的元素,因此它使ViewState保持不变。b) 我将大多数控件简化为html控件(再次),现在我在ViewState上不再出现错误,这是典型的。。。c) 除此之外,我现在还删除了_ItemDataBound,方法是处理asp代码中的行可见性和检查状态(灵感来自)
private void SaveCategoryJudgements(Product product)
{
int lRepeaterItemCount = 0;
foreach (RepeaterItem lItem in repeaterCategories.Items) {
lRepeaterItemCount ++;
var lCheckedRadioButton = GetCheckedRadioButton(lItem.Controls);
if (lCheckedRadioButton != null) {
int lCategoryId;
int lJudgement;
if ( (int.TryParse(lCheckedRadioButton.Attributes["value"], out lJudgement))
&& (int.TryParse(lCheckedRadioButton.Name, out lCategoryId)) )
{
ClassificationData.SaveDocumentCategoryByUser(product, lCategoryId, (Enum_BelongsToCategory)lJudgement);
}
}
}
}
public HtmlInputRadioButton GetCheckedRadioButton(ControlCollection controls)
{
foreach (Control lControl in controls) {
if (lControl is HtmlInputRadioButton) {
var lRadioButton = (HtmlInputRadioButton)lControl;
if (lRadioButton.Checked == true)
{
return lRadioButton;
}
}
else {
var lRadioButton = GetCheckedRadioButton(lControl.Controls);
if (lRadioButton != null) {
return lRadioButton;
}
}
}
return null;
}
<ItemTemplate>
<tr>
<td>'<%# Eval("Name") %>'</td>
<td><asp:RadioButton ID="rdBelonging" ...</td>
<!-- more Enum_BelongsToCategory-RadioButtons... -->
</tr>
<asp:Panel ID="pnlExplanationRow" runat="server" Visible="false">
<tr>
<td colspan=?>....</td>
</tr>
</asp:Panel>
</ItemTemplate>