我想根据选中的复选框更新数据库中的列总数 [英] I want to update the total number of a column in the database in accordance with a checked checkbox
本文介绍了我想根据选中的复选框更新数据库中的列总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello,I want to select multiple checkboxes in gridview and update those rows if a checkbox in that row has been checked.For now it only updates for the first checkbox it finds.I am using the 3 tier architecture.The code looks as follows, I have code behind the button and on the Business Logic layer
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkselect = (CheckBox)
GridView1.Rows[i].Cells[0].FindControl("chkselect");
if (chkselect != null)
{
if (chkselect.Checked)
{
string idca = GridView1.Rows[i].Cells[1].Text;
try
{
BusinessLogic.vote obj = new BusinessLogic.vote();
obj.id = Convert.ToInt32(idca);
obj.sendvote();
}
catch (Exception ex)
{
string err = string.Empty;
err += ex.Message;
throw new Exception(err);
}
finally
{
Response.Redirect("~/Vote Cast.aspx");
}
}
}
}
}
}
}
namespace Business Logic
public void sendvote()
{
csDAL objdal = new csDAL();
List<csparameterlisttype> objl = new List<csparameterlisttype>();
objl.Add(new csParameterListType("@candidate_id", System.Data.SqlDbType.Int, id));
objdal.executespreturnnd("update_total", objl);
}
</csparameterlisttype></csparameterlisttype>
推荐答案
您最终将重定向到Vote Cast.aspx.如果选中了一个复选框,则try块将执行,并且将移动到最后一个块,它将重定向到下一页,并且不会检查其他复选框
You are redirecting to Vote Cast.aspx at finally. If one check box checked is matched then try block will execute and the will move to the final block and it will redirect u to the next page and it wont check for other check boxes
第一行已处理,finally
块中的Response.Redirect
会将您重定向到新页面,因此其他行将不被处理.仅在for
循环完成后,才应重定向到新页面.
请记住,无论是否引发异常,finally
块中的代码始终会执行.
After your first row is processed, theResponse.Redirect
in yourfinally
block redirects you to the new page, so the other rows are not processed. You should redirect to the new page only after thefor
loop completes.
Remember that the code in afinally
block always executes irrespective of if an exception is thrown or not.
这篇关于我想根据选中的复选框更新数据库中的列总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文