Hello,

I am opening a calendar popup when user clicks on a button but here I have to click twice to get this calendar. On the 1st click it doe not do anything but on 2nd click it shows up the calendar.
here is my code-

<script type="text/javascript" >
    function openCalender_range_start_date() {
        window.open('popupcalendar.aspx?textbox=TextBox2', 'cal', 'width=220,height=200,left=270,top=180')
}

and on popupcalendar.aspx-

protected void Change_Date(System.Object sender, System.EventArgs e)
{
    if (Request.QueryString["TextBox2"] != "")
  {
    string strScript = 
        "<script>window.opener.document.forms(0)." + 
        Request.QueryString["textbox"].ToString() + ".value = '" +
        Calendar1.SelectedDate.ToString("MM/dd/yyyy") +
        "';self.close()" +
        "</" + "script>"; 
    RegisterClientScriptBlock("Calendar_ChangeDate", strScript);

}
}

and on the button- Button2.Attributes.Add("onclick", "openCalender_range_start_date();"); Can somebody tell me what I am doing wrong?

Dani AI

Generated

The behavior saw is exactly what suggested: the client onclick handler is being added only after the first server-side click. That first click causes a postback (so nothing visible happens client-side), the attribute gets added during the postback, and the second click finally runs the client script. Fix by wiring the client-side open before the user ever clicks.

Two simple fixes (pick one):

  • Put the client handler in the markup (no postback):
<asp:Button ID="btnOpen" runat="server" Text="Open"
    OnClientClick="openCalendarPopup(); return false;" />
  • Or assign the client handler on initial page load in code-behind:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        btnOpen.OnClientClick = "openCalendarPopup(); return false;";
    }
}

Both approaches ensure the popup is opened immediately on the first click. The return false; prevents the normal postback; if a postback is required after opening the popup, return true (or omit return false) and coordinate the flow on the server.

On the popup page, make the reply-to target robust: pass the target control's client id and guard the query string before using it. Example pattern:

string target = Request.QueryString["target"];
if (!string.IsNullOrEmpty(target))
{
    string script = "<script>"
        + "var el = window.opener.document.getElementById('" + target + "');"
        + "if(el) el.value = '" + Calendar1.SelectedDate.ToString("MM/dd/yyyy") + "';"
        + "window.close();</script>";
    ClientScript.RegisterStartupScript(this.GetType(), "setDate", script);
}

Extra tips: use the control's ClientID when building the popup URL to avoid ASP.NET naming-container mismatches. If the button must sometimes do server work and sometimes just open a popup, use OnClientClick to open and then return true/false depending on whether you want the postback to follow. See the ASP.NET Button OnClientClick docs and ClientScript.RegisterStartupScript for details: OnClientClick and RegisterStartupScript.

Recommended Answers

All 5 Replies

When you say

Hello,
and on the button- Button2.Attributes.Add("onclick", "openCalender_range_start_date();");

Can you show the whole line? it could be how you've placed this code.

that's what i have as my complete line of code.

Ok but where is that line of code? if its in the "onclick" that would explain it

My code is-

protected void Button2_Click1(object sender, EventArgs e)
{


Button2.Attributes.Add("onclick", "openCalender_range_start_date();");
}

What is that I am doing wrong?

exactly what I suggested you'd done

the first time you click it, you then tell it on clicking it to do something else, so its not there the first time... but is the second..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.