我有一个带有多个选中复选框的网格视图。我想返回已选择的项目数组,并将其放在标签(逗号分隔)
中12,34,34,54,54,5
我希望它被解析然后发送到查询字符串或将整个值发送到查询字符串。
如何获取多个选中的复选框并返回一组项目?
答案 0 :(得分:1)
试试这个:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Collections.Generic.List<int> Values = new System.Collections.Generic.List<int> { 1, 2, 3, 4, 5, 6, 7 };
grdTest.DataSource = Values;
grdTest.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
HtmlInputCheckBox chkTest = null;
string SelectedValues = "";
foreach (GridViewRow row in grdTest.Rows)
{
chkTest = (HtmlInputCheckBox) row.FindControl("chkTest");
if (chkTest != null && chkTest.Checked)
{
SelectedValues += (SelectedValues == "" ? chkTest.Value : ", " + chkTest.Value);
}
}
litValues.Text = SelectedValues;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdTest" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input id="chkTest" type="checkbox" name="chkTest" runat="server"
value="<%# (int)Container.DataItem %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />
<br />
<asp:Literal ID="litValues" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
答案 1 :(得分:0)
<form>
<input type="checkbox" name="check[]" value ="1"/>
<input type="checkbox" name="check[]" value ="2"/>
<input type="checkbox" name="check[]" value ="3"/>
<input type="checkbox" name="check[]" value ="4"/>
</form>
当您尝试检索参数'check'时,您将获得所选的所有值的数组。
编辑: 此链接应有助于检索ASP中的值。