将gridview嵌套的dropdown.selectedValue添加到数据库

时间:2013-04-18 09:15:08

标签: asp.net gridview

我有两个与此问题相关的表格......

tblCellTechnology
  CellTechnologyID, PK
  Description
  CellName

tblActionSchedule
  ActionID, PK
  ActionPhase
  ActionPeriod
  CellTechnology, FK

我有一个批量编辑网格视图,其中的文本框显示ActionID,Phase,Period和直接链接到tblCellTechnology的CellTechnologyID的下拉列表(根据图像)。我还有另一个文本框,根据tblScheduleAction.CellTechnologyID中的值显示单元格名称(ActionDescription是从另一个表中提取的)。

如何从ddl中获取所选值并将其写入tblScheduleAction.CellTechnologyID?当我将CellTechnologyID硬添加到tblScheduleActionID时,cellname文本框会显示正确的值。我只需要能够将ddl.SelectedValue存储到tblScheduleAction.CellTechnologyID。我正在使用masterpages和sqldatasources来处理记录的插入/更新/删除。

我希望底部gridview中的celltechnologyid列显示潜在的celltechnologyid列表,因为它从顶部网格视图直接链接到该列,所选值也应存储在tblScheduleAction.CellTechnologyID中。 cellname列应显示相应的cellname。 正如他们所说,一张图片胜过千言万语...... http://imgur.com/21FjOzh

1 个答案:

答案 0 :(得分:0)

通过嵌套的网格视图,我假设您在每个父网格视图行中引用子网格视图。获取子gridview控件的值非常简单。

  

如何从ddl中获取所选值并将其写入tblScheduleAction.CellTechnologyID?

  1. 您使用FindControl查找保存记录中的foreach循环中的childgridview
  2. 然后,您将使用查找控件来查找下拉列表&保存到您的数据库
  3. 检查工作示例(Aspx)

        <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NestedGridviews.aspx.cs" Inherits="WebApplication1.NestedGridviews" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    
            <asp:GridView ID="gvParent" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="CategoryID" DataSourceID="sdsParentGrid" 
                onrowdatabound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                        InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
                    <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" 
                        SortExpression="CategoryName" />
                    <asp:BoundField DataField="Description" HeaderText="Description" 
                        SortExpression="Description" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="False" 
                                DataKeyNames="ProductID" DataSourceID="sdsChildGrid">
                                <Columns>
                                    <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                                        InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
                                    <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                                        SortExpression="ProductName" />
                                    <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" 
                                        SortExpression="SupplierID" />
                                    <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                                        SortExpression="CategoryID" />
                                    <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" 
                                        SortExpression="Discontinued" />
                                    <asp:TemplateField HeaderText="My DropDown Column">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="ddlTest" runat="server">
                                                <asp:ListItem>Potatoes</asp:ListItem>
                                                <asp:ListItem>Tomatoes</asp:ListItem>
                                                <asp:ListItem>Mangoes</asp:ListItem>
                                            </asp:DropDownList>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
                            <asp:SqlDataSource ID="sdsChildGrid" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
                                SelectCommand="SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [Discontinued] FROM [Alphabetical list of products] WHERE ([CategoryID] = @CategoryID)">
                                <SelectParameters>
                                    <asp:Parameter Name="CategoryID" Type="Int32" />
                                </SelectParameters>
                            </asp:SqlDataSource>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="sdsParentGrid" runat="server" 
                ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
                SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories] ORDER BY [CategoryName]">
            </asp:SqlDataSource>
    
        </div>
        <asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" 
            Text="Save to Database" />
        </form>
    </body>
    </html>
    

    代码隐藏

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication1
    {
        public partial class NestedGridviews : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    GridView gvChild = e.Row.FindControl("gvChildGrid") as GridView;
                    SqlDataSource sdsTemp = e.Row.FindControl("sdsChildGrid") as SqlDataSource;
                    if (sdsTemp!=null)
                    {
                        sdsTemp.SelectParameters[0].DefaultValue = gvParent.DataKeys[e.Row.RowIndex]["CategoryID"].ToString();
                        if (gvChild != null)
                        {
                            gvChild.DataBind();
                        }
                    }
                }
            }
    
            protected void btnSave_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow growParent in gvParent.Rows)
                {
                    // Find child gridview for each row
                    GridView gvChild = growParent.FindControl("gvChildGrid") as GridView;
                    if (gvChild != null)
                    {
                        // use a foreach loop to find your control
                        foreach (GridViewRow growChild in gvChild.Rows)
                        {
                            DropDownList ddlTemp = growChild.FindControl("ddlTest") as DropDownList;
                            if (ddlTemp != null)
                            {
                                Response.Write(ddlTemp.SelectedValue.ToString());
                            }
                        }
                    }
                }
            }
        }
    }
    

    enter image description here