如何将详细信息视图中的下拉列表绑定到不同的表

时间:2015-11-06 21:13:35

标签: c# sql asp.net detailsview

我是asp.net的新手,我需要一些帮助。 我使用的是c#和sql。

我想做的是以下内容: 我有一个详细信息视图,我可以使用它来添加Employees并将其保存到Employee表中的数据库中。在详细信息视图中,我为dept_id创建了一个模板字段(该字段是外键)。我想将其作为下拉列表,您可以在其中选择部门名称(在Department表中),而不是输入部门ID。 我非常坚持这一点。任何帮助将不胜感激。

这是.aspx

    <asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="False" 
                CellPadding="4" DataKeyNames="Emp_Id" DataSourceID="SqlDataSource3" 
                DefaultMode="Insert" ForeColor="#333333" GridLines="None" Height="50px" 
                Width="149px">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
                <EditRowStyle BackColor="#999999" />
                <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
                <Fields>
                    <asp:BoundField DataField="Emp_Id" HeaderText="Emp_Id" InsertVisible="False" 
                        ReadOnly="True" SortExpression="Emp_Id" />
                    <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                        SortExpression="FirstName" />
                    <asp:BoundField DataField="LastName" HeaderText="LastName" 
                        SortExpression="LastName" />
                    <asp:BoundField DataField="EndUser" HeaderText="EndUser" 
                        SortExpression="EndUser" />



                    <asp:TemplateField HeaderText="Dept_id" SortExpression="Dept_id">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="ddlSqlDataSource"
                            DataTextField="dept_name" DataValueField="dept_id" SelectedValue='<%# Bind("dept_id") %>'>
                            </asp:DropDownList>
                        </EditItemTemplate>


                        <InsertItemTemplate>
                            <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="ddlSqlDataSource"
                            DataTextField="dept_name" DataValueField="dept_id" SelectedValue='<%# Bind("dept_id") %>'>
                            </asp:DropDownList>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label5" runat="server" Text='<%# Bind("Dept_id") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>



                    <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                    <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />




                    <asp:TemplateField HeaderText="JoinDate" SortExpression="JoinDate">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("JoinDate") %>' TextMode="Date"></asp:TextBox>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("JoinDate") %>' TextMode="Date"></asp:TextBox>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("JoinDate") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ButtonType="Button" ShowInsertButton="True" />

                </Fields>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            </asp:DetailsView>

我不知道应该向(ddlSqlDataSource)添加什么

<asp:SqlDataSource ID="ddlSqlDataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                SelectCommand="SELECT [dept_name] FROM [Department]">
            </asp:SqlDataSource>

2 个答案:

答案 0 :(得分:0)

我解决了问题

的.aspx

<asp:TemplateField HeaderText="Dept_id" SortExpression="Dept_id">
                      <ItemTemplate>
                      <%#Eval("dept_id") %>
                      </ItemTemplate>

                       <InsertItemTemplate>
                           <asp:DropDownList ID="ddlDept" runat="server" DataSourceID="ddlSqlDataSource"
                           DataTextField="dept_name" DataValueField="dept_id" SelectedValue='<%# Bind("Dept_id") %>'>
                           </asp:DropDownList>
                            <asp:SqlDataSource ID="ddlSqlDataSource" runat="server"
              ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

               SelectCommand="SELECT dept_id, dept_name FROM Department ">

           </asp:SqlDataSource>
                       </InsertItemTemplate>
                       <ItemTemplate>
                           <asp:Label ID="Label5" runat="server" Text='<%# Bind("Dept_id") %>'></asp:Label>
                       </ItemTemplate>
                   </asp:TemplateField>

代码背后:

protected void DetailsView2_DataBound(object sender, EventArgs e)
    {
        DropDownList ddlDept = (DropDownList)DetailsView2.FindControl("ddlDept");

        ddlDept.Items.Insert(0, new ListItem("Select department"));

    }

答案 1 :(得分:0)

我在后面的代码中解决了数据绑定处理程序中的类似问题:

protected void OrgsDetailView1_DataBound(object sender, EventArgs e)
{
    if (OrgsDetailsView1.CurrentMode == DetailsViewMode.Edit)
    {
        var drpCategory = OrgsDetailsView1.FindControl("drpCategory") as DropDownList;
        if (drpCategory != null) 
        {
            //drops to here when in edit mode and dropdownList is available.
            //next 3 lines just get the dataTable (catsdt)
            var cats = new Categories();  //class that gets dataTable
            var prm = new ParmsClass();  //parameter for stored procedure
            var catsdt = cats.GetAll(prm);  //gets the dataTable

            // load the dropdownlist
            drpCategory.DataSource = catsdt;
            drpCategory.DataTextField = "name";
            drpCategory.DataValueField = "Id";
            drpCategory.DataBind();
        }
相关问题