我在gridview中添加了一个复选框(chkCtrl)列,当选中复选框时,我想获取与该行对应的电子邮件地址,并将所有结果存储在列表或数组中。我希望在发送电子邮件时发生这种情况。单击按钮。
Gridview Code ASP.net
<asp:GridView CssClass="table table-hover" BorderWidth="2px" ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="GrabModuleID" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >
<Columns>
<asp:CommandField EditText="" ShowSelectButton="True" SelectText="View Student Details" />
<asp:BoundField DataField="StudentNo" HeaderText="Student Number" SortExpression="StudentNo" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" Visible="false" />
<asp:BoundField DataField="ModuleID" HeaderText="ModuleID" SortExpression="ModuleID" Visible="false" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkCtrl" runat="server" />
</ItemTemplate>
</asp:TemplateField>
发送电子邮件按钮代码:
<asp:Button ID="SendEmail" runat="server" class="btn btn-primary" Text="Send Class Email" OnClick="SendEmail_Click" />
答案 0 :(得分:1)
您可以使用foreach
中的Row.Cells [index]访问BoundFieldsforeach (GridViewRow row in GridView1.Rows)
{
Control controlcbx = row.Cells[6].Controls[1]; //control for checkbox
string email = row.Cells[2].Text; // email text
List<string> emailList = new List<string>(); //list to save emails
CheckBox cbx = controlcbx as Checkbox;
if(cbx.Checked)
{
emailList.Add(email);
}
}
验证Cell [index]是否与您的BoundFields匹配
答案 1 :(得分:1)
逻辑非常简单。单击按钮,只需遍历gridview行并找到每行中的复选框。接下来,检查是否选中了该复选框,然后找到电子邮件地址并将其存储在集合中。
protected void SendEmail_Click(object sender, EventArgs e)
{
List<string> emails = new List<string>();
foreach (GridViewRow row in grdCustomer.Rows)
{
CheckBox chkCtrl = row.FindControl("chkCtrl") as CheckBox;
if (chkCustomer.Checked)
{
cities.Add(row.Cells[1].Text);
}
}
}