Click here to Skip to main content
15,887,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When select an item from dropdownlist it will open a file in new tab. There is no error.
but when I click on link button,click event does not fire of link button.
Again dropdownlist SelectedIndexChanged event is fired when I click on link button.

When I select from dropdownlist , it open a file new tab but dows call javascript function
"ResetTarget()"

What I have tried:

ASP.NET
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        function ResetTarget() {
            alert('Yes');
            document.forms[0].target = '';
        }
        function SetTarget() {
            document.forms[0].target = "_blank";
            window.setTimeout("document.forms[0].target='';", 500);
        }

    </script>
    <asp:HiddenField ID="hidCSRF" runat="server" Value="" />
    <table align="center" class="SubFormWOBG" style="line-height: 25px; width: 100%">
        <tr>
            <td style="width: 40%">Application Code:
            </td>
            <td style="width: 60%">
                <asp:LinkButton ID="lnkApplicationCode" runat="server" CausesValidation="false" 
                     OnClick="lnkApplicationCode_click"></asp:LinkButton>
                
            </td>
        </tr>

        <tr runat="server" id="MandatAtt">
            <td>Mandatory Attchment:</td>
            <td>
                <asp:DropDownList runat="server" ID="ddlAttachment" AutoPostBack="true"
                    OnSelectedIndexChanged="ddlAttachment_SelectedIndexChanged">
                </asp:DropDownList>

            </td>
        </tr>
    </table>

</asp:Content>


in c# file.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {

                lnkApplicationCode.Text = "135";
                MandatAtt.Visible = false;               
                ddlAttachment.Attributes["onchange"] = "SetTarget();";
                MandatAtt.Visible = true;
     }
            catch (Exception)
            {
                Response.Redirect("~/ErrorPage.aspx", false);
            }
            finally
            {

            }
        }
    }
    protected void ddlAttachment_SelectedIndexChanged(object sender, EventArgs e)
    {
            try
            {
                string[] arr = ddlAttachment.SelectedValue.ToString().Split(',');
                int int_attachmentCode = Convert.ToInt32(arr[0]);
                int int_attachmentSerialCode = Convert.ToInt32(arr[1]);
                NOCAPInternalUtility.INDAppViewFiles(Convert.ToInt64(lnkApplicationCode.Text.Trim()), int_attachmentCode, int_attachmentSerialCode);
            }
            catch (System.Threading.ThreadAbortException ex)
            {

            }
            catch (Exception)
            {
                Response.Redirect("~/ErrorPage.aspx", false);
            }
            finally
            {
                
                hidCSRF.Value = Convert.ToString(System.Guid.NewGuid());
                ViewState["CSRF"] = hidCSRF.Value;
                ScriptManager.RegisterStartupScript(this, GetType(), "function", "ResetTarget();", false);
            }       

    }
    protected void lnkApplicationCode_click(object sender, EventArgs e)
    {
            hidCSRF.Value = Convert.ToString(System.Guid.NewGuid());
            ViewState["CSRF"] = hidCSRF.Value;
            try
            {
                long ApplicationCode = Convert.ToInt64(lnkApplicationCode.Text);
                Session["ApplicationCode"] = ApplicationCode;
               
            }
            catch (Exception)
            {
                Response.Redirect("~/ErrorPage.aspx", false);
            }
        
    }
Posted
Updated 31-Aug-20 1:38am

1 solution

You can call the ResetTarget() function in your code by changing the below line

ScriptManager.RegisterStartupScript(this.GetType(), "displayScript","<script language='javascript'>return ResetTarget();</script>");


if you dont't want to call further any other function then pass return false after you function

function ResetTarget() {
            alert('Yes');
            document.forms[0].target = '';
            return false;
        }
 
Share this answer
 

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