I need to invoke a server side function when an item is picked in an ASP drop-down box. Can someone please tell me how to do that?
4 Answers
Within ASP.NET use the drop down selected index change event. Alternatively for a client side event you could use JQuery and then use the following JavaScript function to contact the Server:
function CallServer() {
$.ajax({
url: 'webserviceURL',
type: "POST",
datatype: "json",
success: function (result) {
if (result.Success) {
} else {
}
}
});
}
1 Comment
pragan
thanx buddy, I actually forgot to mention the JS part, I needed to invoke server side function using JS without postbacking the form...
set
ddl.autopostback = true ;
and fire selectedindexchange event
4 Comments
tvanfosson
...but this doesn't use "a [sic] javascript"
Claudio Redi
@tvanfosson: Although that actually uses javascript in the backgroud, I give you that is not probably what he tried to ask.
tvanfosson
@ClaudioRedi - yes, I'm assuming that he intended "using some javascript that I write"
Mohit
if you don't want post back don't you think it will be more feasible to use update panel to achieve your target use as many as needed so you can speed up your web application and save data transportation amount and also use update panel for this ddl and for that portion of web page which data you wan't to update.
You can do it like this:
Aspx
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList>
CS
protected void ddl_SelectedIndexChanged(Object sender, EventArgs e)
{
//call your function
}
2 Comments
tvanfosson
Strictly speaking this doesn't meet the requirement of using (non-framework) javascript.
Mohit
is there any need to be bound with some language Visual Studio and other IDE handle all that stuff it self they'll invoke javascript with ajax if you put this ddl into Updatepanel you're not required to do so
AutoPostBack="True"to the dropdown markup and addOnChange="YourMethodHere".