4

I had populated an ASP.net dropdown list with AJAX now I need to get the Id to store in into the database in a C# method, (I'm using LINQ)

This is my webmethod

[WebMethod]
public static ArrayList GetLanguageList()
{
    ArrayList lstArrLanguage = new ArrayList();
     IQueryable<Common.Town> myList = new SupplierBL().GetTowns();
     foreach(Common.Town t in myList)
    {
        string name = t.Name;
        string id = t.TownId.ToString();
        lstArrLanguage.Add(new ListItem(name, id));
    }

    return lstArrLanguage;
}

My test.aspx code

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "test.aspx/GetLanguageList",
            data: '',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                $("#ddlLanguage").empty().append($("<option></option>").val("[-]").html("Please select"));
                $.each(msg.d, function () {
                     $('#<%=ddlLanguage.ClientID%>').append($("<option></option>").val(this['Value']).html(this['Text']));
                });
            },
            error: function () {
                alert("An error has occurred during processing your request.");
            }
        });
    });
</script>
Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52
Eliza
  • 135
  • 1
  • 4
  • 15
  • Eliza, the `GetLanguageList()` method where are you calling that from are you doing that based on a button click or form a SelectedItem in the DDL..? can you show the relevant code..? also what is Common.Town? – MethodMan Jan 26 '13 at 15:07
  • With AJax from the webpage – Eliza Jan 26 '13 at 15:24
  • when you get the DropDown list values loaded, does it have the name, Id in the list..? if so now you need to get the ID based on the selected item.. from the selected Item you could do a Split(',') and get the array[1] value correct me if I am understanding you incorrectly Eliza – MethodMan Jan 26 '13 at 15:30
  • I know which data I need to get, but I don't know to to getting it into a c# method when I tried string text = ddlDropDown.SelectedValue.ToString() in gives me an error Invalid postback or callback argument – Eliza Jan 26 '13 at 15:37
  • are you doing any `if(!IsPostBack)` check..? you could actually do something like this `var value = Request.Form[ddlLanguage.UniqueID];` it will get the value that was selected stored on the Server not the Client. – MethodMan Jan 26 '13 at 15:43
  • Nothing still gives me that error – Eliza Jan 26 '13 at 16:01
  • what method in your code is this giving you the error..? can you get at the document level..? would really need to see where you are getting this error what code or event Eliza.. thanks – MethodMan Jan 26 '13 at 16:05

2 Answers2

7

You can't get selected value from DropDownList if you adding options in javaScript. You can try the following

string selectedValue = Request.Form[ddlLanguage.UniqueID];

This question may be useful also.

Community
  • 1
  • 1
Muhammad Hani
  • 8,476
  • 5
  • 29
  • 44
0

If you populate the value of dropdown via ajax than it can't be available on Server Side because the page doesn't postback during ajax request.

In order to get the value of dropdown in C# use below snippets :

String _value = Convert.ToString(Request[ddlLanguage.ClientID]);

Hope this will help !!

Kundan Singh Chouhan
  • 13,952
  • 4
  • 27
  • 32