0

I have an asp:Button that gets clicked and does backend stuff (or I want to do back end stuff).

The issue is that i have a large javascript template block that I want to pass back into my code back end.

Is there a decent/good way to do this? Is there a way to grab the javascript block from the on click method in my code behind (mypage.aspx.cs)?

3
  • What do you mean by 'javascript block'. Do you have an example? Commented Feb 28, 2014 at 9:04
  • What do you mean by passing it to back end, do you want to invoke it from code behind? Commented Feb 28, 2014 at 9:06
  • I am passing a JSON javascript statement using handlebars. So its pretty much var anArrayOfOptions and I want to pass it to the back end for processing. Commented Feb 28, 2014 at 9:43

2 Answers 2

1

You could pop the json in a hidden field on button click using jquery and then retrieve it on the back end. You should parse the hidden fields value on the backed to a json object using something like json.net so you can play with the content http://james.newtonking.com/json

It will be something like the following

<asp:HiddenField runat="server" id="hfJson" />
<asp:Button ID="btnSubmit" runat="server" Text="Click me" OnClick="btnSubmit_Click"  />
<script>
    $(function () {
        $('#<%=btnSubmit.ClientID %>').click(function () {
            $('#<%=hfJson.ClientID %>').val(anArrayOfOptions);
        });
    });
</script>

Then in your code behind

protected void btnSubmit_Click(object sender, EventArgs e)
{
    JObject anArrayOfOptions = JObject.Parse(hfJson.Value);
}

Hope that helps :)

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

Comments

0

I think you can use a textbox with visibility off. convert that object to json and keep it updated to that textbox. You can keep the updated the object manually or can use knockout.js and onclick the form will automatically post the values to the server side, where you can use it.

Comments

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.