4

Here i am using a radio button to calculate the date difference between two textboxes and i am showing it in another textbox.It is working only for the first time when i click the radio button after that it is not working..Here is my code

<asp:RadioButton ID="rdoSpecifiedDates"  runat="server" class="bodycontent"   GroupName="status"/>
<asp:RadioButton ID="rdoDateRange" runat="server" class="bodycontent" 
GroupName="status"  oncheckedchanged="rdoDateRange_CheckedChanged" AutoPostBack="true"
/>

 <asp:UpdatePanel ID="Update" runat="server">
 <ContentTemplate>
 <asp:TextBox ID="txtDays" runat="server" CssClass="bodycontent" MaxLength="6" ReadOnly="true"></asp:TextBox>
 </ContentTemplate>
 <Triggers>
 <asp:AsyncPostBackTrigger ControlID="rdoDateRange" />
 </Triggers>
 </asp:UpdatePanel>

and

protected void rdoDateRange_CheckedChanged(object sender, EventArgs e)
        {
            DateTime startdate=Convert.ToDateTime(txtOStartDate.Text);
            DateTime enddate=Convert.ToDateTime(txtOEndDate.Text);
            var result = (enddate - startdate).TotalDays;
            txtDays.Text =Convert.ToString( result);
        }

Any suggestion?

bala3569
  • 10,832
  • 28
  • 102
  • 146

3 Answers3

4

its happening because you are forcing only post back on rdoDateRange...and when the other rdoSpecifiedDates is clicked no postback occur so that's why you rdoDateRange dose not reflect any change..

So make the rdoSpecifiedDates AutoPostBack = true.

hmm...either you have to put both rdobuttons in your trigger.. like this

   <asp:RadioButton ID="rdoSpecifiedDates" runat="server" AutoPostBack="true" class="bodycontent" GroupName="status" />
        <asp:RadioButton ID="rdoDateRange" runat="server" class="bodycontent" GroupName="status"
            OnCheckedChanged="rdoDateRange_CheckedChanged" AutoPostBack="true" />
        <asp:UpdatePanel ID="Update" runat="server">
            <ContentTemplate>
                <asp:TextBox ID="txtDays" runat="server" CssClass="bodycontent" MaxLength="6" ReadOnly="true"></asp:TextBox>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="rdoDateRange" />
                <asp:AsyncPostBackTrigger ControlID="rdoSpecifiedDates" />
            </Triggers>
        </asp:UpdatePanel>

OR

Put both the radio buttons in update pannel like this..

<asp:UpdatePanel ID="Update" runat="server">
            <ContentTemplate>
                <asp:RadioButton ID="rdoSpecifiedDates" runat="server" AutoPostBack="true" class="bodycontent"
                    GroupName="status" />
                <asp:RadioButton ID="rdoDateRange" runat="server" class="bodycontent" GroupName="status"
                    OnCheckedChanged="rdoDateRange_CheckedChanged" AutoPostBack="true" />
                <asp:TextBox ID="txtDays" runat="server" CssClass="bodycontent" MaxLength="6" ReadOnly="true"></asp:TextBox>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="rdoDateRange" />                
            </Triggers>
        </asp:UpdatePanel>
Tami
  • 576
  • 7
  • 18
  • gave it like that it is not working..Second time when i click that radio button it is not even going to the rdoDateRange_CheckedChanged event. – bala3569 Mar 29 '12 at 11:57
2

You Also can do..:

<div>

   <asp:RadioButton ID="rdoSpecifiedDates" runat="server" class="bodycontent" 
    GroupName="status" oncheckedchanged="rdoSpecifiedDates_CheckedChanged" AutoPostBack="true" />
    <asp:RadioButton ID="rdoDateRange" runat="server" class="bodycontent" GroupName="status"
        OnCheckedChanged="rdoDateRange_CheckedChanged" AutoPostBack="true" />

 <asp:ScriptManager ID="ScriptManager1" runat="server" />   
    <asp:UpdatePanel ID="Update1" runat="server" UpdateMode="Conditional" >

            <ContentTemplate>
                <asp:TextBox ID="txtDays" runat="server" CssClass="bodycontent" MaxLength="6" Enable="False"></asp:TextBox>
            </ContentTemplate>
        </asp:UpdatePanel>

    </div>

And

protected void rdoDateRange_CheckedChanged(object sender, EventArgs e)
        {
            DateTime startdate = DateTime.Now.AddHours(2);
            DateTime enddate = DateTime.Now.AddHours(5);
            TimeSpan  result = enddate - startdate;
            txtDays.Text = result.ToString();
            Update1.Update();
        }
 protected void rdoSpecifiedDates_CheckedChanged(object sender, EventArgs e)
        {

        }
Gabriel Scavassa
  • 576
  • 1
  • 4
  • 21
1

If your radio buttons are outside of the update panel (and need to be so), the selected/checked item will never fire due to not having any JavaScript on its <input> tag, which is logical in the sense that the page thinks the element is already selected so why would it need to fire off back to the server when it is clicked?

But it obviously causes issues in this scenario because, as triggers for an update panel, it never "repaints" itself to reflect that the selected item has changed.

My workaround for this is to "repaint" the radio buttons as well by having them in their own <asp:UpdatePanel> control:

<asp:UpdatePanel ID="RadioButtonUpdate" runat="server">
    <ContentTemplate>
        <asp:RadioButton ID="rdoSpecifiedDates"  runat="server" class="bodycontent"
            GroupName="status" OnCheckedChanged="rdoSpecifiedDates_CheckedChanged" AutoPostBack="true" />
        <asp:RadioButton ID="rdoDateRange" runat="server" class="bodycontent" 
            GroupName="status"  OnCheckedChanged="rdoDateRange_CheckedChanged" AutoPostBack="true" />
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel ID="Update" runat="server">
    <ContentTemplate>
        <asp:TextBox ID="txtDays" runat="server" CssClass="bodycontent" MaxLength="6" ReadOnly="true"></asp:TextBox>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="rdoDateRange" />
    </Triggers>
</asp:UpdatePanel>

This feels to me like the cleanest solution outside of housing the radio buttons within the update panel you are wanting refreshed.

DenverCoder9
  • 1,119
  • 3
  • 16
  • 30