Home > Net >  Transfer a js string to c# ASP.NET
Transfer a js string to c# ASP.NET

Time:07-14

Im making a website using ASP.NET and I have a string using JavaScript and I want to access the string using c#. If there's a way to do this let me know. Im getting the value of a textbox using javascript like this: string str = document.getElementById("TextBoxID"); and Im trying to access the str string using c# because I want to insert it into a database.

CodePudding user response:

Well, you don't note if this is webforms or mvc?

But, as a general rule, any control, or values on the form can be used in code behind. but, to do so, you need a page post-back.

So, it really depends. If you run some client side js code, set a value into to a text box, or even hidden field? Then you need to post-back to run code behind, and the c#/vb.net code can freely get/use/grab/see the values in that text box or hidden field.

However, if you don't want a post-back, and page cycle? Then you have to do a ajax call from the client side, and PASS the value(s) to the web method you created.

So, js code is free to set, modify values of controls. The REAL issue then is how, and when you plan to call use code behind.

Without a post-back, and just a web method call (ajax), then all of the controls on the web page are NOT available in code behind. So, you either suffer a post-back, or you pass the value(s) of the controls from the ajax call - either approach is fine, and each use case will be different.

So, you might have say this markup. A text box, a button to modify the text box value. Assuming jQuery installed (don't we all??).

So, when you say "pass" or "use" the value of the text box in c#? Well, as noted, it going to depend on if you going to live with a post-back, or not?

I mean, you could place a textbox, a button, and do a ajax call to code behind.

So, you drop in say this markup:

    <div>
        <asp:TextBox ID="TextBox1" runat="server" ClientIDMode="Static"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" 
            OnClientClick="myjava();return false"
            />
    </div>
    <script>

        function myjava() {
            textBoxValue = $('#TextBox1').val()
            // now run some code behind
            $.ajax({
                type: "POST",
                url: "AjaxTest1.aspx/MyWebMethod",
                data: JSON.stringify({ strTextBox1 : textBoxValue }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (myresult) {
                    // put result of method call into txtResult
                    // ajax data is ALWAYS ".d" - its a asp.net thing!!!
                },
                error: function (xhr, status, error) {
                    var errorMessage = xhr.status   ': '   xhr.statusText
                    alert('Error - '   errorMessage)
                }
            });
        }
    </script>

So, we grab the value of the text box (or we could set it). and our code behind is this:

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod()]

    public static void MyWebMethod(string strTextBox1)
    {

        Debug.Print("value of text box passed = "   strTextBox1);

    }

But, we could for example drop in a plane jane asp.net button, and wire up a click event for that button. But, then we would be using a post-back.

so, lets drop in a plane jane button below the above one, say like this:

        <asp:TextBox ID="TextBox1" runat="server" ClientIDMode="Static"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" 
            OnClientClick="myjava();return false"
            />
        <br />
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
    </div>

We double click on that 2nd button, and write this code:

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod()]

    public static void MyWebMethod(string strTextBox1)
    {

        Debug.Print("value of text box passed = "   strTextBox1);

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Debug.Print("value of text passed = "   TextBox1.Text);

    }

So, in this case, code behind can use the controls directly - due to the post back.

So, when you say you want to pass a string value from client side code? Well, the issue REALLY comes down to when, and how you want to trigger that code.

  • Related