简体   繁体   English

如何将DetailsView中的下拉列表绑定到其他表

[英]How to bind dropdownlist in detailsview to different table

I'm new to asp.net and I need some help, please. 我是asp.net的新手,请帮忙。 I'm using c# and sql. 我正在使用c#和sql。

What I want to do is the following: I have a details view where I can use it to add Employees and save it to my database in Employee table. 我要执行的操作如下:我有一个详细信息视图,可以在其中使用它添加雇员并将其保存到雇员表中的数据库中。 Within the details view, I'v created a template field for dept_id (this field is a foreign key). 在详细信息视图中,我为dept_id创建了一个模板字段(此字段是外键)。 I want to make it as a drop down list where you can choose the department name (which is in the Department table) instead of entering the department id. 我想将其作为一个下拉列表,您可以在其中选择部门名称(位于“部门”表中),而不用输入部门ID。 I'm very stick in this. 我对此非常坚持。 Any help will be appreciated. 任何帮助将不胜感激。

This is the .aspx 这是.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>

I don't know what should I add exactly to the (ddlSqlDataSource) 我不知道我到底该添加什么到(ddlSqlDataSource)

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

I have Solved the problem 我已经解决了问题

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

Code behind: 后面的代码:

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

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

    }

I solved my similar problem in the databound handler in code behind: 我在后面的代码中的数据绑定处理程序中解决了类似的问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM