0

I have been trying for a while to get the variable passed from the JQuery function to the code behind so that I can enter it into a database via an SQL statement. However, I never seem to get the variable passed. Or if it is passing, I can't seem to get it to display to make sure it has been passed. Can anyone see what's wrong with my script?

<script type="text/javascript">
  $(function () {
      var comment = $("#comment"),
      allFields = $([]).add(comment);
      $('#<%= hidden.ClientID %>').val(comment);


      $("#dialog-form").dialog({
          autoOpen: false,
          height: 300,
          width: 350,
          modal: true,
          buttons: {
              "Add Comment": function () {
                  var bValid = true;
                  allFields.removeClass("ui-state-error");
                  if (bValid) {
                      $("#comments tbody").append("<tr>" +
                        "<td>" + comment.val() + "</td>" +
                        "<td>" + "<%=currentUser%>" + "</td>" +
                        "</tr>");

                      $(this).dialog("close");
                      //                          
                  }
              },
              Cancel: function () {
                  $(this).dialog("close");
              }
          },
          close: function () {
              allFields.val("").removeClass("ui-state-error");
          }
      });

      $("#NewComment")
    .button()
    .click(function () {
        $("#dialog-form").dialog("open");

    });
  });  
    </script>  

And here is my hidden input field:

<input id="hidden" type="hidden" runat="server" />

2 Answers 2

3

I would suggest to use an actual Asp.Net HiddenField control like this:

Markup:

<div>
    <asp:HiddenField ID="hidden" runat="server" />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>

Code Behind:

protected void Button1_Click(object sender, EventArgs e)
{
    string hidden = this.hidden.Value;
    Response.Write(hidden);
}

Javascript:

<script>
    $(document).ready(function () {
        $("#<%=hidden.ClientID %>").val("Hello, World!");
    });
</script>

Output: Hello, World!

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

9 Comments

passing "hello world!" works fine. it's when I try passing the variable "comment" that it doesn't work. I have a JQuery Dialog popup that has an input field in it and an Add Comment Button in it. The user types a comment and hits the button. I'm trying to retrieve the comment variable from JQuery.
You don't exactly mention what variable you are talking about in your question. comment should be comment.val(). What is var comment = $("#comment")? Also you should have a ";" instead of a "," in there.
var comment = $("#comment") is getting the value of the input field inside of the dialog pop up. Sorry, the id of the input field inside the popup is comment.
I think I see what you want to do now. But it seems to me "Comment" is a table. If comment is a table, do you want the html of the table inside the hidden field? you do it like this: $("#comment").html()
when i response.write(hidden), I am only getting [object, Object]
|
0

What are you trying to do with this line: $('#<%= hidden.ClientID %>').val(comment); ? aren't you missing something like comment**.val()**

Still if I were you I would use a simple handler and make a ajax request.

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.