views:

870

answers:

2

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?

A: 
<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.

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

ASP.NET : The checkbox and checkboxlist control

Virat Kadaru
this regular html what about asp.net, programmatically?
ferronrsmith
I have added a link to the solution, hope it helps (I am not a asp.net programmer so I can't give you the exact code myself)
Virat Kadaru
that seems more of a question than an actual answer :(
ferronrsmith
A: 

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"&gt;

<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"&gt;
<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>
Chris Mullins
not exactly what i'm look for. the list is not programatically created in the code behind. Its grab from a database and placed in a gridview.
ferronrsmith
thanks anyways for the help.
ferronrsmith
I was just generating a list in the page load to have something to bind to. I think the same concept would apply if the list came from the database. Can you post some of of your code so we can see what you are working with?
Chris Mullins
I fixed the issue, thanks anyways. Your code gave me some ideas
ferronrsmith