在 UpdatePanel 中设置 DropDownList 的 SelectedValue

发布于 2024-10-29 09:44:26 字数 5647 浏览 5 评论 0原文

我有一组级联下拉菜单,但我希望在页面加载时显示默认的选定值。 现在,我的下拉列表默认为 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();

请告诉我是否需要任何其他详细信息

I've got a set of cascading dropdowns, but I want the default selectedvalue to be there when when page loads though.
Right now, my dropdownlist is defaulting to the first value in the DDL.
(ASP.NET C#)

and the code (issue is with the "DDL_Assignment" dropdown)...

<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>

behind code
"posRow.Assignment" outputs the correct value... the dropdownlist just isn't taking it in for some reason

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();

let me know if any other details are needed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁轨上的流浪者 2024-11-05 09:44:27

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

needed to databind before each 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();
┾廆蒐ゝ 2024-11-05 09:44:26

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

You need to add your code witch is setting dropdown list selected value in the page load. when you change selected value of a dropdown list in fired event it would not set the selected value.
just call your setter method in page load event of page. you problem will be solved.

燕归巢 2024-11-05 09:44:26

而不是

DDL_Occupation.SelectedValue = posRow.Position;

您需要 SelectedIndex 吗?

DDL_Occupation.SelectedIndex = posRow.Position;

Instead of

DDL_Occupation.SelectedValue = posRow.Position;

Could you need SelectedIndex?

DDL_Occupation.SelectedIndex = posRow.Position;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文