James
James

Reputation: 3805

ASP.Net Drop Down List not passing a value when updated using ajax

I have some jQuery that I'm using to open a pop-up window where a new consignor can be added to the database. The original window has a dropdownlist of all of the current consignors. When you add the new consignor in the pop-up window, that window closes and the original window then reloads the dropdownlist's data and selects the one just created.

All of that works perfectly. My issue is that when you fill out the rest of the form and submit it, it passes an empty string instead of the value of the selected item. Is this because it's an ASP.Net script? I don't know a lot about ASP.Net, but I've never had this issue with PHP. Can someone explain how I would go about refreshing the dropdownlist without refreshing the entire page and still get the list to pass it's value upon form submission?

My javascript code on the page that opens the pop-up and reloads the list is below:

function openConsignorAdd() {
    var url;
    url = "/admin/consignor/csAdd.aspx";
    window.open(url, "WizardWindow", "width=400,height=500,resizable=yes,scrollbars=yes");
}

function loadNewAdded(fn, cs_txt_id) {
    //    var pagePath = window.location.pathname;
    var pagePath = "/admin/getNewList.asp";
    var paramList = "data=";

    //Call the page method
    $.ajax({
        type: "POST",
        url: pagePath + "?type=" + fn + "&cs_txt_id=" + cs_txt_id,
        data: paramList,
        success: function (data) {
            //create jquery object from the response html
            var $response = $(data);
            //query the jq object for the values
            var results = $response.filter('select#results').html();

            if (fn == "consignor") {
                $("select#<%=itemConsigner.ClientID%>").html(results);
            } else if (fn == "cdr") {
                $("select#<%=itemCDR.ClientID%>").html(results);
            }
        },
        error: function () {
            alert("Failed To Refresh!\n\nYou must manually refresh the page.");
        }
    });
}

My javascript code on the pop-up page to refresh the list is:

function refreshOpener(cs_txt_id) {
    window.opener.loadNewAdded("consignor", cs_txt_id);
}

Those both work. And to get the value of my dropdownlist, I simply use:

if (itemConsigner.SelectedValue.ToString() != string.Empty)
{
    itemCsTxtId = itemConsigner.SelectedValue.ToString();
}

with my dropdownlist being:

<asp:DropDownList ID="itemConsigner" runat="server" TabIndex="1"></asp:DropDownList>

If you need more info, just let me know. Any help is appreciated.

Upvotes: 0

Views: 1170

Answers (2)

James
James

Reputation: 3805

It seems that the issue is that since I am making the change after the page loads, the server does not see my new addition as one of the original options so ignores it completely. This is good so that people cannot just edit your forms I guess. So what I did was instead of getting the value of itemConsigner.SelectedValue, I grab the value for Request.Form["itemConsigner"] with the long ID. That way it doesn't validate that my submitted option was an original option.

Upvotes: 1

DRobertE
DRobertE

Reputation: 3508

Might be a silly observation but without all the code I'm not sure if this is the case. Are you just updating the original list with the id in the select options. The value needs to be populated as well for each. That could be why you are getting an empty value on after form submission.

Upvotes: 0

Related Questions