【问题标题】:How to bind dropdownlist in detailsview to different table如何将详细信息视图中的下拉列表绑定到不同的表
【发布时间】:2016-02-08 03:11:45
【问题描述】:

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

我想要做的是: 我有一个详细信息视图,我可以在其中使用它来添加员工并将其保存到我的 Employee 表中的数据库中。在详细信息视图中,我为 dept_id 创建了一个模板字段(该字段是外键)。我想将其设置为下拉列表,您可以在其中选择部门名称(在部门表中)而不是输入部门 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>

【问题讨论】:

  • 为了让自己更轻松,只需进入页面的设计视图,然后突出显示/单击您尝试为其添加数据源的控件......当您这样做,右上角会出现一个小箭头,它会打开一个菜单,帮助您完成此操作。
  • 首先,感谢您的回复。好吧,在正常情况下,我会这样做,但是我尝试为(下拉列表)添加数据源的控件位于详细信息视图中,并且无法单击。只能点击详情视图@Gavin

标签: c# sql asp.net detailsview


【解决方案1】:

我已经解决了问题

.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"));

    }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多