Click here to Skip to main content
14,981,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have bind one dropdown using jquery and after changing it's value page is reloaded.
and that time i want to set the dropdown selected value with default value.

What I have tried:

C#
string language = "en";
                string lan = "English";
                if (StateHandler.GetHandler(StateHandler.StateType.Session).IsKeyExist(SessionKeys.Language))
                {
                    int LanguageId = Convert.ToInt32(StateHandler.GetHandler(StateHandler.StateType.Session).Get(SessionKeys.Language));
                    switch (LanguageId)
                    {
                        case 1:
                            language = "en";
                            lan = "English";
                            break;
                        case 2:
                            language = "hi";
                            lan = "Hindi";
                            break;
                        default:
                            break;
                    }
                    ddlLanguages.ClearSelection();

                    ddlLanguages.SelectedValue = LanguageId.ToString();
Posted
Updated 10-May-16 1:36am
v2
Comments
Nigam,Ashish 27-Apr-16 8:55am
   
Where have you written this code(In which Event).
aparnaChandras 27-Apr-16 8:57am
   
on page load
aparnaChandras 27-Apr-16 9:00am
   
$(document).ready(function () {
prc_bind_Data();

});
prc_bind_Data = function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/BindDatatoDropdown",
data: "{}",
dataType: "json",
success: function (msg) {
if (msg != null) {
if (msg.d != null) {
// $("#ddlLanguages").append($("<option></option>").val(0).html("Select Language"));
var Dropdown = $('#<%=ddlLanguages.ClientID %>');
// Dropdown.append(new Option("SELECT Language", 0));
var obj = JSON.parse(msg.d);
$.each(obj, function (i, d) {

Dropdown.append(new Option(d.LanguageName, d.PkLanguageId));

// $("#ddlLanguages").append($("<option></option>").val(d.PkLanguageId).html(d.LanguageName));

});

}
}

},
error: function (result) {
alert("Error");
}
});
}
// $('#ddlLanguages').selected = true;

$(function () {

$("#ddlLanguages").change(function () {
alert($("#ddlLanguages").val());

ddlLanguageChange($("#ddlLanguages").val());

});
ddlLanguageChange = function (pLanguage) {
alert("Here");
postData = { pLanguage: pLanguage };
$.ajax({
async: false,
type: "POST",
url: "Default.aspx/ddlLanguageChange",
data: JSON.stringify({ "dbParameters": postData }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg != null) {
if (msg.d != null) {


// $("#ddlLanguages").val() =

window.location.reload(true);

// $("#ddlLanguages option:contains(" + msg.d + ")").attr('selected', 'selected');
// var obj = JSON.parse(msg.d);
//
// $.each(obj, function (i, d) {
//
// });

// if(msg.d == "hi")
// {
// $("#ddlLanguages").val() = "Hindi";
// }
// else {
// $("#ddlLanguages").val() = "English";
// }

}
}
},
error: function (result) {
alert("Error");
}
});
}
});


this is jquery code written on .aspx

1 solution

Page reload means your server side stuff can execute again, so make a javascriptvariable and assign it and empty string. In that empty string use code brackets to assign the value of your protected page property.

Now your code behind can easily set it, and your jquery can easily access it and parse.

a bit lotech but does the trick.

Another way would be to use ajax instead of posting back but that's not the question.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900