Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a calender with dropdowndown list to select month and year.
i didnt use jquery. I dont have any error but the page gets reloaded whenevr i click month, year from dropdown and date from calender.  though values are corrected added reloading is disturbing. what should be done to to stop this?  pls help in  code.

Aspx code:
HTML

XML
<tr>
                       <td>
                            <asp:Label ID="lblExpiry" Font-Bold="true" Font-Size="Larger" runat="server">Expiry</asp:Label>
                       </td>
                       <td>
                                <asp:TextBox runat="server" ID="txtExpiry" />
                       </td>
                      <td>
                           <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/Images/cal.jpg" Height="33px" OnClick="ImageButton2_Click" Width="38px" /></td>
                       <td>
                            <asp:RequiredFieldValidator ID="reqvalExpiry" runat="server" ValidationGroup="valStent" ControlToValidate="txtExpiry"
                                    CssClass="field-validation-error" ErrorMessage="The Expiry field is required." />
                       </td>
                   </tr>
                   <tr>
                        <td>
                       <asp:DropDownList runat="server" ID="ddlMonth" AutoPostBack="true" OnSelectedIndexChanged="Set_Calendar">
    <asp:ListItem Text="January" Value="1" />
    <asp:ListItem Text="February" Value="2" />
    <asp:ListItem Text="March" Value="3" />
    <asp:ListItem Text="April" Value="4" />
    <asp:ListItem Text="May" Value="5" />
    <asp:ListItem Text="June" Value="6" />
    <asp:ListItem Text="July" Value="7" />
    <asp:ListItem Text="August" Value="8" />
    <asp:ListItem Text="September" Value="9" />
    <asp:ListItem Text="October" Value="10" />
    <asp:ListItem Text="November" Value="11" />
    <asp:ListItem Text="December" Value="12" />
</asp:DropDownList>

<asp:DropDownList runat="server" ID="ddlYear" AutoPostBack="true" OnSelectedIndexChanged="Set_Calendar">
    <asp:ListItem Text="2011" Value="2011" />
    <asp:ListItem Text="2012" Value="2012" />
    <asp:ListItem Text="2013" Value="2013" />
    <asp:ListItem Text="2014" Value="2014" />
    <asp:ListItem Text="2015" Value="2015" />
</asp:DropDownList>
                            </td>
                   </tr>
                   <tr>
                       <asp:Calendar ID="Calendar2" runat="server" OnSelectionChanged="Calendar2_SelectionChanged" Font-Size="XX-Small">
                           <TodayDayStyle BackColor="#003366" />

                       </asp:Calendar>
                   </tr>


Code behind:

HTML

C#
protected void Page_Load(object sender, EventArgs e)
        {


            Calendar2.Visible = false;
            //ddlMonth.Visible = false;
            //ddlYear.Visible = false;
            Populate_MonthList();
            Populate_YearList();

            txtExpiry.Enabled = false; }
<pre> protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            txtExpiry.Enabled = true;
            ddlMonth.Visible = true;
            ddlYear.Visible = true;
            Calendar2.Visible = true;
        }
        protected void Populate_MonthList()
        {
            //Add each month to the list
            var dtf = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;
            for (int i = 1; i &lt;= 12; i++)
                ddlMonth.Items.Add(new ListItem(dtf.GetMonthName(i), i.ToString()));

            //Make the current month selected item in the list
            ddlMonth.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
        }


        protected void Populate_YearList()
        {
            //Year list can be changed by changing the lower and upper
            //limits of the For statement
            for (int intYear = DateTime.Now.Year - 20; intYear &lt;= DateTime.Now.Year + 20; intYear++)
            {
                ddlYear.Items.Add(intYear.ToString());
            }

            //Make the current year selected item in the list
            ddlYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;
        }

        protected void Set_Calendar(object Sender, EventArgs e)
        {
            int year = int.Parse(ddlYear.SelectedValue);
            int month = int.Parse(ddlMonth.SelectedValue);
            Calendar2.TodaysDate = new DateTime(year, month, 1);
        }

        protected void Calendar2_SelectionChanged(object sender, EventArgs e)
        {
            txtExpiry.Text = Calendar2.SelectedDate.ToShortDateString();
            Calendar2.Visible = false;
        }
    }
Posted
Updated 2-Mar-15 23:45pm
v2

1 solution

1.Note that each time you click on a control that could generate post-back, in your case buttons or drop down lists, the code behind events are executed, and Load event is always executed.

2.You should separate the two main cases than may occur in the management of your event from your code: first invocation, post back invocation. For doing this you should modify your Load event handler like below:
C#
protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostBack())
     {    
            Calendar2.Visible = false;
            Populate_MonthList();
            Populate_YearList();
            txtExpiry.Enabled = false; 
     }
}
 
Share this answer
 
v3
Comments
Member 11377180 3-Mar-15 8:27am    
inspite of trying this its still reloading.
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
Calendar2.Visible = false;
Populate_MonthList();
Populate_YearList();
txtExpiry.Enabled = false;
}
}
Raul Iloc 4-Mar-15 1:33am    
Each control that has AutoPostBack set on true will generate a postabck, this means that on click/select the entire page will be sent to the web server, and web server will process the events, re-render the page and finally the resulted HTML + CSS will be send back to the browser, and the browser will re-draw the page. To refine/improve this normal behaviour you should/could use AJAX and/or take care in using AutoPostBack properties. Also in the event handler you should manage the 2 cases (IsPostBack true or false) like I suggested in my solution above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900