先看一代码:
foreach(DataGridItem dgi in this.dg1.Items)//datagrid名为dg1
{
CheckBox cb4=new CheckBox();//说明:cb4是datagrid(现命名为dg1)模板列上的一个Checkbox
cb4=(CheckBox)dgi.Cells[0].FindControl("cb4");
if(cb4.Checked==true)
{
string sql="insert into Notice(N_from,N_to,N_time,Notice) values('rr','ggg','"+DateTime.Now.ToString()+"','"+content.Text.Replace("'","''")+"')";
System.Data.SqlClient.SqlCommand cmd=new SqlCommand(sql,cn);
cmd.ExecuteNonQuery();
}
}------------------------------
当只有下面这些代码时:
string sql="insert into Notice(N_from,N_to,N_time,Notice) values('rr','ggg','"+DateTime.Now.ToString()+"','"+content.Text.Replace("'","''")+"')";
System.Data.SqlClient.SqlCommand cmd=new SqlCommand(sql,cn);
cmd.ExecuteNonQuery();
插入数据库成功
----------------------
但当外加
foreach(DataGridItem dgi in this.dg1.Items)
{CheckBox cb4=new CheckBox();
cb4=(CheckBox)dgi.Cells[0].FindControl("cb4");
if(cb4.Checked==true)
时,就没执行insert语句,项目急,sos!!郁闷中、、、、、、、、、、、、、、、、、
foreach(DataGridItem dgi in this.dg1.Items)//datagrid名为dg1
{
CheckBox cb4=new CheckBox();//说明:cb4是datagrid(现命名为dg1)模板列上的一个Checkbox
cb4=(CheckBox)dgi.Cells[0].FindControl("cb4");
if(cb4.Checked==true)
{
string sql="insert into Notice(N_from,N_to,N_time,Notice) values('rr','ggg','"+DateTime.Now.ToString()+"','"+content.Text.Replace("'","''")+"')";
System.Data.SqlClient.SqlCommand cmd=new SqlCommand(sql,cn);
cmd.ExecuteNonQuery();
}
}------------------------------
当只有下面这些代码时:
string sql="insert into Notice(N_from,N_to,N_time,Notice) values('rr','ggg','"+DateTime.Now.ToString()+"','"+content.Text.Replace("'","''")+"')";
System.Data.SqlClient.SqlCommand cmd=new SqlCommand(sql,cn);
cmd.ExecuteNonQuery();
插入数据库成功
----------------------
但当外加
foreach(DataGridItem dgi in this.dg1.Items)
{CheckBox cb4=new CheckBox();
cb4=(CheckBox)dgi.Cells[0].FindControl("cb4");
if(cb4.Checked==true)
时,就没执行insert语句,项目急,sos!!郁闷中、、、、、、、、、、、、、、、、、
解决方案 »
- 多个datatable写入多个的excel。 一键导出!
- gridview的问题???????
- 大家帮我实现一个功能啊。可能对你们来说是很简单的。。我菜啊。。
- vs2003与vs2005
- 我应聘的是DELPHI程序员,新公司却要求我用B/S结构开发一个非常小的项目,做的好才肯聘我,大家帮忙给点建议
- 为什么一个页面做完后会出现这样的提示?
- 在datagrid有个密码列,如何显示的是*号和不是密码呢?
- 关于页面间数据库连接共享的问题
- ”应用程序错误“是什么原因造成的,如何解决?
- 服务器IIS的问题.求帮助求帮助
- 怎么根据DateTime.Now生成一个字符串作为文件名,比如如果当前时间是2005年8月1日,15点30分56秒,则返回
- textbox 自动换行的问题
{
//...初始绑定
}里
好像就没执行到
public class putnotice : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg;
protected System.Web.UI.WebControls.TextBox content;
protected System.Web.UI.WebControls.Button sendmessage;
protected System.Web.UI.HtmlControls.HtmlTableCell TD1;
protected System.Web.UI.WebControls.DataGrid dg1;
System.Data.SqlClient.SqlConnection cn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectString"]);
public void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string a=Session["usernumber"].ToString();
string b=Session["username"].ToString();
string c=Session["Popedom"].ToString();
BindGrid1();
BindGrid2();
if(c=="4")
{
validation("对不起,只有有权限的人才可以给别人发送消息!");
}
if(c=="3"||c=="1")
{
dg.Visible=false;
dg1.Visible=true; }
if(c=="2")
{
dg.Visible=true;
dg1.Visible=false;
}
}
void validation(string e)
{
Response.Write("<script>top.location.href='javascript:history.go(-1);'alert('"+e+"')</script>");
}
void success(string e)
{
Response.Write("<script>top.location.href='index.aspx';alert('"+e+"')</script>");
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dg1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dg1_PageIndexChanged);
this.sendmessage.Click += new System.EventHandler(this.sendmessage_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void sendmessage_Click(object sender, System.EventArgs e)
{
string c=Session["Popedom"].ToString();
string a=Session["usernumber"].ToString();
string b=Session["username"].ToString();
cn.Open();
if(c=="2")
{
foreach(DataGridItem dgi in this.dg.Items)
{
CheckBox cb2=new CheckBox();
cb2=(CheckBox)dgi.Cells[0].FindControl("cb2");
if(cb2.Checked==true)
{
string strSql="insert into Notice(N_from,N_to,N_time,Notice) values('"+b+"','"+dgi.Cells[2].Text+"','"+DateTime.Now.ToString()+"','"+content.Text.Replace("'","''")+"')";
System.Data.SqlClient.SqlCommand cmd=new SqlCommand(strSql,cn);
cmd.ExecuteNonQuery();
}
}
}
else if(c=="3")
{
foreach(DataGridItem dgi in this.dg1.Items)
{
if (!Page.IsPostBack)
{
CheckBox cb4=new CheckBox();
cb4=(CheckBox)dgi.Cells[0].FindControl("cb4");
if(cb4.Checked==true)
{
string sql="insert into Notice(N_from,N_to,N_time,Notice) values('rr','ggg','"+DateTime.Now.ToString()+"','"+content.Text.Replace("'","''")+"')";
System.Data.SqlClient.SqlCommand cmd=new SqlCommand(sql,cn);
cmd.ExecuteNonQuery();
}
} }
}
success("您的消息已经发送成功!!");
cn.Close();
} public void SelectAll(object sender, System.EventArgs e)
{
foreach(DataGridItem dgi in this.dg.Items)
{
CheckBox cb2=new CheckBox();
cb2=(CheckBox)dgi.Cells[0].FindControl("cb2");
cb2.Checked=((CheckBox)sender).Checked;
}
}
public void SelectAll2(object sender, System.EventArgs e)
{
foreach(DataGridItem dgi in this.dg.Items)
{
CheckBox cb4=new CheckBox();
cb4=(CheckBox)dgi.Cells[0].FindControl("cb4");
cb4.Checked=((CheckBox)sender).Checked;
}
}
private void BindGrid1()
{
string sql="select M_no,M_name from vw_iteminfo";
System.Data.SqlClient.SqlDataAdapter da=new SqlDataAdapter(sql,cn);
DataSet ds=new DataSet();
da.Fill(ds);
dg.DataSource=ds;
dg.DataBind(); }
private void BindGrid2()
{
string sql="select M_no,M_name from Member";
System.Data.SqlClient.SqlDataAdapter da=new SqlDataAdapter(sql,cn);
DataSet ds=new DataSet();
da.Fill(ds);
dg1.DataSource=ds;
dg1.DataBind();
} private void dg1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dg1.CurrentPageIndex=e.NewPageIndex;
BindGrid2();
}
是不是你的item循环了,但是你写入的值没有进行对应的循环赋值?
------------------------------------------------------------
循环后一个也没能插入,如果没有下面这些:
foreach(DataGridItem dgi in this.dg1.Items)
{CheckBox cb4=new CheckBox();
cb4=(CheckBox)dgi.Cells[0].FindControl("cb4");
if(cb4.Checked==true)
单纯的插入是完全可以的
{if (!Page.IsPostBack)
{
这里来
}
』
{if (!Page.IsPostBack)
{
这里来
}
』
-------------------------------------
我还得要循环insert的,放到Page_Load里,那么就没法在触发事件时,循环insert数据到数据库
{if (!Page.IsPostBack)
{
这里来
}
』--------------------------
照做了,没有解决问题
{
CheckBox cb2=new CheckBox();
cb2=(CheckBox)dgi.Cells[0].FindControl("cb2");
if(cb2.Checked==true)
{
string strSql="insert into Notice(N_from,N_to,N_time,Notice) values('"+b+"','"+dgi.Cells[2].Text+"','"+DateTime.Now.ToString()+"','"+content.Text.Replace("'","''")+"')";
System.Data.SqlClient.SqlCommand cmd=new SqlCommand(strSql,cn);
cmd.ExecuteNonQuery();
}
}用Response.Write代替插入语句看看,是循环的问题,还是其他问题。
public void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string a=Session["usernumber"].ToString();
string b=Session["username"].ToString();
string c=Session["Popedom"].ToString();
BindGrid1();
BindGrid2();
if(c=="4")
{
validation("对不起,只有有权限的人才可以给别人发送消息!");
}
if(c=="3"||c=="1")
{
dg.Visible=false;
dg1.Visible=true; }
if(c=="2")
{
dg.Visible=true;
dg1.Visible=false;
}
}
改为
public void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostback)
{
// 在此处放置用户代码以初始化页面
string a=Session["usernumber"].ToString();
string b=Session["username"].ToString();
string c=Session["Popedom"].ToString();
BindGrid1();
BindGrid2();
if(c=="4")
{
validation("对不起,只有有权限的人才可以给别人发送消息!");
}
if(c=="3"||c=="1")
{
dg.Visible=false;
dg1.Visible=true; }
if(c=="2")
{
dg.Visible=true;
dg1.Visible=false;
}
}
}
在.net中isPostBack个示非首次加载,即用户点击页面提交请求到服务器的结果,
而直接在page_load里边加入BindGrid1();BindGrid2();两个绑定函数,就是说每当你点击页面一次那么页面重新加载时就会执行一次绑定,那么请问从新绑定过的数据后你的CB也就重新加载了,它选 了吗???明显没有选 啊,所以不执行了:然次方法,还是在改Page_Load中的东西
奖你原来的改成:
public void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string a=Session["usernumber"].ToString();
string b=Session["username"].ToString();
string c=Session["Popedom"].ToString();
if(!this.Page.IsPostBack)
{
BindGrid1();
BindGrid2();
}
//就这地方,你每次点击就加载DataGrid了!
if(c=="4")
{
validation("对不起,只有有权限的人才可以给别人发送消息!");
}
if(c=="3"||c=="1")
{
dg.Visible=false;
dg1.Visible=true; }
if(c=="2")
{
dg.Visible=true;
dg1.Visible=false;
}
}
yybb520(bbyy)
coley(唉~眼镜又厚了~)
---------------------------
不过新加个问题
如何实现全先功能呢?
public void SelectAll2(object sender, System.EventArgs e)
{
foreach(DataGridItem dgi in this.dg.Items)
{
CheckBox cb4=new CheckBox();
cb4=(CheckBox)dgi.Cells[0].FindControl("cb4");
cb4.Checked=((CheckBox)sender).Checked;
}
}
用这个函数好像不行呀,郁闷又来了
如何实现全先功能呢?
public void SelectAll2(object sender, System.EventArgs e)
{
foreach(DataGridItem dgi in this.dg.Items)
{
CheckBox cb4=new CheckBox();
cb4=(CheckBox)dgi.Cells[0].FindControl("cb4");
cb4.Checked=((CheckBox)sender).Checked;
}
}
用这个函数好像不行呀,郁闷又来了
如果要全选你可以在客户端使用脚本
----------------------------------
但我的所有的checkbox都是作为模板列在datagrid上的,你说的好像不行
请个给例子