将参数传递给JavaScript函数

时间:2011-05-31 14:49:38

标签: javascript

int i = Convert.ToInt32(Session["sayfaTuru"]);            
ClientScript.RegisterStartupScript(this.GetType(), "RefreshOpener", "f2(" + i + ")", true);

function f2(i) {
            if (i == 1) {//CariKartHareketleri
                opener.document.getElementById("HiddenField1").value = "hello world";
                window.opener.location.href = window.opener.location.href; //çağıran sayfayı yeniliyor

            }
            else if (i == 2) {//islemler
                opener.document.getElementById("HiddenField1").value = "hello world";
                window.opener.__doPostBack('GridRefreshPanel.ClientID', '');

            }
            else if (i == 3) {//hizmet listesi
                opener.document.getElementById("HiddenField1").value = "hello world";
                window.opener.__doPostBack('GridRefreshPanel.ClientID', '');

            }

        } 

我在调试i中的代码时说f2(i)未定义。我做错了什么?

编辑:我从这个问题中学到了很多东西,但我仍然没有答案。我尝试了答案中给出的每个想法,但i仍然是undefined ...

编辑:我仍然没有解决未定义的原因:),但我接受的答案通常是我的解决方案。

3 个答案:

答案 0 :(得分:2)

ClientScript.RegisterStartupScript采用脚本定义,而非调用

由于您想在外部定义该功能,请改为使用this method

ClientScript.RegisterClientScriptBlock(this.GetType(), "RefreshOpener", "MyJavaScriptFile.js", true);

要在按钮单击时执行带有参数f2的JavaScript函数Session["sayfaTuru"],请使用以下(未测试):

  1. Page_Load中,添加包含f2定义的JavaScript文件:

    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.RegisterClientScriptBlock(...);
        // Do other stuff
    }
    
  2. 然后添加实际的按钮点击监听器,它将调用f2

    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="f2(<%=Session['sayfaTuru']%>);" />
    

答案 1 :(得分:2)

您是否尝试过调试服务器端代码以查看是否

int i = Convert.ToInt32(Session["sayfaTuru"]);

首先给你你想要的东西?


看看上面是如何检查的,我继续创建了我自己的测试,它工作得很好。这是我使用的测试:

<强> JS:

<script type="text/javascript">    
    function f2(i, hiddenFieldId, updatePanelId) {

        console.log(i + ', "' + hiddenFieldId + '", "' + updatePanelId + '"');
        var hiddenField = window.opener.document.getElementById(hiddenFieldId);

        switch (i) {

            case 1: //CariKartHareketleri
                hiddenField.value = "hello world 1";
                window.opener.location.href = window.opener.location.href; //çağıran sayfayı yeniliyor
                break;

            case 2: //islemler
                hiddenField.value = "hello world 2";
                window.opener.__doPostBack('' + updatePanelId + '', '');
                break;

            case 3: //hizmet listesi
                hiddenField.value = "hello world 3";
                window.opener.__doPostBack('' + updatePanelId + '', '');
                break;

            default:
                alert("error");
                break;      

        }
    }
</script>

<强> C#:

Session["sayfaTuru"] = 2; // initialize for testing purposes

int i = Convert.ToInt32(Session["sayfaTuru"]);

string script = string.Format("f2({0}, '{1}', '{2}');", 
                              i.ToString(), 
                              this.HiddenField1.ClientID, 
                              this.GridRefreshPanel.UniqueID);

ClientScript.RegisterStartupScript(this.GetType(), "RefreshOpener", script, true);

console.log输出:

2, "HiddenField1", "GridRefreshPanel"

答案 2 :(得分:0)

我认为您可能会在代码隐藏中抛出异常。你试图连接字符串,但我是一个整数。试试这个:

int i = Convert.ToInt32(Session["sayfaTuru"]);            
ClientScript.RegisterStartupScript(this.GetType(), "RefreshOpener", "f2(" + i.ToString() + ")", true);

虽然我实际上更喜欢使用String.Format:

int i = Convert.ToInt32(Session["sayfaTuru"]);            
ClientScript.RegisterStartupScript(this.GetType(), "RefreshOpener", String.Format("f2({0})", i), true);