选中复选框时如何选择frm gridview的值 [英] how to select values frm gridview when checkbox is checked

查看:96
本文介绍了选中复选框时如何选择frm gridview的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过选中网格视图中的复选框将邮件发送给选定的用户... 
,同时调试它不接受checkbox.checked = true ....
请分享您的观点并指出编码中的错误...

来源:

<%@ Page 语言 = C# 调试 = true AutoEventWireup = < span class =code-keyword> true CodeFile = Default.aspx.cs 继承 = _默认 %> ;
< !DOCTYPE html PUBLIC < span class =code-attribute> - // W3C // DTD < span class =code-attribute> 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 = 服务器 >
< title > < / title >
< / head >
< span class =code-key单词>< body >
< 表格 id = form1 runat = server >
< div >
< table < span class =code-keyword>>
< tr >
< td >
subject
< / td >
< td >
< asp:TextBox ID = txtsubject runat < span class =code-keyword> = server > < / asp:TextBox >
< / td > <跨越ss =code-keyword>< / tr >
< tr >
< td > 消息< span class =code-keyword>< / td >
< td >
< asp:TextBox < span class =code-attribute> ID = txtmessage runat = 服务器 TextMode = MultiLine > < < span class =code-leadattribute> / asp:TextBox >
< / td >
< ; / tr >
< / table >
< / div >
< div >
< / div >
< div >
< / div >
< div >
< span class =code-keyword>< asp:按钮 ID = btnsubmit runat = server 文字 = <跨班=code-keyword>发送 onclick = btnsubmit_Click / >
< / div >
< asp:GridView ID = GridView1 runat = server AutoGenerateColumns = False >
< >
< asp :TemplateField HeaderText = sno >
< ItemTemplate >
< asp:标签 ID = lbl1 runat = server < span class =code-attribute>
文本 =' <% #bind( sno %> ' > < / asp:标签 >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 名称 >
< ItemTemplate >
< asp:Label ID = lbl2 runat = server 文字 =' < ;% #bind( name%> ' > < / asp:标签 >
< / ItemTemplate >
< < span class =code-leadattribute> / asp:TemplateField >
< asp:TemplateField HeaderText < span class =code-keyword> = mail >
< ItemTemplate >
< asp:标签 < span class =code-attribute> ID = lbl3 runat = server 文字 < span class =code-keyword> =' <% #bind( mailid%> ' < span class =code-keyword>> < / asp:Label < span class =code-keyword>>
< / ItemTemplate >
< / asp: TemplateField >
< asp:TemplateField >
< HeaderTemplate >
< asp:CheckBox ID = chkhdr runat = server / >
< / HeaderTemplate >
< ItemTemplate >
< asp:CheckBox ID = CheckBox1 runat = server / >
< / ItemTemplate >
< / asp:TemplateField >
< /列 >
< / asp:GridView >
< / form >
< / body >
< / html >
aspx.cs文件................................ .................................................. 。



使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Configuration;
使用System.Data;
使用System.Data.SqlClient;
使用System.Net.Mail;
使用System.Net;
public partial class _Default:System.Web.UI.Page
{
Class1 obj = new Class1();
公共字符串主机,frommail,密码;
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(GridView1.Rows中的GridViewRow行)
{
if(row.RowType == DataControlRowType.DataRow)
{
CheckBox chk =(CheckBox)行。的FindControl( CheckBox1);
{
if(chk.Checked == true)
{
string email = row.Cells [2] .Text;
s.To.Add(电子邮件);
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);
}
}
}
}
}
}



类文件.. .................................................. .......................


使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Configuration;
使用System.Data;
使用System.Data.SqlClient;
使用System.Net.Mail;
使用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);
返回ds;
}
}

解决方案

Hai



请使用更新面板进行网格视图和按钮单击,bcz页面加载你只需在gridview中加载数据,当你检查并点击按钮时,它再次重新启动页面,我的意思是页面锄头服务器所以再次出现页面加载事件,在页面加载事件你将编写gridview绑定,所以它再次绑定网格视图中的数据。



如果你使用gridview和button的更新面板,然后页面部分回发,我的意思是它不会完全重定向,所以gridview不再绑定。



 <   ajax:UpdatePanel     ID   =  UpdatePanel      runat   =  server    UpdateMode   = 条件 >  
< ContentTemplate >
< div >
< span class =code-keyword>< asp:按钮 ID = btnsubmit runat = server 文字 = 发送 onclick = btnsubmit_Click / >
< / div >
< asp:GridView ID = GridView 可见 = false runat = server HeaderStyle-Width = 200 HeaderStyle-BackColor = #2B6292 HeaderStyle-ForeColor = 白色

AllowSorting = true AllowPaging = true 宽度 = 600 AutoGenerateColumns = 错误 OnRowCreated = GridView_OnRowCreated

Dat aKeyNames = Id onsorting = GridView_OnSort >
< < span class =code-keyword>>
...
< /列 >
< / asp:GridView >
< / ContentTemplate >
< 触发器 >
< ajax:AsyncPostBackTrigger ControlID = btnsubmit / >
< /触发器 >
< / ajax:UpdatePanel >



赞这个尝试...


嗨......

<前lang =cs> 字符串 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 ))。文本;
// lblmail是您绑定mailID的标签的ID。
// 此处邮件已准备好您的mailid。在这里进行操作。
}
}



谢谢你


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;
}
}

解决方案

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...


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


这篇关于选中复选框时如何选择frm gridview的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆