用在源代码区里给gridview1绑定了一个名字叫做 "CBox"的checkbox类型的控件
以下是代码
<asp:GridView ID="GridView1" runat="server" AllowPaging ="True" AutoGenerateColumns ="False" Font-Size="10pt"
HorizontalAlign ="Center" Width="780px" ShowHeader ="False" borderwidth="0px">
<Columns >
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CBox" runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
此时可以正确现实出这个checkbox可是选中了这个选框后,在后台用到这个控件的checked返回值得时候却显示为false bool isChecked = ((CheckBox) row.FindControl("CBox")).Checked;请问怎样解决这个问题呢,谢谢!
以下是代码
<asp:GridView ID="GridView1" runat="server" AllowPaging ="True" AutoGenerateColumns ="False" Font-Size="10pt"
HorizontalAlign ="Center" Width="780px" ShowHeader ="False" borderwidth="0px">
<Columns >
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CBox" runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
此时可以正确现实出这个checkbox可是选中了这个选框后,在后台用到这个控件的checked返回值得时候却显示为false bool isChecked = ((CheckBox) row.FindControl("CBox")).Checked;请问怎样解决这个问题呢,谢谢!
解决方案 »
- 多域名,多语言,如何实现同步登录
- [100分求助]服务器偶尔会返回这个错误{在一个非套接字上尝试了一个操作}
- .net培训教程中ado.net连接sql server时的错误
- StringBuilder.Append()的奇怪问题
- asp.net调用access报表
- 请大家帮个忙,把我下面的代码用VB.NET改写一下好吗?
- 如何设置从光区启动?
- treeview结构树是不是不能在设计时构造,而一定要用代码生成呢,我在设计时构造在IE浏览时不显示出TREE结构,而是显示出一行有关的结点文
- 初学者的一个有关asp.net调试问题~~~
- 点编辑的时候怎么让datagrid聚焦??急!!
- 【求助】怎么才能触发Application_End事件。
- 求一条sql语句 急!!!!!!!顶者有分
{
if (inSession() != true)
{
Response.Redirect("index.aspx");
}
else
{
GV();
}
}
应该没什么问题吧
protected void Page_Load(object sender, EventArgs e)
{
if (inSession() != true)
{
Response.Redirect("index.aspx");
}
else
{
GV();
GridView1.databind();
}
}
{
CheckBox chk;
chk=(CheckBox)gvr.FindControl("CheckBox1");
if(chk.Checked)
{
...
}
}
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox) row.FindControl("CBox")).Checked;
if (isChecked)
{
.....
}
}
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox) row.FindControl("CBox")).Checked;
if (isChecked)
{
.....
}
}
前台的html代码是这样的
<asp:GridView ID="GridView1" runat="server" AllowPaging ="True" AutoGenerateColumns ="False" Font-Size="10pt"
HorizontalAlign ="Center" Width="780px" ShowHeader ="False" borderwidth="0px">
<Columns >
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CBox" runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>结果就是只能显示出这个checkbox的框但是不能得到值
楼上的能不能给提供一个可以试验的代码 谢谢
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox) row.FindControl("CBox")).Checked;
if (isChecked)
{
.....
}
}GridView1.Rows.Count這里好象沒有值,我也郁悶了半天
protected void alldelbtn_Click(object sender, EventArgs e)
{
conString();
CheckBox chk;
foreach (GridViewRow gvr in GridView1.Rows)
{
chk = (CheckBox)gvr.FindControl("CheckBox1");
if (chk.Checked)
{
int id = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value.ToString());
com = new SqlCommand("update authors set delstate=1 where au_id='" + id + "'", con);
try
{
con.Open();
com.ExecuteNonQuery();
lblmessage.Text = "批量删除成功!";
con.Close();
}
catch (Exception ex)
{ lblmessage.Text = "无法批量删除!"; }
}
}
PBind();
}
#endregion
是还是false啊 还是取不出KEY值来啊
huangzhixiang() 都没问题了吗不是
你再好好看看
晕了
那你看看 chk=(CheckBox) row.FindControl("CBox");
chk有没有被实力化啊?
{ CheckBox chk;
foreach (GridViewRow gvr in GridView1.Rows)
{
chk = (CheckBox)gvr.FindControl("CBox");
if (chk.Checked)
{
int id = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value.ToString());
com = new SqlCommand("update authors set delstate=1 where au_id='" + id + "'", con);
try
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = "update MessageBoard set isnew='false' where id='" + GridView1.Rows[i].Cells[1].Text + "'";
ADOConnectionClass.ConnectingwithoutReturn(sqlcmd);
sqlcmd.Dispose();
}
catch (Exception ex)
{
sqlcmd.Dispose();
}
}
}
<asp:GridView ID="GridView1" runat="server" AllowPaging ="True" AutoGenerateColumns ="False" Font-Size="10pt"
HorizontalAlign ="Center" Width="780px" ShowHeader ="False" borderwidth="0px">
<Columns >
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CBox" runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
后台:
protected void Audi_btn_Click(object sender, EventArgs e)
{ CheckBox chk;
foreach (GridViewRow gvr in GridView1.Rows)
{
chk = (CheckBox)gvr.FindControl("CBox");
if (chk.Checked)
{
try
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = "update MessageBoard set isnew='false' where id='" + GridView1.Rows[i].Cells[1].Text + "'";
ADOConnectionClass.ConnectingwithoutReturn(sqlcmd);
sqlcmd.Dispose();
}
catch (Exception ex)
{
sqlcmd.Dispose();
}
}
}Page_Load里的代码是这样写的:protected void Page_Load(object sender, EventArgs e)
{
if (inSession() != true)
{
Response.Redirect("index.aspx");
}
else
{
GV();
GridView1.databind();
}
}结果就是只能显示出这个checkbox的框但是不能得到值
{
Response.Redirect("index.aspx");
}
else
{
GV();
GridView1.databind();
}
改成
if(!IsPostBack)
{
if (inSession() != true)
{
Response.Redirect("index.aspx");
}
else
{
GV();
GridView1.databind();
}
}
试试?
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = "update MessageBoard set isnew='false' where id='" + GridView1.Rows[i].Cells[1].Text + "'";//整个gridview就只有一GridView1.Rows[i].Cells[1].Text肯定不回有值了
ADOConnectionClass.ConnectingwithoutReturn(sqlcmd);
sqlcmd.Dispose();
}
catch (Exception ex)
{
sqlcmd.Dispose();
}
if (inSession() != true)
{
Response.Redirect("index.aspx");
}
else
{
GV();
GridView1.databind();
}
改成
if (inSession() != true)
{
Response.Redirect("index.aspx");
}
else
{
if(!IsPostBack)
{ GV();
GridView1.databind();
}
}
我觉得应该这样改才正确
{
if (inSession() != true)
{
Response.Redirect("index.aspx");
}
else
{
if(!IsPostBack)
{
GV();
GridView1.databind();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (inSession() != true)
{
Response.Redirect("index.aspx");
}
else
{
if(!IsPostBack)
{
GV();
GridView1.databind();
this.GridView1.DataKeyNames = new string[] { "mm_ID" };
//读该行的主键值!!
}
}
}
前台:
<script type="text/javascript" language="javascript">
function SelectAllCheckboxes(spanChk){
var oItem = spanChk.children;
var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
xState=theBox.checked;
elm=theBox.form.elements;
for(i=0;i<elm.length;i++)
if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
{
if(elm[i].checked!=xState)
elm[i].click();
}
}
</script>
<asp:TemplateField HeaderText="全选">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox" />全选
</HeaderTemplate>
</asp:TemplateField>
后台,当然你也可以不使用全选,可以随便点几个
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (Session.Contents["Control"].ToString() == "N")
{
Response.Write("<script language='javascript'>alert('您尚未分配该权限,请与管理员联系!');</script>");
return; }
else
{
int temp = 0;
dbm_cp_dal dal = new dbm_cp_dal();
for (int i = 0; i < gdv_CP.Rows.Count; i++)
{
GridViewRow row = gdv_CP.Rows[i];
string ctlname = "chkSelect";
Control mycontrol = row.FindControl(ctlname);
bool isChecked = ((CheckBox)mycontrol).Checked;
if (isChecked)
{
temp = dal.Delete("where CPID=" + System.Convert.ToInt32(row.Cells[1].Text));
}
}
if (temp >= 1)
{
Response.Write("<script language='javascript'>alert('删除数据成功!');</script>");
dal = null;
GridViewBind();
}
else
{
Response.Write("<script language='javascript'>alert('请先选择要删除的数据!');</script>");
return;
}
} }
catch
{
Response.Write("<script language='javascript'>alert('数据删除失败!');</script>");
return;
} }
答案里面有上面这句话的都有分。