admin管理员组

文章数量:1289582

I'm trying to populate on dropdown(i.e dropdown2) on the value basis of other dropdown(i.e dropdown1), It's populate fine,now my problem is when i'm click on asp button to get the selected value of dropdown 2 it will be return blank value, I'm not not understand what is going wrong?

HTML

<select runat="server" id="ddlFilter" ClientIDMode="Static">
    <option value="0">Both</option>
    <option value="1">Add new</option>
    <select runat="server" id="ddlDepartment" ClientIDMode="Static"></select>
    <asp:Button ID="btnSearch" runat="server" Text="Show Report" CssClass="search_btn" onclick="btnSearch_Click" />

JS

<script>
$(document).ready(function () {
    $("#ddlFilter").change(function () {
        if ($("#ddlFilter").val() > 0) {
            $("#ddlDepartment").append( < option > New < option > );
        }
    });
});
</script>

C#

protected void btnSearch_Click(object sender, EventArgs e) {
    string val= ddlDepartment.Value;
}

I'm trying to populate on dropdown(i.e dropdown2) on the value basis of other dropdown(i.e dropdown1), It's populate fine,now my problem is when i'm click on asp button to get the selected value of dropdown 2 it will be return blank value, I'm not not understand what is going wrong?

HTML

<select runat="server" id="ddlFilter" ClientIDMode="Static">
    <option value="0">Both</option>
    <option value="1">Add new</option>
    <select runat="server" id="ddlDepartment" ClientIDMode="Static"></select>
    <asp:Button ID="btnSearch" runat="server" Text="Show Report" CssClass="search_btn" onclick="btnSearch_Click" />

JS

<script>
$(document).ready(function () {
    $("#ddlFilter").change(function () {
        if ($("#ddlFilter").val() > 0) {
            $("#ddlDepartment").append( < option > New < option > );
        }
    });
});
</script>

C#

protected void btnSearch_Click(object sender, EventArgs e) {
    string val= ddlDepartment.Value;
}
Share Improve this question edited Aug 16, 2013 at 8:53 putvande 15.2k3 gold badges36 silver badges51 bronze badges asked Aug 16, 2013 at 8:50 Ajay RawatAjay Rawat 592 silver badges14 bronze badges 4
  • 1 You are missing single quotes here .append('<option>New<option>'); – palaѕн Commented Aug 16, 2013 at 8:53
  • If your problem fixed with any of the answers below or not , RESPOND something ! Thanks ! – zey Commented Aug 16, 2013 at 9:16
  • hey respond to the answers. Here there are 5 helping hands to u and you are simply not responding!!! – Subin Jacob Commented Aug 16, 2013 at 10:30
  • any answer not work for me – Ajay Rawat Commented Aug 17, 2013 at 7:43
Add a ment  | 

5 Answers 5

Reset to default 6

You can't get selected value from dropdown if you adding options in javascript. you can use:

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

See Question

change this

if ($("#ddlFilter").val() > 0) 

to

if ($(this).val() > 0) 

change this

 $("#ddlDepartment").append( < option > New < option > );

to

$("#ddlDepartment").append( '<option>New</option>');

If you want to get the value from the drop down list in the C# code behind you need to add the new item on the first drop down lists SelectedIndexChanged event and add the "New Option" to the dropdowns item collection.

The steps would be:

1) add SelectedIndexChanged for ddlFilter
2) Check if the value for ddlFilter is "1"
3) If the value is "1" add "New Option" ListItem to the dropdownlist name ddlDepartment.

Then when the button is pressed you should get the selected value as item is now in the dropdown's list collection.

Try likes this ,

ddlFilter.Items[ddlFilter.SelectedIndex].Text;

will return your string text Both,Add new

ddlFilter.Items[ddlFilter.SelectedIndex].value;

will return your value 0,1

You will find 100's of solutions for dropdown,

In c# : String Val=ddlDepartment.SelectedItem.Value;

本文标签: cget the value of dropdown on asp button clickStack Overflow