【问题标题】:How to get values from multiple DropDownLists in a GridView on button_click?如何从button_click上的GridView中的多个DropDownLists获取值?
【发布时间】:2014-06-04 16:38:15
【问题描述】:

我的 asp.net 网络表单上有一个GridView,其中所有列和行都是用 C# 代码从后面创建的。除第一列外,每列中的每个单元格都是DropDownListDataSource

这是我的GridView

 <asp:GridView ID="gv_Rota" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_Rota_RowDataBound">
                     <HeaderStyle BackColor="#6a3d98" ForeColor="White" Height="20" />
                     <RowStyle HorizontalAlign="Center" Height="20px" Width="100px" />
                     <AlternatingRowStyle Height="20px" />
                 </asp:GridView>

以及列和单元格的c#创建:

 private void BindGrid(int Amount)
    {
        gv_Rota.DataSource = null;
        gv_Rota.Columns.Clear();

        BoundField bfield = new BoundField();
        bfield.HeaderText = "Days";
        bfield.DataField = "Days";
        gv_Rota.Columns.Add(bfield);

        for (int i = 0; i < Amount; i++)
        {
            int week = i + 1;
            string sWeek = "Week " + week.ToString();

            TemplateField tfield = new TemplateField();
            tfield.HeaderText = sWeek;
            gv_Rota.Columns.Add(tfield);
        }

        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Days", typeof(string)));
        dt.Rows.Add("M");
        dt.Rows.Add("T");
        dt.Rows.Add("W");
        dt.Rows.Add("T");
        dt.Rows.Add("F");
        dt.Rows.Add("S");
        dt.Rows.Add("S");
        gv_Rota.DataSource = dt;
        gv_Rota.DataBind();
    }

然后在我的GridViewRowDataBound 触发器上,我将DropDownList 控件添加到每个单元格:

protected void gv_Rota_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 1; i <= ColumnCount; i++)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ddlShift = new DropDownList();
                ddlShift.ID = "ddlShift";
                ddlShift.DataSource = DCListOfShifts;
                ddlShift.DataValueField = "SHIFT_ID";
                ddlShift.DataTextField = "SHIFT_NAME";
                ddlShift.DataBind();
                ddlShift.Items.Insert(0, new ListItem("Shift..."));
                ddlShift.CssClass = "ddl_rotamanager";
                e.Row.Cells[i].Controls.Add(ddlShift);
            }
        }  
    }

例如在我的页面上显示的内容:

所以我的问题和问题是我现在想将每一列(第一列除外)保存到数据库中,作为“预定周”,并且不知道如何获取所有或任何中选定项目的值DropDownLists 然后将其传递回数据库。 请记住,这不是在 selectedindexchanged 上完成的。我将从所有DropDownLists 中选择项目,然后按一个按钮提交值。 请有人给我一些关于如何完成的重点吗?谢谢。

编辑 1: 响应解释了我如何在一行或单个单元格中获取 DropDownList 的每个值。但是,现在我需要更具体地了解如何在 而不是行中获取特定单元格的值。是否有可能进一步强调这一点?

【问题讨论】:

    标签: c# asp.net gridview drop-down-menu webforms


    【解决方案1】:

    使用Gridview.Rows 属性

    protected void MyButton_Click(object sender, EventArgs e)
    {
        foreach(GridViewRow row in gv_Rota.Rows)
        {
           //find this control in this row
           DropDownList dd = row.FindControl("MyDropdown") as DropDownList;
    
           //OR
           //find this control in a specific cell
           DropDownList day = row.Cells[0].FindControl("MyDropdown") as DropDownList;
           DropDownList Week1 = row.Cells[1].FindControl("ddShift") as DropDownList;
    
           //this is just a pseudo-code to determine which dropdown was selected per row. 
           //You can improve on this
           if(dd1.SelectedValue != string.Empty)
             thisDay.SelectedWeek = dd1.SelectedValue;
           else if(dd2.SelectedValue != string.Empty)
             thisDay.SelectedWeek = dd2.SelectedValue;
           ....
        }
    }
    

    【讨论】:

    • 感谢您的回复。因此,使用第一种方法,我可能会获得每一行中每个单元格的值,而第二种方法可以让我获得行中的特定单元格,据我所知?但是,我特别需要 column 而不是一行中的所有 7 个单元格,因为我需要能够获得一周中每一天的选定值。日为行,周为列。这可能吗?如果可以,怎么做? (已编辑的问题)。
    • 我相信你可以改进这段代码来做到这一点。此循环应运行 7 次(天数),然后对于每一行,使用获取单元格的第二个示例检查单元格 1、2、3 和 4,以获取下拉菜单并检查是否选中
    • 你的意思是每行只能选择一个吗?用户可以选择多个(有限制)吗?
    • 我想我可以从中工作并尝试让它工作。 有关更多信息: 我需要每一行的所有选择,但需要确定该单元格所在行的哪一列,因为显然我需要知道该班次是哪一周。将列视为周,将行视为天。所以要将事件保存到我的数据库中。我首先会在第一列中获取第一个下拉列表单元格以获取 WEEK 1 - MONDAY。
    • 这将是第二行。您可以有一个计数器将每一天表示为 1,2,3,...7。第 1 周的星期二是 row2.Cells[1].FindControl("ddlShift")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多