jay sedani
jay sedani

Reputation: 57

How can I change the text of asp label through c# function which is called by ajax

I want to access asp control through the ajax. Explanation : I want to call c# function through the ajax. This c# function is changing multiple label text with different conditions.

Please check following code.

    <input id="btnSilverGetPrice2" class="btn btn-next btn-fill btn-success btn-wd" type="button" value="Get Price" />

    <script type="text/javascript">
        function CheckCode(val) {
            $.ajax({
                type: "GET",
                url: "Premium-Membership.aspx/FCheckCode",
                data: { 'name': val },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response.d);
                }
            });
        }

        $("#btnSilverGetPrice2").click(function () {
            CheckCode();
        })    

        function OnSuccess(response) {
            alert(response.d);
        }
    </script>

C# Code

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
    public static string FCheckCode()
    {
        Main_Website_SignIn obj = new Main_Website_SignIn();
        string ans=obj.trial();
        return ans;
    }

    public string trial()
    {
        try
        {
            if (Session["LUSER"].ToString() == "Jobseeker")
            {
                if (DropDownListSilver.SelectedValue == "6")
                {
                    lblShowRegularPrice.Text = "500/6 Months";
                    lblShowPopularPrice.Text = "1000/6 Months";
                    lblShowPlatinumPrice.Text = "1500/6 Months";

                    lblSilverPrice.Text = "500";
                    lblGoldPrice.Text = "1000";
                    lblPlatinumPrice.Text = "1500";
                }
             }
         }
         catch (Exception){}
         return "working";      
     }

Message returned successfully, but label text not changed.

Upvotes: 0

Views: 1673

Answers (1)

Flater
Flater

Reputation: 13773

I want to access asp control through the ajax. Explanation : I want to call c# function through the ajax. This c# function is changing multiple label text with different conditions.

Once the webpage lands in the user's browser, ASP (and .Net in general) has gone out of scope. .Net can no longer access the controls. In essence, the order of operations is as follows:

  1. Request begins.
  2. All your .Net code executes, where you can set your controls, e.g. set the value for myLabel.Text
  3. .Net converts your control into a HTML string. At this time, it uses the values (e.g. myLabel.Text) to generate the HTML.
  4. The HTML is returned to the browser, who displays it.
  5. Request ends.

When you use ajax, you are executing an action in the browser. You're working with the generated HTML content, not the ASP.Net controls that were once used to generate the HTML from.

The short answer to your question is that you can't do it like that. For in-browser operations, you need an in-browser solution. Assign the values via Javascript/jQuery. E.g.:

function OnSuccess(response) {
    $("#myLabel").text(response.foo);
    $("#myOtherLabel").text(response.bar);
    $("#myOtherOtherLabel").text(response.baz);
}

Upvotes: 2

Related Questions