日期:2014-05-20  浏览次数:20595 次

如何用Js遍历dataGrid中模板列的checkbox 是否被选中!高手帮忙一下!!!!
如题,由于小弟对js   不是很熟,现在用dataGrid显示数据,其中有一个为checkbox的模板列,此外还有修改按钮!
现在问题是要求单击“修改”按钮时判断checkbox是否被选中,选中后判断是否为单选(因为一次只能修改一行纪录)
我的服务器代码为:   btn_modify.attribute.add( "onlick ", "javascript:checkModify(); ")
现在要求大侠们帮我写一下这个checkModify();的js代码,要求遍历datagrid中的checkbox,并判断!
写的详细点!!!小弟先谢过了!

------解决方案--------------------
function checkModify()
{
var objs = document.getElementsByTagName( "input ");

for(var i=0; i <objs.length; i++)
{
if(objs[i].type.toLowerCase() == "checkbox " )
{
if(objs[i].checked)
{
//...选中
}

}
}
}
------解决方案--------------------
一次只能修改一行纪录,那你这样做好了

<% @ Page Language= "C# " %>
<% @ Import Namespace= "System.Data " %>
<% @ Import Namespace= "System.Data.OleDb " %>
<Script Language= "C# " Runat= "Server ">
OleDbConnection MyConn;
public void Page_Load(Object src,EventArgs e)
{
//連接語句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "+Server.MapPath( ". ")+ "..\\DataBase\\db1.mdb; ";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open(); if(!Page.IsPostBack)
{
BindGrid();
}
}
ICollection CreateTable()
{
string strSel = "select * from Score ";
DataSet ds = new DataSet(); OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds, "Score "); return ds.Tables[ "Score "].DefaultView;
}
public void BindGrid()
{
score.DataSource = CreateTable();
score.DataBind();
}//處理Edit命令
public void DataGrid_EditCommand(Object sender,DataGridCommandEventArgs e)
{
score.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}//處理Cancel命令
public void DataGrid_CancelCommand(Object sender,DataGridCommandEventArgs e)
{
score.EditItemIndex = -1;
BindGrid();
}//處理Update命令
public void DataGrid_UpdateCommand(Object sender,DataGridCommandEventArgs e)
{
//更新數據庫中的信息
string strName = e.Item.Cells[1].Text;//ReadOnly
int intChinese = Int32.Parse(((TextBox)e.Item.Cells[2].Controls[0]).Text);//Edit狀態下Cell為TextBox
int intMath = Int32.Parse(((TextBox)e.Item.Cells[3].Controls[0]).Text);
int intEnglish = Int32.Parse(((TextBox)e.Item.Cells[4].Controls[0]).Text); //更新數據庫中的數據
string strUpdate = "Update Score Set Chinese= "+intChinese+ ",Math= "+intMath+ ",English= "+intEnglish+ " Where Name= ' "+strName+ " ' ";
OleDbCommand MyComm = new OleDbCommand(strUpdate,MyConn);
MyComm.ExecuteNonQuery();

score.EditItemIndex = -1;
BindGrid();}
</script>
<html>
<head>
<title> </title>
</head>
<body>
<form runat= "server ">
<center>
<b> 演示EditCommandColumn </b>
<asp:DataGrid id= "score " runat= "server "
HeaderStyle-BackColor= "#aaaadd "
AlternatingItemStyle-BackColor= "#eeeeee "
AutoGenerateColumns= "False "
OnEditCommand= "DataGrid_EditCommand "
OnUpdateCommand= "DataGrid_UpdateCommand "
OnCancelCommand= "DataGrid_CancelCommand "