简体   繁体   中英

How to set value to textbox from static method in asp.net?

I have an application where I am getting some value over ajax post method and sending to [WebMethod] static method code behind. But I cannot assign the value to the textbox of the page. Here is my code:

    [WebMethod]
    public static string copyData(string name, string msg, string sing_cal1, string timepick, string timepickst, string sing_cal4, string step)
    {
        if (step == "3")
        {
            if (HttpContext.Current != null)
            {
                Page page = (Page)HttpContext.Current.Handler;
                TextBox txtCampaignNameEditC = (TextBox)page.FindControl("txtCampaignNameEdit");
                TextBox txtMsgEditC = (TextBox)page.FindControl("txtMsgEdit");
                TextBox txtSentFromC = (TextBox)page.FindControl("txtSentFrom");
                Label lblScheduledTimeC = (Label)page.FindControl("lblScheduledTime");


                txtCampaignNameEditC.Text = name; // Here I am getting error as "Object reference not set to an instance of an object."
            }

        }

        return "";
    }

<script type="text/javascript">
    $(document).ready(function () {
        $('#wizard').smartWizard({ onLeaveStep: leaveAStepCallback, onFinish: onFinishCallback });
        function leaveAStepCallback(obj) {
            var step_num= obj.attr('rel');
            var name = $("#<%= txtCampaignName.ClientID  %>").val();
            var msg = $("#<%= txtMessage.ClientID  %>").val();
            var cal1 = $("#<%= single_cal1.ClientID  %>").val();
            var timepicks = $("#<%= txtTimePick.ClientID  %>").val();
            var pickst = $("#<%= txtTimePickST.ClientID  %>").val();
            var cal4 = $("#<%= single_cal4.ClientID  %>").val();
            $.ajax({
                type: "POST",
                url: "CreateCampaign.aspx/copyData",
                data: '{name: "' + name + '", msg: "' + msg + '", sing_cal1: "' + cal1 + '", timepick: "' + timepicks + '", timepickst: "' + pickst + '", sing_cal4: "' + cal4 + '", step: "'+ step_num +'"}',


                contentType: "application/json; charset=utf-8",
                dataType: "json"
            });
            return true;
  }

This code behind method does not let me to assign the received parameter to the textbox. Please help me on this. Thanks.

The only time the code behind has access to modify controls is during the page life cycle. Once the page has been rendered the page is no longer connected to the server.

Moving code that references controls from the WebMethod to another function will not work because there are no controls. You must return data from the web method and place the data in the DOM using JavaScript.

To be more clear, I will say that you can't assign values to textbox using [WebMethod], because [WebMethod] don't run the ASP.Net page lifecycle. You need to use JavaScript to assign values to a textbox.

For tutorials on how to get and set textbox values with JavaScript you can check: this or this

Set the ClientIDMode property of the textbox to Static . That will ensure that whatever ID you set for the textbox will also be the id on the client. That way the id will be predictable and you can refer to it from JavaScript.

Then in your client code after the ajax is executed you can update the value in JavaScript.

document.getElementById("yourControlId").value = "whatever";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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