Search code examples
c#asp.netupdatepanelautopostback

Autopostback is working only for the first time in ASP.NET?


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?


Solution

  • 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>