首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

ASP.NET中使用DATAGRID绑定数据库并分页显示(二)解决思路

2012-02-16 
ASP.NET中使用DATAGRID绑定数据库并分页显示(二)ASP.NET中使用DATAGRID绑定数据库并分页显示(二)分页没有

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);


if (objCheckBox.Checked) {
strResult += objDataGrid.DataKeys[objDataGrid.Items[i].ItemIndex] + ", ";
}
}
if (strResult != string.Empty) {
strResult = strResult.Substring(0, strResult.Length - 1);
}
return strResult;
}
/// <summary>
/// 选择DataGrid中所有的CheckBox
/// </summary>
/// <param name= "sender "> 对象 </param>
/// <param name= "strFullBoxName "> 实现全选的CheckBox的ID </param>
/// <param name= "strCheckBoxName "> DataGrid中的CheckBox的ID </param>
/// <param name= "objDataGrid "> 显示数据的DataGrid </param>
public static void selectingAllCheckBox(object sender,string strFullBoxName, string strCheckBoxName, DataGrid objDataGrid) {
DataGridItem objItem = (DataGridItem)(((Control)sender).Parent.Parent);
CheckBox objBox = (CheckBox)objItem.FindControl(strFullBoxName);
if (objBox.Checked) {
for (int i = 0; i < objDataGrid.Items.Count; i++) {
HtmlInputCheckBox objHtmlBox = (HtmlInputCheckBox)objDataGrid.Items[i].FindControl(strCheckBoxName);
objHtmlBox.Checked = true;
}
}
else {
for (int i = 0; i < objDataGrid.Items.Count; i++) {
HtmlInputCheckBox objHtmlBox = (HtmlInputCheckBox)objDataGrid.Items[i].FindControl(strCheckBoxName);
objHtmlBox.Checked = false;
}
}
}

热点排行