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:
and
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?
<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>
and
<System.Web.Services.WebMethod()> _
Public Shared Function GetPSNData(ByVal name As String) As String
Return "Suck it"
End Function
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?
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.
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
Then create a JSON object to have some configuration values and adjust the OnSuccess function like
Finally you can call the function passing the config parameter like
Just my 2ct
Rainer
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
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>
Finally you can call the function passing the config parameter like
SetPSNData('UserName');
SetPSNData('OtherField');
Just my 2ct
Rainer
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Figured it out