1
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 would normally be my solution.

4
  • Can you link the code for ClientScript.RegisterStartupScript? Commented May 31, 2011 at 14:51
  • @Exelian msdn.microsoft.com/en-us/library/aa479390.aspx#javawasp2_topic5 Commented May 31, 2011 at 14:52
  • Do a view source on the emitted script blockin the browser. What do the function calls look like? Commented May 31, 2011 at 14:53
  • I call the function like on the first lines of my question Mr.Wiseman, and having no problem with the browser as much as i understand. @James Wiseman Commented May 31, 2011 at 15:02

3 Answers 3

2

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"
Sign up to request clarification or add additional context in comments.

13 Comments

tank you Mr.Scott I will try it.Thanks for all the help.
this is realy an educational code for me Mr.Scott, however I am still getting i is undefined.I guess something else is not right.
I don't know what to say. Maybe try posting your entire HTML markup and entire C# code-behind so we can see what could be causing this.
they are really long codes, I don't wanna mess up stackoverflow :D, thanks for all your help. I used window.opener.name.indexof() to get it working not that I am happy about it but it works.I will declare your answer as the answer of the question if I can't get any other answer in three days.Thank you so much Mr.Scott.
Ok ... Just for grins though, what if you didn't create an i variable at all and simply passed in the value directly? In my example above, remove the int i = ...; line and then change i.ToString(), to (Session["sayfaTuru"]).ToString(),. Let's see what that does. I'm wondering if we are having scope issues with the i variable.
|
2

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

7 Comments

Not true. It doesn't take a function at all - takes a string that is anything script-related.
I am sorry I am a noob when it comes to JavaScript, how am I suposed to pass my parameter to a function in an external js file?
@Burn just wanted to make sure - are the calls to RegisterClientScriptBlock (etc.) inside of Page_Load?
No they are not, they are in a button_clicked method.I read this "This class would be best used when you have a JavaScript function that you want to initiate when the page is loaded" when you sent the link but check the answer of this question OnClick must work and finish before OnClientClick.
When do you want the function f2 to be executed - on page load, or on button click?
|
0

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

8 Comments

I already tried the first one Kon, I will try the one with String.Format.
Have you tried to debug and step through the code-behind to see what the value of i is?
Second one did not work, too.Well in the aspx.cs file in this line ClientScript.RegisterStartupScript(this.GetType(), "RefreshOpener", "f2(" + i.ToString() + ")", true); i is 1 or 2 or 3 as I wanted.However, in the aspx file where I wrote f2(i) it say undefined.
View source of your web page that gets rendered and search for the invocation of f2(). See what is being passed into it.
thank you I will try it and inform you Kon.thanks for your help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.