Using the Ajax Toolkit Mutually Exclusive Checkbox Extender inside a Gridview

Last post 10-24-2009 9:27 AM by jboconne. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 10-21-2009 10:56 AM

    • jboconne
    • Top 200 Contributor
    • Joined on 04-08-2009
    • Wannabe Slacker
    • Points 44

    Using the Ajax Toolkit Mutually Exclusive Checkbox Extender inside a Gridview

    Hello,

    I'm new to dotnet Slackers and relatively new to ASP.Net/AJAX Web application development so please bear with me.

    Ok, here is my scenario. I have a Gridview and it contains approx. 10 columns. In 2 of the columns, I'm dynamically displaying a checkbox; one for "Send" and the other "Delete". I want for each gridview row, to be able to check or uncheck the checkbox but make it mutually exclusive to the other checkbox column. For instance, If the user selects the "Send" checkbox and the "Delete" checkbox is not selected, then if the user selects the "Delete" checkbox, it deselects the "Send" checkbox. 

    I have included the Ajax Mutually Exclusive checkbox extender to accomplish this and it works for each gridview row great, however,
    here's the problem I'm having:
    When I select a checkbox on a different Gridview row, it deselects the other selected checkbox in the different Gridview Rows.

    I need the mutually exclusive checkbox extender to be mutually exclusive for each Gridview Row too.

    Here's what I currently have for the existing code:

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    <

    ItemTemplate>
    <asp:CheckBox ID="chkSend" runat="server" Font-Size="small" OnCheckedChanged="tosend_CheckedChanged" Checked="<%# GetChkBoxSendValue(Container) %>" AutoPostBack="True" />
    <cc1:MutuallyExclusiveCheckBoxExtender ID="chkSendExtender" runat="server" TargetControlID="chkSend" Key="SendOrDelete"/>
    </ItemTemplate>

     

     

    <ItemTemplate>
    <asp:CheckBox ID="chkDelete" runat="server" Font-Size="small" OnCheckedChanged="todelete_CheckedChanged"Checked="<%# GetChkBoxDeleteValue(Container) %>" AutoPostBack="True" />
    <cc1:MutuallyExclusiveCheckBoxExtender ID="chkDeleteExtender" runat="server" TargetControlID="chkDelete" Key="SendOrDelete"/>
    </ItemTemplate>

    Any assistance with this matter would be greatly appreciated.

    Jeff O'Connell

  •  Advertisement

    Featured Advertisement

     
  • 10-21-2009 11:05 AM In reply to

    • Sonu
    • Top 10 Contributor
    • Joined on 05-22-2006
    • Montreal / Canada
    • Slacker
    • Points 12,183
    • MVP

    Re: Using the Ajax Toolkit Mutually Exclusive Checkbox Extender inside a Gridview

    Can you show me tosend_CheckedChanged and todelete_CheckedChanged?

    [MVP since 2005] [MCAD]
    Webmaster of DotNetSlackers
    Question or Suggestion?
    Feel free to ask my any .NET question
    Our Posting FAQ
  • 10-21-2009 6:51 PM In reply to

    • jboconne
    • Top 200 Contributor
    • Joined on 04-08-2009
    • Wannabe Slacker
    • Points 44

    Re: Using the Ajax Toolkit Mutually Exclusive Checkbox Extender inside a Gridview

     Sonu,

     

    Thanks for responding, I did a work around and actually used those 2 events to make the checkboxes mutually exclusive for each gridview row without using the Ajax Mutually Exclusive Checkbox Extender.

    Here's the code for anyone else who needs a solution to the same problem;

    protected void tosend_CheckedChanged(object sender, EventArgs e)
    {
       CheckBox checkbox1 =  (CheckBox)sender;
      
    GridRow gridrow1 = (GridRow)checkbox1.Parent.Parent.Parent;
       gridrow1.Record.SetValue(
    "tosend", (checkbox1.Checked?1:0));
      
    CheckBox delcheck = (CheckBox)gridrow1.FindControl(gridrow1.ID.ToString()+"Cell2").Controls[0].Controls[1];
       
       if
    (checkbox1.Checked)
       {
           gridrow1.Record.SetValue(
    "todelete", 0);
            delcheck.Checked =
    false;
       }

     }

     

     

    protected void todelete_CheckedChanged(object sender, EventArgs e)
    {
        
    CheckBox checkbox1 = (CheckBox)sender;
        
    GridRow gridrow1 = (GridRow)checkbox1.Parent.Parent.Parent;
          gridrow1.Record.SetValue(
    "todelete", (checkbox1.Checked ? 1 : 0));
        
    CheckBox Sndcheck = (CheckBox)gridrow1.FindControl(gridrow1.ID.ToString() + "Cell1").Controls[0].Controls[1];
        
        
    if (checkbox1.Checked)
         {
            gridrow1.Record.SetValue(
    "tosend", 0);
            Sndcheck.Checked =
    false;
          }
    }

  • 10-21-2009 9:28 PM In reply to

    • Sonu
    • Top 10 Contributor
    • Joined on 05-22-2006
    • Montreal / Canada
    • Slacker
    • Points 12,183
    • MVP

    Re: Using the Ajax Toolkit Mutually Exclusive Checkbox Extender inside a Gridview

    Very good. However I think you could have solved that with a single function:

    protected void oncheck(object sender, EventArgs e)
    {
       string oppositeCheckboxId = "chkSend";
       string action = "tosend";

       CheckBox checkbox1 =  (CheckBox) sender;
       if (checkbox1.Id == "chkSend")
       {
           oppositeCheckboxId = "chkDelete";
           action = "todelete";
       }
     
       GridRow gridrow1 = (GridRow)checkbox1.Parent.Parent.Parent;
       gridrow1.Record.SetValue(action, (checkbox1.Checked ? 1:0));  
     
       if (checkbox1.Checked)
       {
           CheckBox oppositeCheckbox = (CheckBox)gridrow1.FindControl(otherCheckboxId);     
           gridrow1.Record.SetValue((checkbox1.Id == "chkSend" ? "todelete" : "tosend"), 0);
           oppositeCheckbox.Checked =false;
       }
    }

    In the above code, I simply check the sender.Id. If the sender is the "chkSend" checkbox, then we need to change the opposite checkbox which is "chkDelete". Then I simply save the id in a variable and use that to get the checkbox, which I can simply then uncheck. Based on the variables I can also use the SetValue.

    This code may contain compiler errors - I wrote it in notepad :)

    [MVP since 2005] [MCAD]
    Webmaster of DotNetSlackers
    Question or Suggestion?
    Feel free to ask my any .NET question
    Our Posting FAQ
  • 10-22-2009 10:54 AM In reply to

    • jboconne
    • Top 200 Contributor
    • Joined on 04-08-2009
    • Wannabe Slacker
    • Points 44

    Re: Using the Ajax Toolkit Mutually Exclusive Checkbox Extender inside a Gridview

     Sonu,

    Thank you very much for the suggestion and your help regarding this matter. I will try this in my code.

    Thanks

    Jeff

  • 10-23-2009 5:54 PM In reply to

    • mkadlec
    • Not Ranked
    • Joined on 10-23-2009
    • Seattle, WA
    • Wannabe Slacker
    • Points 30

    Re: Using the Ajax Toolkit Mutually Exclusive Checkbox Extender inside a Gridview

    Jeff,

      Are you ok with the postback though?  If you are doing it without Ajax you'll be getting a postback which might not be very good for the user experience.  If the asp.net Ajax control isn't working like you want, you could still do what you need using pure Javascript.


    Mark.

    Filed under:
  • 10-24-2009 9:27 AM In reply to

    • jboconne
    • Top 200 Contributor
    • Joined on 04-08-2009
    • Wannabe Slacker
    • Points 44

    Re: Using the Ajax Toolkit Mutually Exclusive Checkbox Extender inside a Gridview

     Thanks Mark.

    I should clarify. I'm still using AJAX (update panel) for the page postbacks, I'm just not using the Mutually Exclusive Checkbox extender from the Ajax toolkit to handle the mutual exclusivity of two checkboxes in the same gridview row. I need to save the state of the checkbox (checked or not) when the user is using paging with the Gridview so I built the mutual exclusive checkbox functionalty into the same server-side event that does that.

    Thanks again for your interest and follow-up.
    Jeff

     

Page 1 of 1 (7 items)