ASP.NET gridview复选框问题
我正在尝试在gridview中实现一个复选框,
这个复选框的作用是验证一条记录,
当按下这个验证按钮时,所有带有选中复选框的项目都会输入到数据库中
这是我的代码:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cbox = ((CheckBox)row.FindControl("Verify"));
if (cbox.Equals(true))
{
String DraftsText = ((TextBox)row.FindControl("numDrafts")).Text;
String TCtext = ((TextBox)row.FindControl("numTC")).Text;
if (row.RowType == DataControlRowType.DataRow)
{
//Header trs = new Header();
// GridView1.Rows[0].FindControl("numTC");
if (TCtext != "" && DraftsText != "")
{
// try
// {
string date = row.Cells[4].Text;
DateTime dateTime = Convert.ToDateTime(date);
string dateFormatted = dateTime.ToString("d-MMM-yy");
string unit = row.Cells[5].Text;
string currency = row.Cells[6].Text;
string totalFC = row.Cells[7].Text;
string totalDC = row.Cells[8].Text;
int d = Convert.ToInt32(DraftsText);
int tc = Convert.ToInt32(TCtext);
hdr = new Header(d, tc, dateFormatted, unit, currency, totalFC, totalDC);
hdr.InsertFCTC(hdr);
}
}
}
}
}
我可能会这样做错误的方式,但在 if (cbox.Equals(true)) 它给了我一个例外:对象引用未设置为对象的实例。
知道我能做什么来解决这个问题吗?
非常感谢
I am trying to implement a checkbox within gridview,
The job of this checkbox is to verify a record,
When this verify button is pressed, all items with a checked checkbox are inputted into the database
This is my code:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cbox = ((CheckBox)row.FindControl("Verify"));
if (cbox.Equals(true))
{
String DraftsText = ((TextBox)row.FindControl("numDrafts")).Text;
String TCtext = ((TextBox)row.FindControl("numTC")).Text;
if (row.RowType == DataControlRowType.DataRow)
{
//Header trs = new Header();
// GridView1.Rows[0].FindControl("numTC");
if (TCtext != "" && DraftsText != "")
{
// try
// {
string date = row.Cells[4].Text;
DateTime dateTime = Convert.ToDateTime(date);
string dateFormatted = dateTime.ToString("d-MMM-yy");
string unit = row.Cells[5].Text;
string currency = row.Cells[6].Text;
string totalFC = row.Cells[7].Text;
string totalDC = row.Cells[8].Text;
int d = Convert.ToInt32(DraftsText);
int tc = Convert.ToInt32(TCtext);
hdr = new Header(d, tc, dateFormatted, unit, currency, totalFC, totalDC);
hdr.InsertFCTC(hdr);
}
}
}
}
}
I might be going at this the wrong way but in the if (cbox.Equals(true))
its giving me an exception: Object reference not set to an instance of an object.
Any idea what i can do to solve this?
Many Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个
if (cbox.Equals(true))
应该是if (cbox.Checked)
因为你的
cbox 是一个复选框对象
,所以它不能用于比较,因此您必须使用cbox
Checked
属性,它将返回true/false
This
if (cbox.Equals(true))
should beif (cbox.Checked)
Since your
cbox is a checkbox object
it can't be used to compare, so you have to use thecbox
Checked
Property, which will returntrue/false
您收到 NullPointerException,因为未找到建议的复选框!或者直接转换为 CheckBox 类型的实例无法按预期工作。
You receive a NullPointerException because the suggested checkbox wasn't found! Or the direct cast into an instance of type CheckBox doesn't worked as expected.
将您的代码更改为类似以下内容并重试:
Change your code to something like this and retry: