2011-04-01 90 views
1

我有一组级联下拉菜单,但是我希望当页面加载时默认selectedvalue在那里。 现在,我的下拉列表默认为DDL中的第一个值。 (ASP.NET C#)在UpdatePanel中设置DropDownList的SelectedValue

和代码(问题是与 “DDL_Assignment” 下拉列表中)...

<tr> 
     <td> 
      <b>Position Type:</b> 
     </td> 
     <td> 
      <asp:Label ID="Lbl_PositionType" runat="server" /> 
     </td> 
    </tr> 
    <tr id="TR_Occupation" runat="server"> 
     <td> 
      <b>Select Occupation:</b> 
     </td> 
     <td> 
      <asp:DropDownList ID="DDL_Occupation" runat="server" DataSourceID="DataSource_Occupation" DataTextField="Position" DataValueField="Position" 
       AutoPostBack="True" OnSelectedIndexChanged="DDL_Occupation_SelectedIndexChanged"> 
      </asp:DropDownList> 

      <asp:ObjectDataSource ID="DataSource_Occupation" runat="server" 
       OldValuesParameterFormatString="original_{0}" SelectMethod="GetPositions" 
       TypeName="HumanResourceTableAdapters.PositionTableAdapter"> 
       <SelectParameters> 
        <asp:ControlParameter ControlID="Lbl_PositionType" Name="PositionType" 
         PropertyName="Text" Type="String" /> 
       </SelectParameters> 
      </asp:ObjectDataSource> 

      <asp:TextBox ID="TB_Occupation" runat="server" /> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <b>Select Assignment Name:</b> 
     </td> 
     <td> 
      <asp:UpdatePanel ID="UP_Assignment" runat="server"> 
       <ContentTemplate> 
        <asp:DropDownList ID="DDL_Assignment" runat="server" 
         AutoPostBack="True" 
         OnSelectedIndexChanged="DDL_Assignment_SelectedIndexChanged" /> 
        <asp:ObjectDataSource ID="DataSource_Assignment" runat="server" 
         OldValuesParameterFormatString="original_{0}" SelectMethod="GetAssignments" 
         TypeName="HumanResourceTableAdapters.PositionTableAdapter"> 
         <SelectParameters> 
          <asp:ControlParameter ControlID="DDL_Occupation" Name="Position" 
           PropertyName="SelectedValue" Type="String" /> 
          <asp:ControlParameter ControlID="Lbl_PositionType" Name="PositionType" 
           PropertyName="Text" Type="String" /> 
         </SelectParameters> 
        </asp:ObjectDataSource> 
        <asp:ObjectDataSource ID="DataSource_AssignmentHourly" runat="server" 
         OldValuesParameterFormatString="original_{0}" 
         SelectMethod="GetAssignmentByPosType" 
         TypeName="HumanResourceTableAdapters.PositionTableAdapter"> 
         <SelectParameters> 
          <asp:ControlParameter ControlID="Lbl_PositionType" Name="PositionType" 
           PropertyName="Text" Type="String" /> 
         </SelectParameters> 
        </asp:ObjectDataSource> 
        <asp:TextBox ID="TB_Assignment" runat="server" /> 
       </ContentTemplate> 
       <Triggers> 
        <asp:AsyncPostBackTrigger ControlID="DDL_Occupation" EventName="SelectedIndexChanged" /> 
       </Triggers> 
      </asp:UpdatePanel> 

     </td> 
    </tr> 

代码隐藏 “posRow.Assignment” 输出正确的值...的下拉列表只是没有把它在出于某种原因

Position1TableAdapter position1TableAdapter = 
       new Position1TableAdapter(); 
      HumanResource.Position1Row posRow = 
       position1TableAdapter.GetData(Convert.ToInt32(Request.QueryString["PositionID"]))[0]; 

      DDL_Assignment.DataTextField = "AssignmentName"; 
      DDL_Assignment.DataValueField = "AssignmentName"; 
      DDL_Assignment.DataSourceID = "DataSource_Assignment"; 

      DDL_Occupation.SelectedValue = posRow.Position; 
      DDL_Assignment.SelectedValue = posRow.Assignment; 
      TB_Assignment.Text = posRow.Assignment; 
      TB_Replaced.Text = posRow.Replaced; 
      DDL_PositionDays.SelectedValue = posRow.PositionDays.ToString(); 
      DDL_ContractDays.SelectedValue = posRow.ContractDays.ToString(); 
      DDL_PositionHours.SelectedValue = posRow.PositionHours.ToString(); 
      DDL_Location.SelectedValue = posRow.Location.ToString(); 
      TB_Contract.Text = posRow.IsContractInformationNull() ? null : posRow.ContractInformation; 
      DDL_1yrContractReason.SelectedValue = posRow.Isposition_1yrcntrctrsnIDNull() ? null : posRow.position_1yrcntrctrsnID.ToString(); 
      RBL_Administrator.SelectedValue = posRow.Administrator.ToString(); 
      RBL_CertifiedSalarySchedule.SelectedValue = posRow.clas_CertifiedScheduleInd.ToString(); 
      RBL_OvertimeExempt.SelectedValue = posRow.position_OvertimeExemptInd.ToString(); 
      RBL_ExtendedContractExempt.SelectedValue = posRow.position_ExtContractExemptInd.ToString(); 
      RBL_LongevityException.SelectedValue = posRow.Longevity_Exception_Indicator.ToString(); 
      RBL_NoStepIncrease.SelectedValue = posRow.position_NoStepInd.ToString(); 
      RBL_JobShare.SelectedValue = posRow.position_JobShareInd.ToString(); 

让我知道是否需要任何其他细节

+0

你尝试在DDL的数据绑定事件或页面的PreRender设置所选值之前数据绑定? – gbs 2011-04-01 21:08:00

+0

无法在数据绑定事件中执行此操作,因为我在更改下拉列表并更改下拉列表后重新绑定我不希望它做出该选择 Page_PreRender在我试过时没有什么作用 – abney317 2011-04-01 21:38:52

+0

因此,您想要在所有页面加载或只是第一个?我明白这只是第一次加载页面,所以你可以检查它!IsPostBack在DataBound中。我可能不会误解你的问题。 – gbs 2011-04-01 21:47:49

回答

0

需要每个DDL

Position1TableAdapter position1TableAdapter = 
       new Position1TableAdapter(); 
      HumanResource.Position1Row posRow = 
       position1TableAdapter.GetData(Convert.ToInt32(Request.QueryString["PositionID"]))[0]; 

      DDL_Assignment.DataTextField = "AssignmentName"; 
      DDL_Assignment.DataValueField = "AssignmentName"; 
      DDL_Assignment.DataSourceID = "DataSource_Assignment"; 

      DDL_Occupation.DataBind(); 
      DDL_Occupation.SelectedValue = posRow.Position; 

      DDL_Assignment.DataBind(); 
      DDL_Assignment.SelectedValue = posRow.Assignment; 

      TB_Replaced.Text = posRow.IsReplacedNull() ? null : posRow.Replaced; 

      DDL_PositionDays.DataBind(); 
      DDL_PositionDays.SelectedValue = posRow.PositionDays.ToString(); 

      DDL_ContractDays.DataBind(); 
      DDL_ContractDays.SelectedValue = posRow.ContractDays.ToString(); 

      DDL_PositionHours.DataBind(); 
      DDL_PositionHours.SelectedValue = posRow.PositionHours.ToString(); 
0

您需要添加鳕鱼女巫在页面加载中设置下拉列表选定值。当您在触发事件中更改下拉列表的选定值时,它不会设置所选值。 只是在页面的页面加载事件中调用setter方法。你的问题将得到解决。

+0

我上面发布的代码是在页面加载下,如果(!IsPostBack) – abney317 2011-04-01 21:09:09

+0

删除!IsPostBack并告诉我发生了什么。也尝试使用选定的索引,而不是选定的值 – 2011-04-01 21:13:21

+0

起飞!IsPostBack弄脏了updatePanels,它仍然默认为页面加载DDL中的第一个选项,查看我对jp2code的有关selectedIndex – abney317 2011-04-01 21:18:33

0

而不是

DDL_Occupation.SelectedValue = posRow.Position;

你能需要SelectedIndex

DDL_Occupation.SelectedIndex = posRow.Position; 
+0

的评论。 DDL_Occupation没有问题,并且Occupation和Assignment DDL都有字符串值,posRow.Position和posRow.Assignment都正确地输出了sting – abney317 2011-04-01 21:15:52

相关问题