开发者

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

            }

        } 

it says i is undefined when I debug the code in f2(i).What am i doing wrong?

EDIT: I learned many things from this problem, but I still do not have an answer.I tried every idea given in the answers but i is still undefined...

EDIT: I still have no solution for undefined reasons :), but the answer I accepted wo开发者_JAVA技巧uld normally be my solution.


Have you tried debugging the server-side code to see if

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

is giving you what you want in the first place?


Well seeing as how the above has been checked, I went ahead and created my own test, and it worked just fine. Here's the test I used:

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

2, "HiddenField1", "GridRefreshPanel"


ClientScript.RegisterStartupScript takes a script definition, not an invocation.

Since you want to define the function externally, use this method instead:

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

To execute the JavaScript function f2 with parameter Session["sayfaTuru"] on button click, use the following (not tested):

  1. In Page_Load, add the JavaScript file which contains the definition for f2:

    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.RegisterClientScriptBlock(...);
        // Do other stuff
    }
    
  2. Then add the actual button click listener which will invoke f2:

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


I think you may be getting an Exception thrown in your code-behind. You're trying to concatenate strings, but i is an integer. Try this instead:

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

Though I would actually prefer to use String.Format:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜