如何将String Argument传递给javascript函数

时间:2017-07-18 01:02:40

标签: javascript c# asp.net

这是我的前端:

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()");

这是我的后端:

<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>

我想将String Argument传递给javascript函数,但是我得到了strApplication1在当前上下文中不存在的错误。

1 个答案:

答案 0 :(得分:2)

您需要在页面类上设置strApplication1公共属性。目前,它只是一个内部变量。

类似的东西:

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();

}