解析多个值并将其发送到查询字符串中

时间:2021-10-26 22:35:18

I have a grid view with multiple select check boxes. I want to return an array of the items that have been selected and put this in a label (comma-separated)

我有一个带有多个选中复选框的网格视图。我想返回已选择的项目数组并将其放在标签中(以逗号分隔)

12,34,34,54,54,5

I want it to be parsed and then sent to a query string or send the whole value to the query string.

我希望它被解析,然后发送到查询字符串或将整个值发送到查询字符串。

How do a get multiple selected check boxes and return an array of items?

如何获取多个选中的复选框并返回一系列项目?

2 个解决方案

#1


Try this:

<%@ 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>

#2


<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>

When you try to retrieve the parameter 'check' you get an array of all the values that were selected.

当您尝试检索参数'check'时,您将获得所选的所有值的数组。

EDIT: This link should help in retrieving the values in ASP.

编辑:此链接应该有助于检索ASP中的值。

ASP.NET : The checkbox and checkboxlist control

ASP.NET:复选框和checkboxlist控件

#1


Try this:

<%@ 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>

#2


<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>

When you try to retrieve the parameter 'check' you get an array of all the values that were selected.

当您尝试检索参数'check'时,您将获得所选的所有值的数组。

EDIT: This link should help in retrieving the values in ASP.

编辑:此链接应该有助于检索ASP中的值。

ASP.NET : The checkbox and checkboxlist control

ASP.NET:复选框和checkboxlist控件