Asp.NET DropDownList在PostBack之后重置SelectedIndex

[英]Asp.NET DropDownList Resets SelectedIndex after PostBack


After doing a lot of research online I'm still stumped by this problem. I have a page that loads the names and count of categories into a drop down list. I only do this if !(Page.IsPostBack). When AutoPostBack fires the SelectedIndex = 0. I've tried several different things. Here is my code:

在網上做了很多研究后,我仍然對這個問題感到難過。我有一個頁面,將名稱和類別的數量加載到下拉列表中。我只會這樣做!(Page.IsPostBack)。當AutoPostBack觸發SelectedIndex = 0.我嘗試了幾種不同的東西。這是我的代碼:

PAGE

<form id="AddAssignmentForm" runat="server">
<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" />

<asp:UpdatePanel ID="CommentUpdate" runat="server">
<ContentTemplate>

Add Comment
<asp:DropDownList ID="ddlCategory" runat="server" Width="206" OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" AutoPostBack="true" />
<asp:TextBox ID="txtName" runat="server" Width="200" />
<asp:TextBox ID="txtAbbrv" runat="server" Width="200" />
<asp:TextBox ID="txtDescription" runat="server" Width="200" Height="90" TextMode="MultiLine" />

</ContentTemplate>
</asp:UpdatePanel>
</form>

Here is the back end code.

這是后端代碼。

private void Page_Load(object sender, System.EventArgs e)
{
    if (!Page.IsPostBack)
    {
        GetCategories();
    }
}

public void GetCategories()
{
    String strSql = @"SELECT Name, Total
                        FROM MyTable";

    if (con.State == ConnectionState.Closed)
        con.Open();

    OleDbCommand cmdsql = new OleDbCommand(strSql, con);
    OleDbDataReader cmdReader = cmdsql.ExecuteReader();

    if (cmdReader.HasRows)
    {
        while (cmdReader.Read())
        {
            ddlCategory.Items.Add(new ListItem(cmdReader["Category_Name"].ToString(), cmdReader["Total"].ToString()));

        }
        ddlCategory.SelectedIndex = -1;
    }


    cmdReader.Close();
    con.Close();
}

public void FillForm(int index)
{
    ListItem item = ddlCategory.Items[index];
    txtName.Text = item.Text + " " + (Convert.ToInt32(item.Value) + 1).ToString();
    txtAbbrv.Text = item.Text.Substring(0, 1) + (Convert.ToInt32(item.Value) + 1).ToString();
}

public void ddlCategory_SelectedIndexChanged(Object sender, EventArgs e)
{
    //When I break here SelectedIndex always = 1.
    FillForm(ddlCategory.SelectedIndex);
}

I just want to be able to populate the form based on the selected index, but I can't seem to get the correct answer. Any help is appreciated.

我只是希望能夠根據所選索引填充表單,但我似乎無法得到正確的答案。任何幫助表示贊賞。

8 个解决方案

#1


11  

Add AppendDataBoundItems="true" for dropdown list

為下拉列表添加AppendDataBoundItems =“true”

#2


3  

I discovered the problem. The values being populated from my SQL statement contained values that repeated. For some reason this was causing the entire thing to malfunction in weird ways which made it so that every time I selected a ListItem the whole list would reset. By making sure no values repeated, the code started working perfectly. Thanks for everyone's help.

我發現了這個問題。從我的SQL語句填充的值包含重復的值。由於某種原因,這導致整個事物以奇怪的方式發生故障,這使得每次我選擇ListItem時,整個列表都會重置。通過確保沒有重復的值,代碼開始完美地工作。謝謝大家的幫助。

#3


3  

Make sure that your value fields are unique for each dropdown list item. If each item has the same value, it will default on index 0.

確保每個下拉列表項的值字段都是唯一的。如果每個項目具有相同的值,則它將默認為索引0。

#4


1  

Do you have Viewstate enabled or disabled? SelectedIndex is Zero based, so it were resetting I think it would be set to zero.

您是否啟用了Viewstate? SelectedIndex是基於零的,所以它重置我認為它將被設置為零。

Also, what do the other properties of the drop down list get set to? Is the selected value correct?

此外,下拉列表的其他屬性設置為什么?所選值是否正確?

Try a different browser. I had an issue with cascading drop downs where it wasn't firing/behaving correctly in Firefox.

嘗試其他瀏覽器。我有一個級聯下拉的問題,它沒有在Firefox中正常觸發/表現。

#5


1  

This happened to me when attempting to use a combined column value for the DataValueField. For example:

嘗試使用DataValueField的組合列值時,我遇到了這種情況。例如:

The stored procedure was written like this:

存儲過程是這樣寫的:

SELECT
    Description,
    Value1 + ',' + Value2 AS Value
FROM
    DropDownListTable

And the DataValueField used the Value field which was a combination of the Value1 and Value2 fields separated by a comma. (I also tried a pipe and no delimiter but had the same results)

DataValueField使用Value字段,它是由逗號分隔的Value1和Value2字段的組合。 (我也嘗試了一個管道,沒有分隔符,但結果相同)

 With ddl
     .DataTextField = "Description"
     .DataValueField = "Value"
     .DataSource = ds
     .DataBind() 
 End With

As soon as I used Value1 or Value2 as the DataValueField, the problem went away.

一旦我使用Value1或Value2作為DataValueField,問題便消失了。

#6


0  

I've been experiencing the same problem, my dropdownlist stateview jump to index 1 right after a postback event from another control. My suggestion simply make sure your dropdownlist values are not empty.

我一直遇到同樣的問題,我的dropdownlist狀態視圖在另一個控件的回發事件后立即跳轉到索引1。我的建議只是確保您的下拉列表值不為空。

Hope its help someone .... :)

希望有人幫助.... :)

#7


0  

I struggled with this too, I tried EnableViewState="true" ViewStateMode="Enabled" but it's not needed in fact, you just have to addd IsPostBack in Page_Load event. Do not forget to add IsPostBack, that is it...

我也在努力解決這個問題,我嘗試了EnableViewState =“true”ViewStateMode =“Enabled”但實際上並不需要它,你只需要在Page_Load事件中添加IsPostBack。別忘了添加IsPostBack,就是它......

if (!IsPostBack)
{
    LoadDropDown();
}

#8


0  

You have to load list to DropDownList if not IsPostBack

如果不是IsPostBack,您必須將列表加載到DropDownList

Example code:

if (!IsPostBack)
{
   //fill here
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/04/13/8d2ba5f80d3899b59fc2814876b9209d.html



 
  © 2014-2022 ITdaan.com 联系我们: