Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

How do I redirect page to new tab using link onclick event not href attribute?

Hi Friends,

I created html link (anchor tag) dynamically using server side c# code and added on click() event to it, I am  using server side method() on page post-back to redirect page to another one .

I want to redirect page in new tab and keep existing page as it is, please provide your solution with on click() event or server side redirect code.

Note in redirect method I am passing some parameters with object used in redirected page some want solution in event itself.

Also, I am using other buttons and links on same page for those it should use same page not redirecting logic, redirecting logic is applicable for dynamic code only.

My server side link code:

CaegoryTest.Append("<a id=\"lnkCatBtn" + TestCategoryBuilder.GetCategoryById(category.CategoryId).Name + "\" name =\"lnkCatBtn" + TestCategoryBuilder.GetCategoryById(category.CategoryId).Name + "\" on click=\"SearchTestCat('" + TestCategoryBuilder.GetCategoryById(category.CategoryId).Id + "')\" > ");

Jvascript code:

        function SearchTestCat(someid) {
            __doPostBack ("lnlCatBtn" + someid, someid);
        // Postback
        function __doPostBack(eventTarget, eventArgument) {
            if (!theForm. onsubmit || (theForm. onsubmit() !== false)) {
                theForm . __EVENTTARGET.value =eventTarget;
                theForm . __EVENTARGUMENT.value =eventArgument;
                theForm . submit();

 My server side C# code used to redirect page:

// custom internal logic created for routing
  /// <summary>
        /// This will redirect the current HttpContext to the route based on the parameters given
        /// </summary>
        /// <param name="parameters"></param>
        public void Redirect(T parameters = null)
            if (HttpContext.Current == null) return;
            HttpContext.Current.Response.RedirectToRoute(_routeName, new RouteValueDictionary((parameters != null ? parameters.GetParameters() : TestRouteParameters.GetParameters())));