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

ASP.NET中使用DATAGRID绑定数据库并分页显示(二)
ASP.NET中使用DATAGRID绑定数据库并分页显示(二)
分页没有什么问题,较为简单。我想请教的问题是:
1:如何选中一行?(当然,可以单击击某一列来实现,但又如何改变这一行的显示状态呢(例如改变背景))。总之,要让操作者很明显地感觉到,当前他选中了哪一行?而且要让程序知道,选中了哪一行?
2:如果在表中显示一个复选框?
3:当用户选择了一些复选框(CHECKBOX)后,提交页面,请问程序如何获知表中共有哪些行被选中了?
-----
分还有不少,如果朋友们觉得有用的话,请说话,呵呵。好几年没有做过ASP的开发了,手生了不少,请朋友们不要见笑。

------解决方案--------------------
to 1:
http://community.csdn.net/Expert/topic/5316/5316019.xml?temp=.4860651
protected void show_teaching_manage_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Pager)
{
e.Row.Attributes.Add( "onmouseover ", "this.style.backgroundColor= '#e0e0e0 ' ");
e.Row.Attributes.Add( "onmouseout ", "this.style.backgroundColor= 'White ' ");
e.Row.Style[ "cursor "] = "hand ";

e.Row.Cells[this.show_teaching_manage .Columns.Count -1].Attributes.Add( "onclick ", "return confirm( '确定要删除此知识点? '); ");
}
}
------解决方案--------------------
private void dgdSupplier_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//删除确认
//LinkButton delBttn = (LinkButton) e.Item.Cells[1].Controls[0];
//delBttn.Attributes.Add( "onclick ", "j avascript:return confirm( '确定删除 " + e.Item.Cells[4].Text + "? '); ");
//颜色交替
e.Item.Attributes.Add( "style ", "cursor:hand ");
e.Item.Attributes.Add( "onmouseover ", "this.style.backgroundColor= 'Moccasin ' ");
e.Item.Attributes.Add( "onmouseout ", "this.style.backgroundColor= ' ' ");
//e.Item.Attributes.Add( "onclick ", "SetValue( "+e.Item.Cells[0].Text+ ",\ " " + e.Item.Cells[1].Text.ToString() + "\ ") ");
e.Item.Attributes.Add( "onclick ", "SetValue( ' "+e.Item.Cells[0].Text+ " ', ' " + e.Item.Cells[1].Text.ToString()+ " ', ' " + e.Item.Cells[2].Text.ToString() + " ', ' " + e.Item.Cells[3].Text.ToString() + " ', ' "+ e.Item.Cells[4].Text.ToString()+ " ') ");


if(e.Item.ItemType == ListItemType.Item)
{
e.Item.Attributes.Add( "onmouseout ", "this.style.backgroundColor= '#ffffff ' ");
}

if(e.Item.ItemType ==ListItemType.AlternatingItem)
{
e.Item.Attributes.Add( "onmouseout ", "this.style.backgroundColor= 'seashell ' ");
}


}


}

------解决方案--------------------
获取所有选中的行的方法供你参考
/// <summary>
/// 操作多列时,获取每列的DataKeyField字段的值,DataGrid必须设置DataKeyField
/// </summary>
/// <param name= "objDataGrid "> DataGrid控件对象 </param>
/// <param name= "strCheckBoxID "> HtmlInputCheckBox的ID值 </param>
/// <returns> 返回DataKeyField以逗号分隔的字符串 </returns>
public static string getSelectedDataGridKeys(DataGrid objDataGrid, string strCheckBoxID) {
string strResult = string.Empty;
for (int i = 0; i < objDataGrid.Items.Count; i++) {
HtmlInputCheckBox objCheckBox = (HtmlInputCheckBox)objDataGrid.Items[i].FindControl(strCheckBoxID);