我有一組級聯下拉菜單,但是我希望當頁面加載時默認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();
讓我知道是否需要任何其他細節
你嘗試在DDL的數據綁定事件或頁面的PreRender設置所選值之前數據綁定? – gbs 2011-04-01 21:08:00
無法在數據綁定事件中執行此操作,因爲我在更改下拉列表並更改下拉列表後重新綁定我不希望它做出該選擇 Page_PreRender在我試過時沒有什麼作用 – abney317 2011-04-01 21:38:52
因此,您想要在所有頁面加載或只是第一個?我明白這只是第一次加載頁面,所以你可以檢查它!IsPostBack在DataBound中。我可能不會誤解你的問題。 – gbs 2011-04-01 21:47:49