我有一个下拉列表和一个添加按钮。单击添加按钮时,下拉列表中的所选项目应添加到数据库中。但是,add按钮调用的函数中使用的selectedIndex值是错误的。它与当前所选项目的实际索引(大多数时间)不匹配。
这是下拉菜单和按钮:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (ddParent1.Items.Count == 0)
{
if (ddBaby.Items.Count == 0)
{
loadCreatureLists(true, true);
}
else
{
loadCreatureLists(true, false);
}
}
else
{
if (ddBaby.Items.Count == 0)
{
loadCreatureLists(false, true);
}
}
}
}
protected void loadCreatureLists(Boolean loadParents, Boolean loadOffspring )
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Drin"].ConnectionString);
string sSql = "select creature, element1, element2 from dbo.tinycastlecreatures order by creature";
SqlCommand cmd = new SqlCommand(sSql, conn);
SqlDataReader reader;
try
{
conn.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
ListItem newItem = new ListItem();
newItem.Text = reader["creature"].ToString();
newItem.Value = normalizeElementOrder(reader["element1"].ToString(),reader["element2"].ToString());
if (loadParents && !chkMine.Checked)
{
ddParent1.Items.Add(newItem);
ddParent2.Items.Add(newItem);
}
if (loadOffspring)
{
ddBaby.Items.Add(newItem);
ddlNewCreature.Items.Add(newItem);
}
}
reader.Close();
}
catch (Exception err) { }
finally { if (!loadParents || !chkMine.Checked) { conn.Close(); } }
}
protected void AddCreature(object sender, EventArgs e)
{
int indx = ddlNewCreature.SelectedIndex;
string sCreature = ddlNewCreature.Items[indx].Text.ToString();
for (int i = 0; i < ddlNewCreature.Items.Count; i++ )
{
sCreature = ddlNewCreature.Items[i].Text.ToString();
}
ddlNewCreature.SelectedIndex = 0;
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Drin"].ConnectionString);
conn.Open();
string sSql = "[dbo].[tinycastle_add_user_creature] '";
sSql += sCreature;
sSql += "','deb'";
SqlCommand cmd = new SqlCommand(sSql, conn);
cmd.ExecuteNonQuery();
sCreature = ddlNewCreature.Items[indx].Text.ToString();
sqlMineSource.DataBind();
gridMine.DataBind();
}
这是背后的代码:
ffmpeg -i in.mpg -level 3.1 -vformat mpeg4 -qscale 1 -f mov out.mov
答案 0 :(得分:0)
B.Yaylaci关于检查下拉列表更改事件中SelectedIndex
的建议是在正确的轨道上找出原因(何时?:))索引与您的预期不匹配,我认为。
此外,如果您需要的只是下拉列表中的文本,那么使用SelectedValue
属性会更容易(也更准确)。
https://stackoverflow.com/a/4399747
答案 1 :(得分:0)
我通过撕掉填充各种下拉列表的方式来规避问题,然后将它们绑定到SQLDataStore。我不知道为什么会有所帮助,但事实确实如此。
答案 2 :(得分:0)
使用ddlNewCreature.SelectedValue这将解决您的问题。&#34; SelectedValue&#34;为您选择的DropDownList提供正确的索引值。这对我有用。