2

I have a gridview with multiple checkboxes, sometimes 40+. I want the checkboxes to call a .net function in the code behind when clicked. Normally this would be simple if the checkboxes were outside of the gridview. But because they are in the gridview and I don't know how many there are, I don't know their id's before runtime.

I am at the point right now that when you click a checkbox, a jQuery script returns the id and value of the checkbox. Unfortunately, I don't know how to pass the value to a function - that will in turn update another gridview.

AFAIK my only option is to get the id's of the controls (checkboxes) before page load and to programatically add then to the update trigger portion of the update panel. Is there any other way to say 'if any checkboxes are clicked, run this'?

3 Answers 3

2

Use a CSS selector for the checkboxes and call __doPostBack to refresh the panel, e.g. assign a CSS class checkboxes to each checkbox, then add script:

$('#checkboxes').change(function() {
  __doPostBack('<%=UpdatePanel1.UniqueID%>');
}

if you are interested in the ID of the checkbox that caused the refresh, you can pass it back to the code:

$('#checkboxes').change(function() {
  __doPostBack('<%=UpdatePanel1.UniqueID%>',$(this).attr('id'));
}

The 2nd parameter will be available as Request.Form["__EVENTARGUMENT"]. You can put anything you want here, e.g. you could also include the state of the checkbox if you wanted. The first is __EVENTTARGET, but you don't need to use that directly. When it matches an UpdatePanel's unique ID, just that panel will be refreshed.

Sign up to request clarification or add additional context in comments.

3 Comments

jamie you helped again! This is interesting and this is more jQuery than my method which is what I want. I have to try it to see how it works, but would this 'mimic' the AJAX trigger and pass to the function in my code behind?
UpdatePanel "triggers" are really a very simple construct -- internally there's just a list of things that cause each UpdatePanel to refresh. Registering a trigger just adds it to the list for that UpdatePanel. Every ASP.NET postback control calls __doPostBack (or __doPostBackWithOptions) to perform its submit when using AJAX/ScriptManager. So... all you are doing is calling it yourself. This is identical to registering a postback control as a trigger for a panel. You can't directly pass control to a server eventhandler this way, though, since there's no server control (continued...)
Instead use __EVENTARGUMENT to set a flag that you can check in OnLoad, for example, to see what happened at the client or how you got there. Nothing stopping you from passing complex data in this flag (e.g JSON data), you can also check to see if __EVENTTARGET matches the UpdatePanel.UniqueID and if so do something from there.
1

You could use UpdatePanel.ChildrenAsTriggers.

<asp:UpdatePanel ID="UpdatePanel1" runat="server"  UpdateMode="Conditional" ChildrenAsTriggers="true"">
    <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server">
         ...
         ...
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

2 Comments

thanks, another good suggestion. I think I looked at this and got stuck. Will a checking a checkbox cause a postback?
@IMAbev it sure will! Make sure it's AutoPostBack is set to True
1

In such cases I usually have a HiddenField and a button with style='display:none;' and set that as the trigger of the UpdatePanel.

The different controls will call a jQuery thing as you mention that updates the HiddenField with proper value based on which control was raising this (which is the checkbox in your case).
Then, it will click the CSS-hidden button ($('#<%=myButton.ClientID %>').click();). This should trigger the update of the UpdatePanel, and I can do any logic I want in the button Server-side Click event too.

2 Comments

I have to look at this further so I better understand it. Since my checkboxes are in a nested gridiview, will I have the same issue with finding control ID?
Not really. You can add some CSS class to all checkboxes, then use jQuery to apply the JS handler to all them using this class. Inside the JS itself, you can use this to express the checkbox that raised it, and $(this) to call jQuery methods against it, so that you can get the ID of the checkbox element from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.