简体   繁体   中英

How to pass String Argument to javascript function

This is my front-end:

ImageButton Details = (ImageButton)e.Row.FindControl("iBtnDetails");//take lable id
                String strApplication1 = Details.CommandArgument.ToString();

                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
                e.Row.Attributes.Add("onClick", "SelectRow()");

This is my back-end:

<script type="text/javascript">
                function SelectRow() {

                    var strApplication1 = '<%=strApplication1%>'

                    if (strApplication1 == "IT Application Request")
                    {

                         window.open('http://.aspx', '_blank');
                    }

                    else if (strApplication1 == "IT Account Request")
                    {
                         window.open('http://.aspx', '_blank');

                    }
                    else if (strApplication1 == "Change Control Management")
                    {
                         window.open('http://.aspx', '_blank');
    }
                    else if (strApplication1 == "Backup & Restore")
                    {
                        window.open('http://.aspx', '_blank');
                    }
                }

</script>

I want to pass String Argument to javascript function, but I got error that strApplication1 doesn't exist in the current context.

You need to make strApplication1 a public property on your page class. Currently, it is just an internal variable.

Something like:

public partial class YourPage : System.Web.UI.Page
{
    public string strApplication1 {get; set;}

    protected void Page_Load(object sender, EventArgs e)
    {
         //Your page logic          
    }

    //Looks like you set the variable in an onDatabound or similar.
    //So use this where you currently set the variable
    strApplication1 = Details.CommandArgument.ToString();

}

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