Link to home
Start Free TrialLog in
Avatar of Member_2_1242703
Member_2_1242703

asked on

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?
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

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.
Avatar of Rainer Jeschor
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
Avatar of Member_2_1242703
Member_2_1242703

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Member_2_1242703
Member_2_1242703

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Figured it out