Click here to Skip to main content
14,695,499 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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("mail@gmail.com");
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("mail@gmail.com", "******");
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;
}
}
Posted

Hai

Pls use update panel for ur grid view and button click,bcz in page load u just load data in gridview,when u check and click button ,it again redired page ,i mean page hoit the server so again page load event occur,in page load event u will write gridview bind ,so again it bind data in grid view.

if u use update panel for gridview and button,then page partial post back,i mean it will not full redirect ,so gridview not bind again.

<ajax:UpdatePanel ID="UpdatePanel"  runat="server" UpdateMode="Conditional">
				<ContentTemplate>
<div>
<asp:Button ID="btnsubmit" runat="server" Text="Send" onclick="btnsubmit_Click" />
</div>
					<asp:GridView ID="GridView" Visible="false" runat="server"  HeaderStyle-Width="200" HeaderStyle-BackColor="#2B6292" HeaderStyle-ForeColor="White" 

					AllowSorting="true" AllowPaging="true" Width="600" AutoGenerateColumns="False" OnRowCreated="GridView_OnRowCreated" 

					DataKeyNames="Id" onsorting="GridView_OnSort">
						<Columns>
							...
						</Columns>
					</asp:GridView>
				</ContentTemplate>
				<Triggers>
					<ajax:AsyncPostBackTrigger ControlID="btnsubmit"/>
				</Triggers>
			</ajax:UpdatePanel>

Like this try...
   
v4
Hi...
string mail=stirng.Empty;
foreach (GridViewRow gdrw in grid1.Rows)
      {
          CheckBox chbx = (CheckBox)gdrw.FindControl("Checkboxid");
          if (chbx != null &amp;&amp; chbx.Checked)
          {
              mail = ((Label)gdrw.FindControl("lblmail")).Text;
              //lblmail is ID of the label for which you are binding mailID.
              // here mail is ready with your mailid. Do the operation here.
          }
      }

Thank u
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900