0

I'm making a database to keep track of wireless projects from a class room. I have most of it working the way I want it to, but I'm having trouble getting information from the dropdown lists into the textbox. Rather than to type in the magnitude themselves, I put them all in a dropdown list. When they type in their Frequency (900) or power (50)they would select the correct magnitude (MHz and mW) for the respective dropdown list. I can't seem to access the DropDown List from the codebehind in c#. I assume it is because it is in the DetailsView box, but I'm sure there is another way. I posted here is the site as it is. (sorry for the long code) What works: Venue, GroupName, Technology, Channel, Notes, Active What doesn't work: Dropdown list for Frequency, Power and Bandwidth.

<%@ Page Title="Insert and Update Frequencies" Language="C#" 

MasterPageFile="~/MasterPages/Management.master" AutoEventWireup="true" CodeFile="AddEditFrequency.aspx.cs" Inherits="Management_AddEditFrequency" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" DefaultMode="Insert" Height="50px" Width="125px" OnItemInserted="DetailsView1_ItemInserted" OnItemUpdated="DetailsView1_ItemUpdated">
        <Fields>
            <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
            <asp:TemplateField HeaderText="Venue" SortExpression="Venue">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Venue") %>'>
                        <asp:ListItem>Select a Venue</asp:ListItem>
                        <asp:ListItem Text="Laboratory">Laboratory</asp:ListItem>
                        <asp:ListItem Text="Gymnasium">Gymnasium</asp:ListItem>
                        <asp:ListItem Text="Convention Center">Convention Center</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Venue") %>'>
                        <asp:ListItem>Select a Venue</asp:ListItem>
                        <asp:ListItem Text="Laboratory">Laboratory</asp:ListItem>
                        <asp:ListItem Text="Gymnasium">Gymnasium</asp:ListItem>
                        <asp:ListItem Text="Convention Center">Convention Center</asp:ListItem>
                    </asp:DropDownList>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="GroupName" SortExpression="GroupName">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="GroupsDataSource" DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("GroupName") %>'>
                    </asp:DropDownList>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="GroupsDataSource" DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("GroupName") %>'>
                    </asp:DropDownList>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Technology" SortExpression="Technology">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Technology") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ForeColor="Red" ErrorMessage="Enter a Technology Name" ControlToValidate="TextBox3" runat="server" />
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Technology") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ForeColor="Red" ErrorMessage="Enter a Technology Name" ControlToValidate="TextBox3" runat="server" />
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Frequency" SortExpression="Frequency">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Frequency") %>'></asp:TextBox>
                    <asp:DropDownList ID="DropDownList4" runat="server">
                        <asp:ListItem>Select a magnitude</asp:ListItem>
                        <asp:ListItem Text="mHz">milli-Hertz</asp:ListItem>
                        <asp:ListItem Text="Hz">Hertz</asp:ListItem>
                        <asp:ListItem Text="kHz">kilo-Hertz</asp:ListItem>
                        <asp:ListItem Text="MHz">Mega-Hertz</asp:ListItem>
                        <asp:ListItem Text="GHz">Giga-Hertz</asp:ListItem>
                        <asp:ListItem Text="THz">Terra-Hertz</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ForeColor="Red" ErrorMessage="Enter a Frequency" ControlToValidate="TextBox4" runat="server" />
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Frequency") %>'></asp:TextBox>
                    <asp:DropDownList ID="DropDownList4" runat="server">
                        <asp:ListItem>Select a magnitude</asp:ListItem>
                        <asp:ListItem Text="mHz">milli-Hertz</asp:ListItem>
                        <asp:ListItem Text="Hz">Hertz</asp:ListItem>
                        <asp:ListItem Text="kHz">kilo-Hertz</asp:ListItem>
                        <asp:ListItem Text="MHz">Mega-Hertz</asp:ListItem>
                        <asp:ListItem Text="GHz">Giga-Hertz</asp:ListItem>
                        <asp:ListItem Text="THz">Terra-Hertz</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ForeColor="Red" ErrorMessage="Enter a Frequency" ControlToValidate="TextBox4" runat="server" />
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Power" SortExpression="Power">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Power") %>'></asp:TextBox>
                    <asp:DropDownList ID="DropDownList5" runat="server">
                        <asp:ListItem>Select a magnitude</asp:ListItem>
                        <asp:ListItem Text="nW">nano-Watt</asp:ListItem>
                        <asp:ListItem Text="uW">micro-Watt</asp:ListItem>
                        <asp:ListItem Text="mW">milli-Watt</asp:ListItem>
                        <asp:ListItem Text="W">Watt</asp:ListItem>
                        <asp:ListItem Text="kW">kilo-Watt</asp:ListItem>
                        <asp:ListItem Text="MW">Mega-Watt</asp:ListItem>
                        <asp:ListItem Text="GW">Giga-Watt</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ForeColor="Red" ErrorMessage="Enter a Power value" ControlToValidate="DropDownList5" runat="server" />
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Power") %>'></asp:TextBox>
                    <asp:DropDownList ID="DropDownList5" runat="server">
                        <asp:ListItem>Select a magnitude</asp:ListItem>
                        <asp:ListItem Text="nW">nano-Watt</asp:ListItem>
                        <asp:ListItem Text="uW">micro-Watt</asp:ListItem>
                        <asp:ListItem Text="mW">milli-Watt</asp:ListItem>
                        <asp:ListItem Text="W">Watt</asp:ListItem>
                        <asp:ListItem Text="kW">kilo-Watt</asp:ListItem>
                        <asp:ListItem Text="MW">Mega-Watt</asp:ListItem>
                        <asp:ListItem Text="GW">Giga-Watt</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ForeColor="Red" ErrorMessage="Enter a Power value" ControlToValidate="DropDownList5" runat="server" />
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Bandwidth" SortExpression="Bandwidth">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Bandwidth") %>'></asp:TextBox>
                    <asp:DropDownList ID="DropDownList6" runat="server">
                        <asp:ListItem>Select a magnitude</asp:ListItem>
                        <asp:ListItem Text="mHz">milli-Hertz</asp:ListItem>
                        <asp:ListItem Text="Hz">Hertz</asp:ListItem>
                        <asp:ListItem Text="kHz">kilo-Hertz</asp:ListItem>
                        <asp:ListItem Text="MHz">Mega-Hertz</asp:ListItem>
                        <asp:ListItem Text="GHz">Giga-Hertz</asp:ListItem>
                        <asp:ListItem Text="THz">Terra-Hertz</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ForeColor="Red" ErrorMessage="Enter a Bandwidth" ControlToValidate="DropDownList6" runat="server" />
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Bandwidth") %>'></asp:TextBox>
                    <asp:DropDownList ID="DropDownList6" runat="server">
                        <asp:ListItem>Select a magnitude</asp:ListItem>
                        <asp:ListItem Text="mHz">milli-Hertz</asp:ListItem>
                        <asp:ListItem Text="Hz">Hertz</asp:ListItem>
                        <asp:ListItem Text="kHz">kilo-Hertz</asp:ListItem>
                        <asp:ListItem Text="MHz">Mega-Hertz</asp:ListItem>
                        <asp:ListItem Text="GHz">Giga-Hertz</asp:ListItem>
                        <asp:ListItem Text="THz">Terra-Hertz</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ForeColor="Red" ErrorMessage="Enter a Bandwidth" ControlToValidate="DropDownList6" runat="server" />
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Channel" HeaderText="Channel" SortExpression="Channel" />
            <asp:TemplateField HeaderText="Notes" SortExpression="Notes">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox7" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("Notes") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox7" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("Notes") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:CheckBoxField DataField="Active" HeaderText="Active" SortExpression="Active" />
            <asp:CommandField ShowEditButton="True" ShowInsertButton="True" CancelText="Clear" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FrequencyTrackerConnectionString1 %>" DeleteCommand="DELETE FROM [Listing] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Listing] ([Venue], [GroupName], [Technology], [Frequency], [Power], [Bandwidth], [Channel], [Notes], [Active]) VALUES (@Venue, @GroupName, @Technology, @Frequency, @Power, @Bandwidth, @Channel, @Notes, @Active)" SelectCommand="SELECT * FROM [Listing] WHERE ([Id] = @Id)" UpdateCommand="UPDATE [Listing] SET [Venue] = @Venue, [GroupName] = @GroupName, [Technology] = @Technology, [Frequency] = @Frequency, [Power] = @Power, [Bandwidth] = @Bandwidth, [Channel] = @Channel, [Notes] = @Notes, [Active] = @Active WHERE [Id] = @Id">
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Venue" Type="String"></asp:Parameter>

            <asp:Parameter Name="GroupName" Type="String" />
            <asp:Parameter Name="Technology" Type="String" />
            <asp:Parameter Name="Frequency" Type="String" />
            <asp:Parameter Name="Power" Type="String" />
            <asp:Parameter Name="Bandwidth" Type="String" />
            <asp:Parameter Name="Channel" Type="String" />
            <asp:Parameter Name="Notes" Type="String" />
            <asp:Parameter Name="Active" Type="Boolean" />
        </InsertParameters>
        <SelectParameters>
            <asp:QueryStringParameter Name="Id" QueryStringField="Id" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Venue" Type="String" />
            <asp:Parameter Name="GroupName" Type="String" />
            <asp:Parameter Name="Technology" Type="String" />
            <asp:Parameter Name="Frequency" Type="String" />
            <asp:Parameter Name="Power" Type="String" />
            <asp:Parameter Name="Bandwidth" Type="String" />
            <asp:Parameter Name="Channel" Type="String" />
            <asp:Parameter Name="Notes" Type="String" />
            <asp:Parameter Name="Active" Type="Boolean" />
            <asp:Parameter Name="Id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="GroupsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:FrequencyTrackerConnectionString1 %>" SelectCommand="SELECT [Name] FROM [GroupList] ORDER BY [SortOrder]"></asp:SqlDataSource>
</asp:Content>

After pressing insert. it should go into the database as Frequency: 900MHz, Power: 50mW, etc...

1

2 Answers 2

1

Can you try this on code behind?

protected void  DetailsView1_ItemCommand(object sender, System.Web.UI.WebControls.DetailsViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Update") { 
        DropDownList yourddl = DetailsView1.FindControl("yourdropdownlist"); 
        if (yourddl.SelectedIndex > 0) { 
            var tbox = DetailsView1.FindControl("yourtextbox");
            tbox.text = Now().ToString; 
        } 
    } 
} 
1
  • Thanks @Suni! This is the exact idea. I tested it but there are some changes I had to make because there were cast errors. In order to fix these commends add a typecast before the DetailsView1 call. Such as DropDownList yourddl = (DropDownList)DetailsView1.FindControl("yourdropdownlist");. For the community, I posted my full solution below. Nov 18, 2014 at 16:23
0

Taking from @Suni's answer I was getting the following Error:

Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.DropDownList'

To solve the error, I type casted the DetailsView1 for the DropDownList and Textbox respectively. Here is the full solution:

protected void DetailsView1_ItemCommand(object sender, System.Web.UI.WebControls.DetailsViewCommandEventArgs e)
{
    if (e.CommandName == "Update" || e.CommandName == "Insert")
    {
        //Frequency
        DropDownList FreqDDL = (DropDownList)DetailsView1.FindControl("DropDownList4");
        if (FreqDDL.SelectedIndex > 0)
        {
            var FreqTbox = (TextBox)DetailsView1.FindControl("TextBox4");
            FreqTbox.Text = FreqTbox.Text + FreqDDL.Text;
        }
        //Power
        DropDownList PowerDDL = (DropDownList)DetailsView1.FindControl("DropDownList5");
        if (PowerDDL.SelectedIndex > 0)
        {
            var PowerTbox = (TextBox)DetailsView1.FindControl("TextBox5");
            PowerTbox.Text = PowerTbox.Text + PowerDDL.Text;
        }

        //Bandwidth
        DropDownList BwidthDDL = (DropDownList)DetailsView1.FindControl("DropDownList6");
        if (BwidthDDL.SelectedIndex > 0)
        {
            var BwidthTbox = (TextBox)DetailsView1.FindControl("TextBox6");
            BwidthTbox.Text = BwidthTbox.Text + BwidthDDL.Text;
        }
    }
} 

The result is as expected. Typing in '900' in the Frequency Textbox and selecting 'MHz' in the Frequency DropDownList results in '900MHz' in the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.