开发者

Button updates its own OnClientClick and then executes that?

I am a .NET newb so please bear with me.

I am working on an online store. It requires the visitor to choose quantities they want and then click a button to choose a date for those items (tours).

I have two buttons - one reads their choices and updates the OnClientClick value of the other and when that one is clicked it executes a popup. The project requires me to do the whole process with just one button. Here's the rough outline of the particulars:

Setup button:

    //Calculates parameters to send to popup based on control contents.
    public void btnSetup_Click(object sender, EventArgs e)
    {
        StringBuilder sbParams = new StringBuilder();
        TotalQty += basketItem.Quantity;                    
        Sku = variant.Sku;               开发者_C百科           
        sbParams.Append(string.Format("?sku={0}&Qty={1}",Sku.Substring(0,4),TotalQty));

        popup = string.Format("window.open('http://somesite.ocm/cal.asp{0}','Reservation Calendar','width=265,height=465')",sbParams.ToString());
    btnCalendar.OnClientClick = popup;
     }

Calendar button:

    // Shows popup via OnClientClick
    public void btnCalendar_Click(object sender, EventArgs e)
    {
        AddPopup.Show();
    }

It works fine as a two-step process, click Setup and then click Calendar buttons. If I move the setup code into the calendar routine it fails. I'm sure this is because the Calendar button's OnClientClick is not set when it is clicked.

What is a best-practices type of way to do this without restructuring the whole program. As I'm new to .NET programming I don't want to wreck the thing.


I would change your code to setup your Button on the Page_Load where it is typical to do much of the setup/prep for the page:

 protected void Page_Load(object sender, EventArgs e)
 {
     if (!IsPostBack)
     {
         StringBuilder sbParams = new StringBuilder();
         TotalQty += basketItem.Quantity;
         Sku = variant.Sku;
         sbParams.Append(string.Format("?sku={0}&Qty={1}",
             Sku.Substring(0,4),TotalQty));
         popup = string.Format("window.open('http://somesite.ocm/cal.asp{0}', 
             'Reservation Calendar','width=265,height=465')", 
             sbParams.ToString());        
         btnCalendar.OnClientClick = popup;
     }
 }

The problem you have is your are trying to add something to the client side action using a server side method. The client side method fires first and then the server side so if you add the function server side, the client side action has already passed it's execution point and does nothing.

If you need this Button to act differently or it is repeated multiple times, you should look into doing the setup during some bind action that is occuring. If not just do it in the Page_Load at the start.

From your example I am not sure why you even care about the server side event so you could change your OnClientClick to be:

popup = string.Format("window.open('http://somesite.ocm/cal.asp{0}', 
    'Reservation Calendar','width=265,height=465'); return false;", 
    sbParams.ToString());        

Notice the return false;. This will cause the window to open but also tell the page to not post back to the server.


Following code snippet might help you.

<asp:Button id="btnSubmit" runat="server" onclick="btnSubmit_Click" OnClientClick="javascript:ShowPopup();"" ></asp:Button>

JS

function ShowPopup()
{
//Code for popup window

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜