gpt4 book ai didi

c# - 回发 ASP NET 上未选中复选框

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:12 25 4
gpt4 key购买 nike

我有一个 asp.net 项目,我们称它们为对象。所有对象都在转发器中创建:

<section class="topbar">
<asp:Button runat="server" ID="btnSave" OnClick="btnSave_OnClick" Text="Save" />
</section>

<asp:Repeater runat="server" id="gwList">
<ItemTemplate>
<div class="block">
<input type="checkbox" runat="server" ID="chkObjectSelected" value='<%# ((Object.Object)Container.DataItem).Id %>'/>

<label><%# ((Object.object)Container.DataItem).Title %></label>

</div>
</ItemTemplate>
</asp:Repeater>

问题来了,在回传中没有一个选中的对象会被设置为选中。这是我用来确定对象是否被选中的代码:

protected void btnSave_OnClick(object sender, EventArgs e)
{
List<string> objectIdSelected = new List<string>();

foreach (RepeaterItem oneObject in gwList.Items)
{
HtmlInputCheckBox chkObj = (HtmlInputCheckBox)carAd.FindControl("chkObjectSelected");

if (chkObj.Checked)
{
objectIdSelected.Add(chkObj.Value.ToString());
}
}
}

最佳答案

尝试替换

<input type="checkbox"  runat="server" ID="chkObjectSelected" value='<%# ((Object.Object)Container.DataItem).Id %>'/> 

有了这个

 <asp:CheckBox runat="server" ID="chkObjectSelected" Value='<%# ((Object.Object)Container.DataItem).Id %>'/> 

并将您的代码更改为:

protected void btnSave_OnClick(object sender, EventArgs e)
{
List<string> objectIdSelected = new List<string>();

foreach (RepeaterItem oneObject in gwList.Items)
{
CheckBox chkObj = (CheckBox)oneObject.FindControl("chkObjectSelected");

if (chkObj.Checked)
{
objectIdSelected.Add(chkObj.Attributes["Value"].ToString());
}
}
}

关于c# - 回发 ASP NET 上未选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37452259/

25 4 0