2

How would I call showItems() function in aspx page from code behind.

<script>
function getItems(){
  var items = [];
  return items; //items=['a','b','c']
}

<form id="form1" runat="server">
  <asp:Hiddenfield id="HiddenField1" runat="server"></asp:hiddenfield>
</form>

code behind:

ScriptManager.RegisterStartupScript(this, GetType(), "items", "<script type='text/javascript'>getItems()</script>", false);
1
  • javascript can't call code behind functions directly. Commented Aug 4, 2015 at 17:44

2 Answers 2

4

A few things here...

First, you don't "call a client-side function from server-side code." What you can do is include some client-side code which itself will call the function, client-side. Which appears to be what you're doing, but I just want to make sure you understand the difference.

Second, your function is called showItems, but you're calling a function called getItems:

<script type='text/javascript'>getItems()</script>

Call showItems() instead? Like this?:

ScriptManager.RegisterStartupScript(this, GetType(), "items", "<script type='text/javascript'>showItems()</script>", false);

Third, the showItems function returns something. But you're not actually doing anything with that result. You're simply invoking the function and ignoring the result. So it's not really clear what you're trying to accomplish.

Sign up to request clarification or add additional context in comments.

6 Comments

I am trying to retrieve the items, how would I store that in a variable?
@sheelachen: In JavaScript: var items = showItems(); Then in any JavaScript code for which items is in scope you can use that variable. It works very similarly to C# or, well, just about any other language. When a function returns a value, you store that value in a variable when calling the function.
:so how would I retrieve those items in code behind?
@sheelachen: Well, you don't. By the time the JavaScript code executes, the page has already rendered client-side. You can send those values to the server with an AJAX call, and the server would need something to handle that call. (Web Methods might be the current WebForms way of doing that?) It sounds like you're misunderstanding some fundamental concepts of web development here, namely that server-side and client-side code don't interact.
thnks David for clarification. can hidden fields be used to pass it to server side?
|
1

Like this:

Page.ClientScript.RegisterStartupScript(GetType(), "key", "showItems();", true);

Edit: To get the javascript return value at C# code behind Assign the values to the hidden control using the javascript code. Then you can access the Hidden control value in C# code. Take a look at this article. for example:

<script type="text/javascript">
    function showItems() {
        var items = new Array(3);
        items[0] = "name1";
        items[1] = "name2";
        items[2] = "name3";
        items[3] = "name4";

        document.getElementById('<%=HiddenField1.ClientID%>').value = items.join(',');
    }
</script>

And in the code behind:

string[] itemArr = HiddenField1.Value.Split(",".ToCharArray());

12 Comments

user2946329:so basically I need to store the returned array(items) in hidden fields and access it in code behind, correct?
user2946329 if items is already an array, as I have edited, should I used JSON.stringify(items)?
I have updated my answer. I think it is now what you were looking for..please try it and let me know if it worked for you.
user2946329 I am getting some errors, I have edited my html, could you please take a look?
it says the name 'hiddenfield1' does not exist in current context.
|

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.