【问题标题】:Set SelectedValue of DropDownList inside UpdatePanel在 UpdatePanel 中设置 DropDownList 的 SelectedValue
【发布时间】:2011-04-01 20:54:58
【问题描述】:

我有一组级联下拉菜单,但我希望在页面加载时默认选择值存在。 现在,我的下拉列表默认为 DDL 中的第一个值。 (ASP.NET C#)

和代码(问题在于“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 的 DataBound 事件或 Page PreRender 中设置选定的值?
  • 无法在数据绑定事件中执行此操作,因为在更改下拉列表时我正在重新绑定,并且在更改后我不希望它做出选择 Page_PreRender 在我试过了
  • 所以您希望在所有页面加载时使用它,还是只在第一个页面加载?我知道这只是第一次加载页面,因此您可以检查它是否为 !IsPostBack 在 DataBound 中。我可能没有误解你的问题。
  • 我希望它仅在页面加载时具有该选定值

标签: c# asp.net drop-down-menu updatepanel selectedvalue


【解决方案1】:

您需要添加您的代码,以便在页面加载中设置下拉列表选择值。当您在触发事件中更改下拉列表的选定值时,它不会设置选定值。 只需在页面的页面加载事件中调用您的 setter 方法。你的问题会得到解决的。

【讨论】:

  • 我在上面发布的代码在页面加载和 if(!IsPostBack) 下
  • 删除 !IsPostBack 并告诉我发生了什么。也尝试使用选定的索引而不是选定的值
  • 取消 !IsPostBack 会弄乱 updatePanels 并且它仍然默认为页面加载时 DDL 中的第一个选项检查我对 jp2code 关于 selectedIndex 的评论
【解决方案2】:

代替

DDL_Occupation.SelectedValue = posRow.Position;

你需要SelectedIndex吗?

DDL_Occupation.SelectedIndex = posRow.Position;

【讨论】:

  • 不。 DDL_Occupation 很好,Occupation 和 Assignment DDL 都有字符串值,posRow.Position 和 posRow.Assignment 都正确输出字符串
【解决方案3】:

需要在每个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();

【讨论】:

    猜你喜欢
    • 2012-09-06
    • 1970-01-01
    • 2010-11-28
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2013-08-14
    相关资源
    最近更新 更多