如何根据下拉列表选择填充多个文本框?

时间:2016-09-01 19:54:10

标签: c# asp.net

我有一个带有下拉列表和3个文本框字段的表单。下拉列表应该填充3个文本框。到目前为止,我得到了填充2个文本框,但我不知道如何填充第3个文本框。这是HTML部分:

        <EditItemTemplate>

            <div style="height:50px;overflow:hidden;border:0px;width:100%">
                <asp:Panel runat="server" Visible='<%# IsActive((string)Eval("Status")) %>'>
                <div class="StatusAlert" style="float:left;">   Permit Picked Up</div>
                </asp:Panel>
                <div class="StatusAlert" style="background-color:#808080;float:left">Pending Pick Up</div>
            </div>
            <p>
                Date:
                <asp:Label ID="dateLabel" runat="server" Text='<%# Bind("PickupDate") %>'></asp:Label>
            </p>
                <asp:DropDownList ID="NameSelect" runat="server" DataSourceID="PickupNameSource" AppendDataBoundItems="true" DataTextField="Pickup_Name" DataValueField="PickupDrivers" OnSelectedIndexChanged="NameSelect_SelectedIndexChanged" AutoPostBack="true">
                    <asp:ListItem Text="SELECT CONTRACTOR" Selected="True" Value=""></asp:ListItem>
                </asp:DropDownList>
                <asp:SqlDataSource ID="PickupNameSource" runat="server" ConnectionString="<%$ ConnectionStrings:ROW_PermitsConnectionString %>" 
                    SelectCommand="[ROW].SELECT_PickupContactsDDL" SelectCommandType="StoredProcedure">
                </asp:SqlDataSource>
            <p>
                Name:
                <asp:TextBox ID="nameBox" runat="server" Text='<%# Bind("Pickup_Name") %>'></asp:TextBox>
            </p>
                Phone:
                <asp:TextBox ID="phoneBox" runat="server" Text='<%# Bind("Pickup_Phone") %>'></asp:TextBox>
            <p>
                DL NO:
                <asp:TextBox ID="DriversBox" runat="server" Text='<%# Bind("DriversNumber") %>'></asp:TextBox>
            </p>
            <p>
                Contractor:
                <asp:DropDownList ID="contractorList" runat="server" AutoPostBack="false"  DataSourceID="SelectContractorDB" SelectedValue='<%# Bind("ContractorID") %>'  DataTextField="Company" DataValueField="ContractorID"></asp:DropDownList>
                <asp:SqlDataSource ID="SelectContractorDB" runat="server" ConnectionString="<%$ ConnectionStrings:ROW_PermitsConnectionString %>" SelectCommand="SELECT * FROM [ROW].[All_Contractor_Names]"></asp:SqlDataSource>
            </p>

这是C#部分:

protected void NameSelect_SelectedIndexChanged(object sender,EventArgs e)         {

        DropDownList NameSelect = pickupView.FindControl("NameSelect") as DropDownList;
        TextBox nameBox = pickupView.FindControl("nameBox") as TextBox;
        TextBox phoneBox = pickupView.FindControl("phoneBox") as TextBox;
        TextBox DriversBox = pickupView.FindControl("DriversBox") as TextBox;

        nameBox.Text = NameSelect.SelectedItem.ToString();
        string[] selectedValues = NameSelect.SelectedValue.Split(',');

        //phoneBox.Text = selectedValues[0];
        //DriversBox.Text = selectedValues[1];

        if (selectedValues.Length > 1)
        {
            phoneBox.Text = selectedValues[0];
            DriversBox.Text = selectedValues[1];
        }
        else
        {
            phoneBox.Text = selectedValues[0];
            DriversBox.Text = " ";
        }


    }

我必须修改存储过程

CREATE PROCEDURE [ROW]。[SELECT_PickupContactsDDL]     @DriversNumber varchar(50)=&#39;%&#39;,@ Pickup_Name varchar(500)=&#39;%&#39;,@ Pickup_Phone varchar(50)=&#39;%&#39; 如 BEGIN

SET NOCOUNT ON;

SELECT 
    Pickup_Name, ISNULL(Pickup_Phone,0) + ',' + ISNULL(DriversNumber,0) AS PickupDrivers, pickupDate
FROM 
    ROW.PickupContacts 
GROUP BY 
    Pickup_Name, pickupDate, Pickup_Phone, DriversNumber
ORDER BY 
    pickupDate DESC

END

1 个答案:

答案 0 :(得分:0)

在下拉列表中选择存储过程,将phoneBox和DriversBox连接为一个值。     例如,              123 - phoneBox 345 - DriversBox              123,345 - 最终下拉列表值

在下拉选择事件中,使用拆分方法在3个文本框中显示它。

存储过程:

ALTER PROCEDURE [ROW].[SELECT_PickupContactsDDL] 
AS 
BEGIN 
    SET NOCOUNT ON; 
        SELECT Pickup_Name, Pickup_Phone + ',' + DriversNumber AS [PickupDrivers], pickupDate FROM ROW.PickupContacts GROUP BY Pickup_Name, pickupDate, Pickup_Phone, DriversNumber ORDER BY pickupDate DESC 
END

设计:

<EditItemTemplate>

            <div style="height:50px;overflow:hidden;border:0px;width:100%">
                <asp:Panel runat="server" Visible='<%# IsActive((string)Eval("Status")) %>'>
                <div class="StatusAlert" style="float:left;">   Permit Picked Up</div>
                </asp:Panel>
                <div class="StatusAlert" style="background-color:#808080;float:left">Pending Pick Up</div>
            </div>
            <p>
                Date:
                <asp:Label ID="dateLabel" runat="server" Text='<%# Bind("PickupDate") %>'></asp:Label>
            </p>
                <asp:DropDownList ID="NameSelect" runat="server" DataSourceID="PickupNameSource" AppendDataBoundItems="true" DataTextField="Pickup_Name" DataValueField="***PickupDrivers***" OnSelectedIndexChanged="NameSelect_SelectedIndexChanged" AutoPostBack="true">
                    <asp:ListItem Text="SELECT CONTRACTOR" Selected="True" Value=""></asp:ListItem>
                </asp:DropDownList>
                <asp:SqlDataSource ID="PickupNameSource" runat="server" ConnectionString="<%$ ConnectionStrings:ROW_PermitsConnectionString %>" 
                    SelectCommand="[ROW].SELECT_PickupContactsDDL" SelectCommandType="StoredProcedure">
                </asp:SqlDataSource>
            <p>
                Name:
                <asp:TextBox ID="nameBox" runat="server" Text='<%# Bind("Pickup_Name") %>'></asp:TextBox>
            </p>
                Phone:
                <asp:TextBox ID="phoneBox" runat="server" Text='<%# Bind("Pickup_Phone") %>'></asp:TextBox>
            <p>
                DL NO:
                <asp:TextBox ID="DriversBox" runat="server" Text='<%# Bind("DriversNumber") %>'></asp:TextBox>
            </p>
            <p>
                Contractor:
                <asp:DropDownList ID="contractorList" runat="server" AutoPostBack="false"  DataSourceID="SelectContractorDB" SelectedValue='<%# Bind("ContractorID") %>'  DataTextField="Company" DataValueField="ContractorID"></asp:DropDownList>
                <asp:SqlDataSource ID="SelectContractorDB" runat="server" ConnectionString="<%$ ConnectionStrings:ROW_PermitsConnectionString %>" SelectCommand="SELECT * FROM [ROW].[All_Contractor_Names]"></asp:SqlDataSource>
            </p>

代码隐藏

protected void NameSelect_SelectedIndexChanged(object sender, EventArgs e)
    {

        DropDownList NameSelect = pickupView.FindControl("NameSelect") as DropDownList;
        TextBox nameBox = pickupView.FindControl("nameBox") as TextBox;
        TextBox phoneBox = pickupView.FindControl("phoneBox") as TextBox;
        TextBox DriversBox = pickupView.FindControl("DriversBox") as TextBox;


        nameBox.Text = NameSelect.SelectedItem.ToString();
        string[] selectedValues = NameSelect.SelectedValue.Split(',');
        phoneBox.Text = selectedValues[0];
        DriversBox.Text = selectedValues[1];

    }