user3736492 user3736492 - 3 months ago 25
ASP.NET (C#) Question

How do I populate multiple textboxes based on a dropdown list selection?

I have a form with a dropdown list and 3 textbox fields. The dropdown list is supposed to populate the 3 textboxes. So far I got it to populate 2 textboxes but I don't know how I can populate the 3rd one. Here is the HTML portion:



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


Here is the C# portion:

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 = " ";
}


}


I had to modify the Stored procedure

CREATE PROCEDURE [ROW].[SELECT_PickupContactsDDL]
@DriversNumber varchar(50) = '%', @Pickup_Name varchar(500) = '%', @Pickup_Phone varchar(50) = '%'
AS
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

Answer

In your dropdown select stored procedure, concatenate phoneBox and DriversBox as one value. For Example, 123 - phoneBox 345 - DriversBox 123,345 - Final drop down list value

On Dropdown selection event, use Split method to show it in 3text boxes.

Stored Procedure:

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

DESIGN:

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

Code-Behind

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

    }