2017-07-18 75 views
1

这是我的前端:如何字符串参数传递给javascript函数

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> 

我想字符串参数传递给javascript函数,但我得到错误,strApplication1在当前上下文中不存在。

回答

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

} 
+0

谢谢@乔恩P,我的问题解决了 – Arrow

相关问题