asp.net update trigger from multiple controls in gridview
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 con开发者_如何学Ctrols (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'?
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.
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>
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.
精彩评论