簡體   English   中英

如何從button_click的GridView中的多個DropDownList中獲取值?

[英]How to get values from multiple DropDownLists in a GridView on button_click?

我在asp.net網絡表單上有一個GridView ,所有列和行都是從后面用C#代碼創建的。 除了第一列之外,每列中的每個單元格都是一個具有DataSourceDropDownList

這是我的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的每個值。 但是,現在我需要更具體地了解如何獲取而不是行中特定單元格的值。 是否有可能對此進一步強調?

使用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;
       ....
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM