1

我正在使用 UpdatePanel 到 DetailsView,其中有一个 DropDonwList。DropDownList 工作在 DetailView 中找到,但我无法从代码隐藏文件中的 DropDownList 中获取选定值。以下是 ASP.NET 代码:

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"></asp:ScriptManagerProxy>
  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate> 
<asp:DetailsView ID="detailsView_User" runat="server" AutoGenerateRows="False" 
  DefaultMode="Insert">
  <Fields>
    <asp:BoundField DataField="Name" HeaderText="User Name" SortExpression="Name" ControlStyle-Width="200" />

  <asp:TemplateField>
    <InsertItemTemplate>          
      <asp:DropDownList ID="dropDownList_Agency" runat="server" SelectMethod="GetAllAgency" AutoPostBack="true" OnSelectedIndexChanged="dropDownList_Agency_SelectedIndexChanged"></asp:DropDownList><br />
      <asp:DropDownList ID="dropDownList_Department" runat="server" SelectMethod="GetAllDepartment" AutoPostBack="true"></asp:DropDownList><br />

      <asp:Button ID="button_Cancel" runat="server" Text="Cancel" OnClick="button_Cancel_Click"/>
      <asp:Button ID="button_CreateUser" runat="server" Text="Create User"/>
    </InsertItemTemplate>
 </asp:TemplateField>
 </Fields>

</asp:DetailsView>
      </ContentTemplate>
    <Triggers>
      <asp:PostBackTrigger ControlID="detailsView_User" /> 
    </Triggers>
   </asp:UpdatePanel>

当我尝试 SelectedValue

dropDownList_Agency.SelectedValue;

我收到此错误:当前上下文中不存在名称“dropDownList_Agency”
我不知道为什么。你有解决方案的想法吗?

4

1 回答 1

0

对于选定的值,您需要使用 IndexOf:

myDropDown.SelectedIndex = 
myDropDown.Items.IndexOf(myDropDown.Items.FindByValue("myValue"))
于 2014-12-16T13:24:33.553 回答