简体   繁体   中英

Open page in new window when a link button is clicked

I was trying to open a new window when a link button is clicked.

<asp:LinkButton ID="lnkpackageinfo" CssClass="linkclass" 
    runat="Server" 
    OnClientClick="lnkpackageinfo_Click()">Compare Packages</asp:LinkButton>

I want the target page to be given in the code behind because in the target page i want to use querystring to hide few buttons and links. It is clear

protected void lnkpackageinfo_Click(object sender, EventArgs e)
{

  long MerchantID = CommonHelper.GetLoggedInMerchant();
  string querystringpackageinfo = ApplicationData.URL_MERCHANT_COMPANY_PACKAGE + "?MerchantCompanyPayment";
  Response.Redirect(querystringpackageinfo, false);
}

This doesnot work for me. Where am i doing wrong? Any someone help me out! thank you in advance!

You are trying to call a server side function ( lnkpackageinfo_Click ) using client side markup ( OnClientClick ).

OnClientClick will try to call the JavaScript function you have named in the value of the attribute, which will not be there as the function is a server side (code behind) function.

You need to write a JavaScript function on the page in order for the client to open a new window.

Can you do something like this?

<asp:LinkButton ID="lnkpackageinfo" CssClass="linkclass" runat="Server"> Compare Packages</asp:LinkButton> 


protected void Page_Load(object sender, EventArgs e)
{
    lnkpackageinfo.Attributes.Add("onclick", "javascript:window.open('" + GetURL()+  "'); return false;");

}


public string GetURL()
{
   long MerchantID = CommonHelper.GetLoggedInMerchant(); 
   string querystringpackageinfo = ApplicationData.URL_MERCHANT_COMPANY_PACKAGE + "?   MerchantCompanyPayment"; 

   return querystringpackageinfo;
}

Well you don't need the () for one thing. Also, Just use OnClick=lnkpackageinfo_Click attribute. Then in that function set a hidden field value to call some javascript to open a new window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM