Rodricks
Rodricks

Reputation: 107

Radiobuttons in different groups getting enabled on PostBack

The issue I am having is I have two groups of radio buttons. When I click "ALL" in either of them the SharePoint DatePicker Control has to be disabled with a showing custom date (as shown in the image). But as you see the first one got enabled when I clicked on the second group of radiobuttons. Would appreciate if you help me with this issue.

Thanks.

enter image description here


Below are two groups of radio buttons:

//First Group

 <asp:RadioButton ID="Rd4Month" Text="All" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
  <asp:RadioButton ID="Rd1Month" Text="1 month" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
  <asp:RadioButton ID="Rd2Month" Text="3 month" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
  <asp:RadioButton ID="Rd3Month" Text="6 month" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
  <asp:RadioButton ID="Rd5Month" Text="Other" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />

//Second Group

<asp:RadioButton ID="Rd4BuildMonth" AutoPostBack="True" Text="All" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
 <asp:RadioButton ID="Rd1BuildMonth" AutoPostBack="True" Text="1 month" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
 <asp:RadioButton ID="Rd2BuildMonth" AutoPostBack="True" Text="3 month" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
  <asp:RadioButton ID="Rd3BuildMonth" AutoPostBack="True" Text="6 month" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
  <asp:RadioButton ID="Rd5BuildMonth" AutoPostBack="True" Text="Other" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />

//SharePoint DateTimeControl

   <table>
   <tr>
   <td style="vertical-align: middle;">
   <asp:Label ID="Label4" runat="server" Text="Start:"></asp:Label>
    </td>
    <td style="vertical-align: middle;">
    <SharePoint:DateTimeControl ID="dtcStartDate" runat="server" DateOnly="True" />
    </td>
    <td style="vertical-align: middle;">
    <asp:Label ID="Label5" runat="server" Text="End:"></asp:Label>
    </td>
    <td style="vertical-align: middle;">
    <SharePoint:DateTimeControl ID="dtcEndDate" runat="server" DateOnly="True" />
    </td>
    </tr>
    </table>

And the codebehind:

  protected void rdgetDateforMonths(object sender, EventArgs e)
    {
        //dtcStartDate.Enabled = true;
       // dtcStartDate.ClearSelection();
       // ViewState["rd4Month"] = "false";
        if (Rd1Month.Checked)
        {
            dtcStartDate.ClearSelection();
            dtcStartDate.Enabled = true;
            dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
            //  Label1.Text = DateTime.Now.AddMonths(-1).ToString();
        }

        if (Rd2Month.Checked)
        {
            dtcStartDate.ClearSelection();
            dtcStartDate.Enabled = true;
            dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-3);
        }

        if (Rd3Month.Checked)
        {
            dtcStartDate.ClearSelection();
            dtcStartDate.Enabled = true;
            dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-6);
        }

        if (Rd4Month.Checked)
        {
            dtcStartDate.ClearSelection();
            DateTime value = new DateTime(2012, 04, 01);
            dtcStartDate.SelectedDate = value;
            dtcStartDate.Enabled = false;
         //   ViewState["rd4Month"] = "true";
        }

        if (Rd5Month.Checked)
        {
            dtcStartDate.Enabled = true;
            dtcStartDate.ClearSelection();
        }

    }

 protected void rdgetBuildDateforMonths(object sender, EventArgs e)
    {


       // dtcBuildStartDate.ClearSelection();
      //  dtcBuildStartDate.Enabled = true;
     //   ViewState["rd4BuildMonth"] = "false";
        if (Rd1BuildMonth.Checked)
        {
            dtcBuildStartDate.Enabled = true;
            dtcBuildStartDate.ClearSelection();
            dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-1);

        }

        if (Rd2BuildMonth.Checked)
        {
            dtcBuildStartDate.ClearSelection();
            dtcBuildStartDate.Enabled = true;
            dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-3);

        }

        if (Rd3BuildMonth.Checked)
        {
            dtcBuildStartDate.Enabled = true;
            dtcBuildStartDate.ClearSelection();
            dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-6);

        }

        if (Rd4BuildMonth.Checked)
        {
            dtcBuildStartDate.Enabled = false;
            dtcBuildStartDate.ClearSelection();
            DateTime value = new DateTime(2012, 04, 01);
            dtcBuildStartDate.SelectedDate = value;
            dtcBuildStartDate.Enabled = false;
          //  ViewState["rd4BuildMonth"] = "true";
        }

        if (Rd5BuildMonth.Checked)
        {
            dtcBuildStartDate.Enabled = true;
            dtcBuildStartDate.ClearSelection();

        }
    }

      protected void Page_Load(object sender, EventArgs e)
    {
        //dtcStartDate.MaxDate = System.DateTime.Today;
        dtcBuildStartDate.MaxDate = DateTime.Now.AddMonths(-1);
        dtcStartDate.MaxDate = DateTime.Now.AddMonths(-1);
        dtcEndDate.MaxDate = System.DateTime.Today;
        dtcBuildEndDate.MaxDate = System.DateTime.Today; ;
        lblErrorMsg.Text = "";

        // When the page loads 1st time

        if (!Page.IsPostBack)
        {
            try
            {
                Rd1Month.Checked = true;
                Rd1BuildMonth.Checked = true;
                dtcEndDate.SelectedDate = System.DateTime.Today; // set end date calendar to today's date
                dtcBuildEndDate.SelectedDate = System.DateTime.Today;
                dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
                dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
            }

            catch (Exception ex)
            {
                lblErrorMsg.Text += ex.Message;
            }

        }
    }

After I modified the Enable for DateTimeControl to False

  <SharePoint:DateTimeControl ID="dtcBuildStartDate" runat="server" DateOnly="True" Enabled="False" />

enter image description here

Upvotes: 1

Views: 502

Answers (1)

Nick Bray
Nick Bray

Reputation: 1963

After reviewing the Page_Load method that you just added and reading the comment to my previous post. It looks like the problem is with the way the share point date time control acts. You can try adding EnableViewState="true".

<SharePoint:DateTimeControl ID="dtcBuildStartDate" runat="server" DateOnly="True" Enabled="False" EnableViewState="true" />

If viewstate is not turned on then every time your page posts back it reloads the default value from the aspx page definition. This would also affect things like the control's value not being maintained after a postback etc. If the enableviewstate doesn't work then you could try adding storing the state yourself in ViewState then on each postback you would have to manage this value, but it might get complicated with dealing with changes etc.

You can also try the following on page load:

protected void Page_Load(object sender, EventArgs e)
        {
            //dtcStartDate.MaxDate = System.DateTime.Today;
            dtcBuildStartDate.MaxDate = DateTime.Now.AddMonths(-1);
            dtcStartDate.MaxDate = DateTime.Now.AddMonths(-1);
            dtcEndDate.MaxDate = System.DateTime.Today;
            dtcBuildEndDate.MaxDate = System.DateTime.Today; ;
            lblErrorMsg.Text = "";

            // When the page loads 1st time

            if (!Page.IsPostBack)
            {
                try
                {
                    Rd1Month.Checked = true;
                    Rd1BuildMonth.Checked = true;
                    dtcEndDate.SelectedDate = System.DateTime.Today; // set end date calendar to today's date
                    dtcBuildEndDate.SelectedDate = System.DateTime.Today;
                    dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
                    dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
                }

                catch (Exception ex)
                {
                    lblErrorMsg.Text += ex.Message;
                }

            }

            SetDateBoxEnabled();
        }

        private void SetDateboxEnabled()
        {
            if (Rd4BuildMonth.Checked)
            {
                dtcBuildStartDate.Enabled = false;
            }
            else
            {
                dtcBuildStartDate.Enabled = true;
            }

            if (Rd4Month.Checked)
            {
                dtcStartDate.Enabled = false;
            }
            else
            {
                dtcStartDate.Enabled = true;
            }
        }

Upvotes: 1

Related Questions