views:

1144

answers:

3

I had displayed check box in a list and i want to access that which check box is selected or not and want to call controller action where i select option from dropdown list

<div id="pnlSent" style="display: none">
            <%= Html.DropDownList("select","msgtype") %>
            <% foreach (Usp_GetUserMessagesResult w in (List<Usp_GetUserMessagesResult>)ViewData["UListM"])
               { %>
                <li>
            <div class="question-info">

                <input id="Checkbox1" type="checkbox" onclick="function() { alert("df") ; } "  />

                <div class="views count"><span></span></div>
                <div class="question">
                    <div class="meta">Subject:  <%= w.Subject %></div>
                    <div class="meta">To: <%= w.Username  %></div>
                    <h3 class="title"><%= Html.Encode(w.Mesg)%></h3>
                </div>
            </div>
        </li>
            <% } %>

    </div>
A: 

You can loop through all of the controls on the div pnlsent and if the control type is a checkbox you can then determine if the checkbox is checked.

Example of looping through controls in VB....

 For Each ctrl As Control In Page.Controls
        If TypeOf ctrl Is TextBox Then
            CType(ctrl, TextBox).BackColor = clr
        Else
            If ctrl.Controls.Count > 0 Then
                SetTextBoxBackColor(ctrl, clr)
            End If
        End If
    Next
Eppz
+1  A: 

In asp.net-MVC you should ahve the info that you want to be able to access on the controller side in a form as so, I noticed you did not add an ID to your checkbox, mine is dynamically binded and I am using the HTML helper that comes with asp.net-mvc:

   <% using(Html.BeginForm("Retrieve", "Home")) %>//Retrieve is the name of the action while Home is the name of the controller
   <% { %>
        <%foreach (var app in newApps)              { %>  
      <tr> 
           <td><%=Html.CheckBox(""+app.ApplicationId )%></td>      

       </tr>  
    <%} %>
   <input type"submit"/>
 <% } %>

then on the controller you could access the information like this:

   public ActionResult Retrieve()
   {
    //since all variables are dynamically bound you must load your DB into strings in a for loop as so:
  List<app>=newApps;
  for(int i=0; i<app.Count;i++)
  {


    var checkbox=Request.Form[""+app[i].ApplicationId];
    // the reason you check for false because the Html checkbox helper does some kind of freaky thing for value true: it makes the string read "true, false"
      if(checkbox!="false")
      {
      //etc...almost same for other parameters you want that are in thr form
      }

   }
//of course return your view
return View("Index");//this vaires by the name of your view ex: if Index.aspx
  }

This site gives more details on how to check information on the controller as controls are handled in the view: http://quickstarts.asp.net/previews/mvc/mvc_HowToRenderFormUsingHtmlHelpers.htm

TStamper
+1  A: 

If you're planning to submit this form, and do anything with the checkbox values on the server side, you'll want to give them name and value attributes (and the name should be the same for each checkbox in the group). The checkbox helper that TStamper mentioned takes care of that for you.

If you just want a client action to happen when the checkbox is ticked or unticked, you can do something like this (I'm assuming that these objects have some kind of key field; I'm calling it MessageID):

<script type="text/javascript">
function handleCheckbox( theBox ) {
  if( theBox.checked) {
    // do checked stuff
  }
  else {
    // do un-checked stuff
  }
}
</script>

...

<input type="checkbox" value="<%= w.MessageID %>" onclick="handleCheckbox(this)"  />

Please note that id values are required to be unique within an HTML document. So creating lots of checkboxes with the ID "Checkbox1" is not a good idea. (If that input element were runat="server", then .NET would generate a unique HTML id from the WebForms ID.

Sixten Otto