Populating 2 controls with different strings using a jQuery call to VB.NET code behind

I'm currently populating a text box from my code behind using the following code:

<script type = "text/javascript">
    function SetPSNData() {
        $.ajax({
            type: "POST",
            url: "create.aspx/GetPSNData",
            data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        $('#<%=tbPN.ClientID%>').val(response.d);
    }
</script>

Open in new window



and

    <System.Web.Services.WebMethod()> _
    Public Shared Function GetPSNData(ByVal name As String) As String
        Return "Suck it"
    End Function

Open in new window



What I would like to do is repeat this process for another control on the page but using a different string. How would I do this?
Mike MillerSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
How about keeping variable in jquery to differentiate between two call, also pass that value to vb.net and update vb.net code to behave different based on value (switch or if else). And when you get response back in jquery that jquery variable can be used to identify which control have to be updated.
0
Rainer JeschorCommented:
Hi,
I would do it this way:
1, Extend the web method to accept a second parameter
The second parameter can hold something where you can differentiate the call like
    <System.Web.Services.WebMethod()> _
    Public Shared Function GetPSNData(ByVal name As String, ByVal what As String) As String
        Select Case what
	    Case "Code1"
              Return "Suck it"
            Case "Code2"
              Return "EE is great"
         End Select
    End Function

Open in new window


Then create a JSON object to have some configuration values and adjust the OnSuccess function like

<script type = "text/javascript">
    var fieldConfig  = {
		"UserName": {
			"Code": "Code1",
			"TargetClientId": "#<%=tbPN.ClientID%>",
			"SourceClientId": "#<%=txtUserName.ClientID%>"
		},
		"OtherField": {
			"Code": "Code2",
			"TargetClientId": "#<%=tbOther.ClientID%>",
			"SourceClientId": "#<%=txtOtherSource.ClientID%>"
		}
	};
	
    function SetPSNData(field) {
        $.ajax({
            type: "POST",
            url: "create.aspx/GetPSNData",
            data: '{
				name: "' + $(fieldConfig[field].SourceClientId)[0].value + '",
				what: "' + fieldConfig[field].Code + '"	}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess(field),
            failure: function (response) {
                alert(response.d);
            }
        });
    }
    var OnSuccess = function(field) {
		return function(response) {
			$(fieldConfig[field].TargetClientId).val(response.d);
    }
</script>

Open in new window


Finally you can call the function passing the config parameter like
SetPSNData('UserName');
SetPSNData('OtherField');

Open in new window


Just my 2ct
Rainer
0
Mike MillerSoftware EngineerAuthor Commented:
I think I didn't ask the questinon right. What I'm trying to do is populate 2 text boxes based off what the user enters into a 3rd. In the code provided, 1 text box is populated off that user entered data. I just want to add another.
0
Mike MillerSoftware EngineerAuthor Commented:
Got it...

        $('#<%=tbPN.ClientID%>').val(response.d[0]);
        $('#<%=tbJN.ClientID%>').val(response.d[1]);
        $('#<%=tbHN.ClientID%>').val(response.d[2]);
        $('#<%=tbOSON.ClientID%>').val(response.d[3]);

  <System.Web.Services.WebMethod()> _
    Public Shared Function GetPSNData(ByVal name As String) As ArrayList
        Dim arrayList As New ArrayList
        arrayList.Add("i")
        arrayList.Add("love")
arrayList.Add("experts")
arrayList.Add("exchange")
        Return arrayList
    End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike MillerSoftware EngineerAuthor Commented:
Figured it out
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.