1

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.

2 Answers 2

2

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

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

1 Comment

thanks for the explanation. But it will be great if you could provide an example.
0

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";

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.