GridView平时要注意的地方。
1)在GridView中,添加删除按钮后,一般需要弹出对话框来确认,下面是一种写法,写在前台:<asp:LinkButton ID="lkbDelete" runat="server" CommandName="delete" OnClientClick="return confirm('确定要删除该条记录么?');">[删除]</asp:LinkButton>
2)在GridView中,系统自带的选择、删除、编辑按钮,可以用TemplateField(模板),在ItemPlate里增加[选择](select)、[编辑](edit)、[删除](delete),Edititemplate里增加[更新](update)、[取消](cancel) ,CommandName设置为括号里的Name。这样设置增强了GridView的美感,在功能上也完全可以取代系统原来的功能。
Example:
<asp:TemplateField HeaderText="操作">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="lbn01" runat="server" CommandName="select">[查看]</asp:LinkButton>
<asp:LinkButton ID="lbn02" runat="server" CommandName="edit">[编辑]</asp:LinkButton>
<asp:LinkButton ID="lbn03" runat="server" CommandName="delete" OnClientClick="return confirm('确定要删除该条记录么?');">[删除]</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbn04" runat="server" CommandName="update">[更新]</asp:LinkButton>
<asp:LinkButton ID="lbn05" runat="server" CommandName="cancel">[取消]</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
Example:点[选择]转到新页面,要在<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="lbn01" runat="server" CommandName="select">[查看]</asp:LinkButton>
<asp:LinkButton ID="lbn02" runat="server" CommandName="edit">[编辑]</asp:LinkButton>
<asp:LinkButton ID="lbn03" runat="server" CommandName="delete" OnClientClick="return confirm('确定要删除该条记录么?');">[删除]</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbn04" runat="server" CommandName="update">[更新]</asp:LinkButton>
<asp:LinkButton ID="lbn05" runat="server" CommandName="cancel">[取消]</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
3)鼠标移动时,GridView颜色变化,代码如下: protected void GvList_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#C6EBFF'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
4)Gridview增加模板列(CheckBox),点“全部选择”时,页面所有CheckBox全部选中(取消时true->false){
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#C6EBFF'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
function funSelectAll()
{
for(var i=0;i<document.forms[0].length;i++)
{
if (document.forms[0][i].type=="checkbox")
{
document.forms[0][i].checked= true;
}
}
}
5)后台写的全选与全不选(添加服务器控件)
{
for(var i=0;i<document.forms[0].length;i++)
{
if (document.forms[0][i].type=="checkbox")
{
document.forms[0][i].checked= true;
}
}
}
protected void lkb01_Click(object sender, EventArgs e)
{
foreach (System.Web.UI.WebControls.GridViewRow gr in GvList.Rows)
{
CheckBox ckb = (CheckBox)gr.FindControl("ckb");
ckb.Checked = true;
}
}
protected void lkb02_Click(object sender, EventArgs e)
{
foreach (System.Web.UI.WebControls.GridViewRow gr in GvList.Rows)
{
CheckBox ckb = (CheckBox)gr.FindControl("ckb");
ckb.Checked = false;
}
}
{
foreach (System.Web.UI.WebControls.GridViewRow gr in GvList.Rows)
{
CheckBox ckb = (CheckBox)gr.FindControl("ckb");
ckb.Checked = true;
}
}
protected void lkb02_Click(object sender, EventArgs e)
{
foreach (System.Web.UI.WebControls.GridViewRow gr in GvList.Rows)
{
CheckBox ckb = (CheckBox)gr.FindControl("ckb");
ckb.Checked = false;
}
}
浙公网安备 33010602011771号