【问题标题】:.NET DropDownList postback selection won't select anything but first <option>.NET DropDownList 回发选择除了第一个 <option> 不会选择任何东西
【发布时间】:2011-01-15 03:02:57
【问题描述】:

我在使用 .NET DropDownList 控件时遇到了这个问题。

问题:每次我进行回发时,它只是默认使用与所选选项相同的第一个选项标记。我似乎无法得到它,因此它指向实际选择的&lt;option&gt;

基本上,这就是正在发生的事情。

1 .我在 Default.aspx 中制作了一个 DropDownList 控件

<asp:DropDownList ID="controlSelector" AutoPostBack="true" 
    OnSelectedIndexChanged="onSelectChange" runat="server" />

2 。我从数据库中提取数据

DevHTMLGetter getControls = new DevHTMLGetter();

DataTable queryResult = new DataTable();
queryResult = getControls.getControlNames("getAdminHTML");

// binds the DataTable to the DropDownList
controlSelector.DataTextField = "controlName";
controlSelector.DataValueField = "controlID";
controlSelector.DataSource = queryResult;
controlSelector.DataBind();

数据是:

    ---------------------------
    | CONTROLID | CONTROLNAME |
    ---------------------------
    |    1      | testcontrol |
    ---------------------------
    |    2      | tstcontrol2 |
    ---------------------------

3 .我不会在提交表单时尝试操作数据:

protected void displayControlsHTML(Object sender, EventArgs
{
    String selectedItem = controlSelector.Attributes["selected2"].ToString();
    String n = controlSelector.Items.FindByText(selectedItem).ToString();

    DevHTMLGetter getControls = new DevHTMLGetter();
    Dictionary<String, String> displayItems = 
        getControls.getControlsForEdit("getSpecificControlItems", selectedItem); 
        // from Web.Config <AppSettings>

    //sets all of the boxes to their appropriate text
    txtControlName.Text = displayItems["controlName"].ToString();
    txtControlClassName.Text = displayItems["className"].ToString();
    txtLiveHTMLEditBox.Text = displayItems["controlHTML"].ToString();
    txtDisplayHTMLEditBox.Text = displayItems["displayHTML"].ToString();
}

我的页面呈现如下:

<select class="myDropDown" id="ctl00_defaultContent_controlSelector" name="ctl00$defaultContent$controlSelector">
    <option value="1" selected="selected">Test Control</option>
    <option value="2">Test Control2</option>
</select>

注意:onSelectChange,DropDownList 控件上的事件不执行任何操作,因为我使用 SUMBMIT 按钮提交它。

【问题讨论】:

    标签: .net select drop-down-menu postback


    【解决方案1】:

    听起来您可能正在重新对回发下拉列表进行数据绑定。

    尝试将if (!PostBack) { ... } 语句包裹在您的数据绑定代码周围:

        if (!IsPostBack)
        {
            controlSelector.DataTextField = "controlName";
            controlSelector.DataValueField = "controlID";
            controlSelector.DataSource = queryResult;
            controlSelector.DataBind();
        }
    

    【讨论】:

    • 我知道我错过了一些东西。谢谢!
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 2017-01-31
    • 1970-01-01
    相关资源
    最近更新 更多