[英]How to get values from multiple DropDownLists in a GridView on button_click?
I have a GridView
on my asp.net webform with all the columns and rows created in C# code from behind. 我在asp.net网络表单上有一个GridView
,所有列和行都是从后面用C#代码创建的。 Except the first column, every cell in each column is a DropDownList
with a DataSource
. 除了第一列之外,每列中的每个单元格都是一个具有DataSource
的DropDownList
。
Here is my GridView
: 这是我的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>
And the c# creation of the columns and cells: 以及列和单元格的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();
}
And then on the RowDataBound
trigger of my GridView
, I add the DropDownList
control to every cell: 然后在GridView
的RowDataBound
触发器上,将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);
}
}
}
What is displayed for example on my page: 例如在我的页面上显示的内容:
So my question and problem is that I now want to save each column (except the first) to a database, as a "scheduled week", and have no idea how to get the values of selected items in all or any of the DropDownLists
to then pass that back to the database. 所以我的问题是,我现在想将每列(第一列除外)保存到数据库中,作为“预定的一周”,并且不知道如何将所有或任何DropDownLists
的选定项目的值然后将其传递回数据库。 Baring in mind, this is NOT done on selectedindexchanged
. 切记,这不是对selectedindexchanged
完成的。 I will select items from all the DropDownLists
and then press a button to submit the values. 我将从所有DropDownLists
选择项目,然后按一个按钮提交值。 Please could someone give me some highlight on how this could be done? 请有人给我一些如何完成的重点吗? Thanks. 谢谢。
EDIT 1: Response explains how I could get every value of a DropDownList in a row or by individual cell. 编辑1:响应说明了如何获取行或单个单元格中DropDownList的每个值。 However, now I need to know more specifically how I can get values of specific cells in the columns rather than the rows. 但是,现在我需要更具体地了解如何获取列而不是行中特定单元格的值。 Is it possible to get further highlight on this? 是否有可能对此进一步强调?
Use Gridview.Rows
property 使用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.