2
Answers

how to select values frm gridview when checkbox is checked

I'm trying to send mails to selected users by selecting check box in grid view...
while debugging its not taking checkbox.checked=true....
kindly share your view and point out the mistake in codings...
 
source :
 
<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<table>
<tr>
<td>
subject
</td>
<td>
<asp:TextBox ID="txtsubject" runat="server"></asp:TextBox>
</td> </tr>
<tr>
<td>message</td>
<td>
<asp:TextBox ID="txtmessage" runat="server" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
</table>
</div>
<div>
</div>
<div>
</div>
<div>
<asp:Button ID="btnsubmit" runat="server" Text="Send" onclick="btnsubmit_Click" />
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="sno">
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%#bind("sno") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="lbl2" runat="server" Text='<%#bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="mail">
<ItemTemplate>
<asp:Label ID="lbl3" runat="server" Text='<%#bind("mailid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkhdr" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
aspx.cs file...................................................................................
 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
Class1 obj = new Class1();
public string host, frommail, password;
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = obj.display();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
MailMessage s = new MailMessage();
s.From = new MailAddress("[email protected]");
s.Subject = txtsubject.Text;
s.Body = txtmessage.Text;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)row.FindControl("CheckBox1");
{
if (chk.Checked==true)
{
string email = row.Cells[2].Text;
s.To.Add(email);
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential p = new NetworkCredential();
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "******");
smtp.EnableSsl = true;
smtp.Send(s);
}
}
}
}
}
}
 
 
 
class file...........................................................................
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Net;
public class Class1
{
string s1 = ConfigurationManager.ConnectionStrings["pri"].ToString();
SqlCommand cmd = new SqlCommand();
public DataSet display()
{
using (SqlConnection con = new SqlConnection(s1))
{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_display";
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
 
 
Answers (2)