Asp.net asp DropDownList始终通过回发检查选择0索引

Asp.net asp DropDownList始终通过回发检查选择0索引,asp.net,postback,code-behind,Asp.net,Postback,Code Behind,当我尝试获取所选项目时,我有aspx和aspx.cs文件 DropDownList 它总是在索引0处获取项 <div style="text-align:center"> <b>Scegliere il nome del report * </b> &nbsp; <asp:DropDownList id="dropdownlist1" style="width:250px;" runat=

当我尝试获取所选项目时,我有aspx和aspx.cs文件

DropDownList
它总是在索引0处获取项

<div style="text-align:center">
             <b>Scegliere il nome del report *  </b>  &nbsp;
             <asp:DropDownList id="dropdownlist1" style="width:250px;" runat="server"></asp:DropDownList>
            <br />
            <br /> 

            <br /> 
            <b>Scegliere la data del report </b>  &nbsp; 
            <asp:TextBox runat="server" ID="txtSchedDate" name="txtSchedDate" type="text" cssclass="datebox" style="height:20px;" ReadOnly="true"/>
            <br />
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Pubblica" OnClientClick="" OnClick="Button1_Click" />
            <br />
            <br />
            <br />
            <br />
            <asp:Label ID="Label1" Font-Names="Arial" Font-Size="15px" runat="server" ForeColor="Red" Font-Bold="True" Text=""></asp:Label>
            <div id="divUpload" style="display:none">
            <div  style="width:200pt;;text-align:center;">Pubblicando...</div>
            </div>
        </div>

我总是得到0索引,请告诉我必须做什么???

这应该有效…只是为了调试建议,试着调用
dropdownlist1.SelectedItem.Text
而不是
dropdownlist1.Items[dropdownlist1.SelectedIndex]。Text
看看你得到了什么

这应该有效…只是为了调试建议,尝试调用
dropdownlist1.SelectedItem.Text
而不是
dropdownlist1.Items[dropdownlist1.SelectedIndex].Text
,然后查看您得到的结果ASP.NET不够“聪明”,无法通过提交的下拉列表的实际选定索引。相反,它取决于浏览器发送选定的值,然后取决于具有不同值的项目

如果存在具有相同值的项,并且选择了其中一项,则服务器端SelectedIndex将返回具有该值的第一项的索引。我刚刚创建了一个快速测试,并被证明是正确的。(对不起,不熟悉任何.NET小提琴)

在您的特定情况下,由于该行,所有项目都有空值:

dropdownlist1.Items.Insert(0, new ListItem(reader.GetString(0), ""));
要“修复”您的问题,只需添加一个值:

string myValue = reader.GetString(0);
dropdownlist1.Items.Insert(0, new ListItem(myValue, myValue));
ASP.NET不够“智能”,无法通过已提交下拉列表的实际选定索引。相反,它取决于浏览器发送选定的值,然后取决于具有不同值的项目

如果存在具有相同值的项,并且选择了其中一项,则服务器端SelectedIndex将返回具有该值的第一项的索引。我刚刚创建了一个快速测试,并被证明是正确的。(对不起,不熟悉任何.NET小提琴)

在您的特定情况下,由于该行,所有项目都有空值:

dropdownlist1.Items.Insert(0, new ListItem(reader.GetString(0), ""));
要“修复”您的问题,只需添加一个值:

string myValue = reader.GetString(0);
dropdownlist1.Items.Insert(0, new ListItem(myValue, myValue));

它总是得到第一个没有任何更改的响应。写入(“try”+dropdownlist1.SelectedIndex+“-”+dropdownlist1.SelectedItem.Text+“-”+dropdownlist1.Items[dropdownlist1.SelectedIndex].Text+“-”+请求[“txtSchedDate”]+“-”;0,firstitem,firstitem,…如果在页面加载代码中没有再次填充DropDownList 1,是否签入调试?我曾经遇到过类似的事情。它总是第一个得到什么都没有改变的响应。写(“try”+dropdownlist1.SelectedIndex+“-”+dropdownlist1.SelectedIndex.Text+“-”+dropdownlist1.Items[dropdownlist1.SelectedIndex]。Text+“-”+请求[“txtSchedDate”]+“-”;0,firstitem,firstitem,…如果在页面加载代码中没有再次填充DropDownList 1,是否签入调试?我也经历过类似的事情。列表中没有一个项目有值。尝试将行更改为:
dropdownlist1.Items.Insert(0,新列表项(reader.GetString(0),reader.GetString(0))我是Dumbassssss:(((((((((()非常感谢Shadow!干杯,这不是小事,所以我要发布一个答案。列表中的所有项目都没有值。请尝试将行更改为:
dropdownlist1.items.Insert(0,新列表项(reader.GetString(0),reader.GetString(0));
我是Dumbassssss:(((((非常感谢你!干杯,这不是小事,所以我将发布一个答案。