none
checkedchanged event for checkbox is not firing in a webPart

    Question

  • Hello

     

    I am developing a simple webpart for sharepoint page in visual studion. i want to change the text of textbox on selection of checkbox, but checkedchanged event for checkbox is not firing, eventhough i set the autopostback property to true.

     

    McBox.AutoPostBack = true;

    McBox.CheckedChanged += new EventHandler(McBox_CheckedChanged);

    void McBox_CheckedChanged(object sender, EventArgs e)

    {

    int m = int.Parse(MetricTons.Text);

    int rate = int.Parse(MRate.Text);

    int freight = m * rate;

    GrossFreight.Text = freight.ToString();

     

    }

    GrossFreight is not changing after checking the checkbox.

     

    Any suggestions

    Wednesday, September 26, 2007 8:35 AM

All replies

  • I have a very similar problem and would be interested to find out if anyone has found a solution.

     

    Wednesday, March 26, 2008 7:59 AM
  • This is a somewhat late response but I had the same problem and I managed to fix it by setting the web part's

    ViewState to dirty via

     

    this.ViewState.SetDirty(true);

     

    Whether this is the best way of doing things, I'm not sure - I am also open to suggestions.

     

    Wednesday, June 18, 2008 6:51 PM
  • Are you creating the CheckBox control in the WebPart's CreateChildControls method?

    Wednesday, June 18, 2008 7:23 PM
  • I can't speak for the original poster but in my case, it was actually a System.Web.UI.WebControls.ListBox and yes, that was being created within the CreateChildControls() method.
    Wednesday, June 18, 2008 8:36 PM
  • Hi,
          Looks like you have not set ID property for this CheckBox contorl. I faced the same issue . I fixed this issue by setting the control id.


    try ... 

    McBox.AutoPostBack = true;

    McBox.ID="<any name>";

    McBox.CheckedChanged +=

    new EventHandler(McBox_CheckedChanged);
    .............
    .............

    Anand Kesavan

     

    • Edited by Anand Kesavan Tuesday, November 10, 2009 9:27 AM Updated
    Monday, November 09, 2009 6:19 PM
  • Usually the event handlers dont get called because the control is not created when the post back occurs or does not get created at the right time.

    If it is not created at the right time, the control hierarchy that is build during the request is different than the one built during the first request. Thus the handler does not get called. Make sure the control is being created during the post back also.
    Twitter: @SharePointLife
    Monday, November 09, 2009 9:50 PM
  • Hey Needo, try using this code:

    cbAddClient = new CheckBox();

    cbAddClient.ID =

    "AddNewCustomer";

    cbAddClient.Text =

    "Add New Customer";

    cbAddClient.AutoPostBack =

    true;

    cbAddClient.EnableViewState =

    true;

    cbAddClient.CheckedChanged +=

    new EventHandler(cbAddClient_CheckedChanged);

     

    //event code

     

    void cbAddClient_CheckedChanged(object sender, EventArgs e)

    {

     

    if (sender is CheckBox)

    {

     

    CheckBox cb = sender as CheckBox;

     

    if (cb.Checked)

    {

    cb.Text =

    "No Add New Customer";

    }

     

    else

    {

    cb.Text =

    "Add New Customer";

    }

    }

    }

     

    regards...

    Thursday, July 15, 2010 4:10 PM
  • set AutoPostaback=true for the checkbox.
    Saturday, July 30, 2011 11:08 AM