当我尝试获取
中的所选项目时,我有aspx和aspx.cs文件DropDownList
它始终将项目设为0索引
<div style="text-align:center">
<b>Scegliere il nome del report * </b>
<asp:DropDownList id="dropdownlist1" style="width:250px;" runat="server"></asp:DropDownList>
<br />
<br />
<br />
<b>Scegliere la data del report </b>
<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>
c#代码
protected double size = 1;
private string connectionString;
private OracleConnection connection;
private OracleCommand processNumQuery;
private int indexdropitem;
protected void Page_Load(object sender, EventArgs e)
{
if (Request["CONNECTSTRING"] == null && Session["CONNECTSTRING"] == null)
{
Response.Redirect("sessionup.asp?type=Pubreport");
}
else
{
if (Request["CONNECTSTRING"] != null)
{
connectionString = Request["CONNECTSTRING"].ToString();
}
else
{
connectionString = Session["CONNECTSTRING"].ToString();
}
if (connectionString.IndexOf("DSN=") >= 0)
{
Utility util = new Utility();
connectionString = util.ConnStr(connectionString);
}
Session["CONNECTSTRING"] = connectionString;
connection = new OracleConnection(connectionString);
connection.Open();
}
if (!IsPostBack)
{
processNumQuery = new OracleCommand("select distinct nome_report from rpg_notification",connection);
OracleDataReader reader = processNumQuery.ExecuteReader();
while (reader.Read())
{
dropdownlist1.Items.Insert(0, new ListItem(reader.GetString(0), ""));
}
reader.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("try :" + dropdownlist1.SelectedIndex + " - " + txtSchedDate.Text + " - " + dropdownlist1.Items[dropdownlist1.SelectedIndex].Text + " - " + Request["txtSchedDate"] + " - ");
}
我想获得0指数,那我该怎么办?
答案 0 :(得分:1)
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));
答案 1 :(得分:0)
这应该有用......只是出于调试目的,尝试拨打dropdownlist1.SelectedItem.Text
而不是dropdownlist1.Items[dropdownlist1.SelectedIndex].Text
,看看你得到了什么