簡體   English   中英

將參數傳遞給 JavaScript function

[英]passing a parameter to a JavaScript function

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', '');

            }

        } 

當我調試f2(i)中的代碼時,它說i未定義。我做錯了什么?

編輯:我從這個問題中學到了很多東西,但我仍然沒有答案。我嘗試了答案中給出的每一個想法,但i仍然undefined ......

編輯:由於未定義的原因,我仍然沒有解決方案:),但我接受的答案通常是我的解決方案。

ClientScript.RegisterStartupScript采用腳本定義,而不是調用

由於您要在外部定義 function,請改用此方法

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

要在單擊按鈕時使用參數Session["sayfaTuru"]執行 JavaScript function f2 ,請使用以下命令(未經測試):

  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']%>);" />

您是否嘗試過調試服務器端代碼以查看是否

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

控制台日志 Output:

2, "HiddenField1", "GridRefreshPanel"

我認為您可能會在代碼隱藏中拋出異常。 您正在嘗試連接字符串,但我是 integer。 試試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM