Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp.net中获取Html select的选定索引?_C#_Asp.net - Fatal编程技术网

C# 如何在asp.net中获取Html select的选定索引?

C# 如何在asp.net中获取Html select的选定索引?,c#,asp.net,C#,Asp.net,我的代码如下: <select id="test"> <option value="a">aaa</option> <option value="b">bbb</option> </select> <asp:Button ID="btnTest" runat="server" Text="Test it!" onclick="btnTest_Click" />

我的代码如下:

 <select id="test">
        <option value="a">aaa</option>
        <option value="b">bbb</option>
    </select>
    <asp:Button ID="btnTest" runat="server" Text="Test it!" onclick="btnTest_Click" />

aaa
bbb
我需要在回发时获取选定索引而不是选定值。如何使用asp.net实现这一点? 这是我填写select html的代码:

<script language="javascript" type="text/javascript">
    $(document).ready(function() {
    $("#<%=btnTest.ClientID %>").click(function(){
    $.ajax(
    { url: "StateCity.asmx/ReferItems?id=" + getParameterByName('id'),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        type: "POST",
        success: function(data) {
        $("#test").empty();
        $.each(data.d, function() {
        $("#test").append($("<option></option>").val(this['Value']).html(this['Text']));
        });

        },
        error: function() { alert("Error"); }
    }) 
    })
</script>

$(文档).ready(函数(){
$(“#”)单击(函数(){
$.ajax(
{url:“StateCity.asmx/refereitems?id=“+getParameterByName('id'),
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
类型:“POST”,
成功:功能(数据){
$(“#测试”).empty();
$.each(data.d,function(){
$(“#test”).append($(“”).val(this['Value']).html(this['Text']);
});
},
错误:函数(){alert(“error”);}
}) 
})
  • runat=“server”
    属性添加到
  • 在代码隐藏(按钮处理程序代码)中,执行以下操作:
  • int-selectedIndex=(测试为HtmlSelect)。selectedIndex;
    
    编辑:回答有关SelectedIndex属性的
    -1
    值的评论

    MSDN:

    可能未选择任何项目。如果未选择任何项目,则 SelectedIndex属性包含的值为-1。这通常是 在页面首次加载且用户未选择项目时发生 默认值。提供代码以在引用项目之前测试此值 否则,如果 索引超出集合的范围


    向Selct HTML控件添加runat属性,以便可以在codebehind中访问它

     <select id="test" runat="server">
          <option value="a">aaa</option>
          <option value="b">bbb</option>
            <option value="c">cc</option>
        </select>
    

    这段代码经过测试。

    这里的问题是相同的:是的,但它是关于选定的值。但是我想要选定的索引,但它总是返回-1。对于选定的值,我使用Request.Form[test.UniqueID]。但是我应该为选定的索引做些什么。请参阅HTML源代码是否有任何选项具有
    selected=“true”
    否没有selected=“true”我用jquery填充selecthtml。怎么办?
    string dd = test.SelectedIndex.ToString();