dimanche 22 janvier 2017

DataBound ASP:CheckBox events not triggering as expected bacause of DataBinding occuring on Page_Load()

I am stuck into an ASP page.

The page contains an ASP:Container, containing several CheckBoxes. I need to add an event on these CheckBoxes OnCheckChange so whenever we click one of the CheckBoxes, it unchecks all of the other checkboxes. I could use a RadioButton, but using CheckBoxes has other advantages for me that are out of this subject so I do not want to use radio buttons.

That would theorically work fine just like this:

<ASP:Repeater id="MyRepeater" runat="server" OnItemDataBound="MyRepeater_ItemDataBound">
    <ItemTemplate>
        <asp:CheckBox id="MyCB" runat="server" OnCheckedChanged="MyCB_CheckedChanged" AutoPostBack="true" />
        <br/>
    </ItemTemplate>

    </ASP:Repeater>

and into the code behind:

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MyRepeater.DataSource=DS;
            MyRepeater.DataBind();
        }
    }


  protected void MyCB_CheckedChanged(object sender, EventArgs e)
    {
       UncheckallOtherCheckBoxes();
    }

However, to have other regions of this page to work as expected, I do have to bind the DataSource on every page load and not only the first time. So I need to change Page_Load to

 protected void Page_Load(object sender, EventArgs e)
    {
     //   if (!IsPostBack)
        {
            MyRepeater.DataSource=DS;
            MyRepeater.DataBind();
        }
    }

And this causes everything to fail! :) Because when I click on a checkbox, it triggers Page_Load before MyCB_CheckedChanged(). Si it DataBinds first and thus, the databinding sends some checkboxes events that are not the ones performed by the user.

Is there a way to solve this????

Thx in advance.




Aucun commentaire:

Enregistrer un commentaire