1
    function SetDropDownValue() {
            var opt = document.createElement("option");
            opt.text = "New Value";
            opt.value = "New Value";
            document.getElementById('<%=DropDownList.ClientID%>').options.add(opt);
            document.getElementById('<%=DropDownList.ClientID%>').value = val;
    }

The above coding works good for me, The new value append to the drop down list while clicking the button. Then i want to get the drop down value in my code behind(C#). It not working in C#.

    string res = DropDownList.SelectedValue;

In the C# coding displays only empty string ("").

How can i get the dropdown selected value?

1
  • Is this WebForms? Otherwise you'd have to post the value of the select to your server side application to handle your request. Commented Feb 6, 2014 at 12:43

1 Answer 1

2

You are changing the html but not the ViewState of the DropDownList that is why you are not getting in asp.net server side code. You can add the value in some hidden field and use that on server side.

HTML

<input type="hidden" id="hdnDDL" runat="server" />

Javascript

function SetDropDownValue() {
    var opt = document.createElement("option");
    opt.text = "New Value";
    opt.value = "New Value";
    document.getElementById('<%=DropDownList.ClientID%>').options.add(opt);
    document.getElementById('<%=DropDownList.ClientID%>').value = val;
    document.getElementById('<%=hdnDDL.ClientID%>').value = val;
}

Code behind

string optionValue = hdnDDL.Value;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.