简体   繁体   English

如何通过ajax调用的c#函数更改asp标签的文本

[英]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. 我想通过ajax访问asp控件。 Explanation : I want to call c# function through the ajax. 说明:我想通过ajax调用c#函数。 This c# function is changing multiple label text with different conditions. 此c#函数将更改具有不同条件的多个标签文本。

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 C#代码

    [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. 消息成功返回,但标签文本未更改。

I want to access asp control through the ajax. 我想通过ajax访问asp控件。 Explanation : I want to call c# function through the ajax. 说明:我想通过ajax调用c#函数。 This c# function is changing multiple label text with different conditions. 此c#函数将更改具有不同条件的多个标签文本。

Once the webpage lands in the user's browser, ASP (and .Net in general) has gone out of scope . 网页进入用户浏览器后,ASP(通常是.Net)已超出范围 .Net can no longer access the controls. .Net不再可以访问控件。 In essence, the order of operations is as follows: 本质上,操作顺序如下:

  1. Request begins. 请求开始。
  2. All your .Net code executes, where you can set your controls, eg set the value for myLabel.Text 您所有的.Net代码都将执行,您可以在其中设置控件,例如,设置myLabel.Text的值
  3. .Net converts your control into a HTML string . .Net将控件转换为HTML字符串 At this time, it uses the values (eg myLabel.Text ) to generate the HTML. 此时,它使用值(例如myLabel.Text )来生成HTML。
  4. The HTML is returned to the browser, who displays it. HTML返回到浏览器,由浏览器显示。
  5. Request ends. 请求结束。

When you use ajax, you are executing an action in the browser. 使用ajax时,您正在浏览器中执行操作。 You're working with the generated HTML content, not the ASP.Net controls that were once used to generate the HTML from. 您正在使用生成的HTML内容,而不是曾经用于生成HTML的ASP.Net控件。

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. 通过Javascript / jQuery分配值。 Eg: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM