tags:

views:

70

answers:

1

I am displaying a list of data in the following manner;

Surname Forename Email Address Email Distribution Ops Manager Sign Off Admin Inputter
<div class="pager">
    <%: Html.PageLinks(Model.PagingInfo, x=> Url.Action("List", new { page = x })) %>
</div>

and in the partial view;

   <tr>
        <td>
            <%: Html.ActionLink("Edit", "Edit", new { id=Model.UserId }) %> |
            <%: Html.ActionLink("Details", "Details", new { id=Model.UserId })%> |
            <%: Html.ActionLink("Delete", "Delete", new { id=Model.UserId })%>
        </td>
        <td>
            <%: Model.Surname %>
        </td>
        <td>
            <%: Model.Forename%>
        </td>
        <td>
            <%: Model.EmailAddress%>
        </td>
        <td>
            <%: Html.CheckBoxFor(x => x.EmailDistributionListFlag) %>
        </td>
        <td>
            <%: Html.CheckBoxFor(x => x.OperationsManagerFlag)%>
        </td>
        <td>
            <%: Html.CheckBoxFor(x => x.SignOffManager)%>
        </td>
        <td>
            <%: Html.CheckBoxFor(x => x.AdministratorFlag)%>
        </td>
        <td>
            <%: Html.CheckBoxFor(x => x.InputterFlag)%>
        </td>
     </tr>

I want to put a click event on each checkbox, so when the checkbox is clicked I can do a database update. However I do not appear to have a unique client name for each checkbox. So I would like to know how to put the click event on the checkbox, and how do I know which user row has been clicked in relation to the checkbox.

+1  A: 

Try to set the checkbox id this way:

Html.CheckBoxFor(x => x.EmailDistributionListFlag, new { id = "checkBoxEmailDistributionListFlag" })

And then you can access it by jquery:

$("#checkBoxEmailDistributionListFlag").click(function(){ ... });

Update:

You missed a few brackets in your js:

$(document).ready(function ()
                  {
                      $("#checkBoxEmailDistributionListFlag").click(function ()
                                         {
                                             alert("done!");
                                         }) // <-- Those were missing
                  });

And make sure you have jquery included.

Update2

To quickly create checkboxes with uniques Ids and bind a function:

Html.CheckBoxFor(x => x.EmailDistributionListFlag, new
                         {
                             id = "checkBoxEmailDistributionListFlag_" + Model.UserId,
                             onclick = "EmailClickFunc(" + Model.UserId + ");"
                         })

And you can define your EmailClickFunc in js:

<script type="text/javascript">
    function EmailClickFunc(id) { alert(id); }
</script>
Yakimych
I am not sure how I know with this click event what the UserId is. In any case I made the change and I put in the click event at the end of the partial view, and nothing happened; <script type="text/javascript">//<![CDATA[ $(document).ready(function () { $("#checkBoxEmailDistributionListFlag").click(function () { alert("done!"); });//]]> </script>
arame3333
Check the generated Html first. Does the checkbox for EmailDirstributionListFlag has it's 'id' attribute set?
Yakimych
It does. However the ID is the same on each row (this is in a grid after all). I suspect the ID should be unique, but how do I make this happen?
arame3333
As an experiment I tried with 1 row, but that did not work either.
arame3333
Is it the UserId you are talking about? You can append it to the checkBox id value: Html.CheckBoxFor(x => x.EmailDistributionListFlag, new { id = "checkBoxEmailDistributionListFlag_" + Model.UserId }).As a quick solution you can also try setting the 'click' function directly:Html.CheckBoxFor(x => x.EmailDistributionListFlag, new { id = "checkBoxEmailDistributionListFlag", click = "EmailClickFunc(" + Model.UserId + ")" }), and define your function in js:<script type="text/javascript">function EmailClickFunc(id) { alert(id); } </script>
Yakimych
I put the script at the bottom of the main page but no luck. I have tried putting breakpoints in Firebug but they are not being fired.
arame3333
Can you post the generated page Html (for a one-row grid for starters)? It's usually easy to spot when analyzing that directly.
Yakimych
I will have to do this in parts, because of character restrictions in these comments.
arame3333
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title> List</title><link href="../Content/Site.css" rel="stylesheet" type="text/css" /> <script src="http://ajax.microsoft.com/ajax/jquery-1.4.1.min.js" type="text/javascript"></script></head>
arame3333
<body> <div class="page"> <table> <tr> <th></th> <th> Surname </th> <th> Forename </th> <th> Email Address </th> <th> Email Distribution </th> <th> Ops Manager </th> <th> Sign Off </th> <th> Admin </th> <th> Inputter </th> </tr>
arame3333
<tr> <td> <a href="/Users/Edit/0">Edit</a> | <a href="/Users/Delete/0">Delete</a> </td> <td> Smith </td> <td> Sue </td> <td> </td>
arame3333
<td> <input click="TestClickFunc(0)" id="checkBoxEmailDistributionListFlag" name="EmailDistributionListFlag" type="checkbox" value="true" /><input name="EmailDistributionListFlag" type="hidden" value="false" /> </td> <td> <input id="OperationsManagerFlag" name="OperationsManagerFlag" type="checkbox" value="true" /><input name="OperationsManagerFlag" type="hidden" value="false" /> </td> </tr> </table>
arame3333
<div class="pager"> <a class="selected" href="/Users/List?page=1">1</a> </div> <script type="text/javascript"> function TestClickFunc(id) { alert(id); } </script> <div id="footer"> </div> </div> </div></body></html>
arame3333
You really should have posted this as part of the question ;)But regarding the code - it's my bad, made a silly typo. Instead of "click = "EmailClickFunc(" + Model.UserId + ")", try "onclick = ...".
Yakimych
Yes, onclick did it!
arame3333
I will add the info to the answer then, hold on.
Yakimych
Thank you for your help
arame3333
Sure. Good luck!
Yakimych