【发布时间】:2011-01-15 03:02:57
【问题描述】:
我在使用 .NET DropDownList 控件时遇到了这个问题。
问题:每次我进行回发时,它只是默认使用与所选选项相同的第一个选项标记。我似乎无法得到它,因此它指向实际选择的<option>。
基本上,这就是正在发生的事情。
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