GridView的使用和后台事件
来源:互联网 发布:淘宝店铺48分重新开店 编辑:程序博客网 时间:2024/05/22 03:07
---控件代码 author:heguikun
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4"
DataKeyNames="RoomId" DataSourceID="ObjectDataSource1" GridLines="Horizontal" Width="741px" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
<FooterStyle BackColor="White" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="RoomId" HeaderText="编号" SortExpression="RoomId" />
<asp:BoundField DataField="Number" HeaderText="房号" SortExpression="Number" />
<asp:BoundField DataField="BedNumber" HeaderText="床位数" SortExpression="BedNumber" />
<asp:BoundField DataField="GuestNumber" HeaderText="客人数" SortExpression="GuestNumber" />
<asp:BoundField DataField="State" HeaderText="状态" SortExpression="State" />
<asp:BoundField DataField="Description" HeaderText="描述" SortExpression="Description" />
<asp:TemplateField ShowHeader="False" HeaderText="编辑">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="更新"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="取消"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/edit.gif" CommandArgument='<%# Eval("RoomId") %>' CommandName="ed" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderText="删除" >
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='~/images/delete.gif'
CommandArgument='<%# Eval("RoomId") %>' CommandName="del" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView>
------------------------------------------------------------
注意事项:
1.控件中添加两个事件RomCommand和RowDataBound
OnRowCommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound"
2注意模板项的属性和事件
-------------------------------------
<asp:TemplateField ShowHeader="False" HeaderText="编辑">
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/edit.gif" CommandArgument='<%# Eval("RoomId") %>' CommandName="ed" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderText="删除" >
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='~/images/delete.gif'
CommandArgument='<%# Eval("RoomId") %>' CommandName="del" />
</ItemTemplate>
</asp:TemplateField>
-------------------------------------------------------
CommandArgument='<%# Eval("RoomId") %>'//为属性id赋值,为后台调用方法用到id的值
CommandName="ed" //定义一个名为后台判断
-----------------------------------下面是后台两个事件的代码-----------------------------------------------
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName=="del")//删除
{
// this.Response.Write("<script type='text/javascript'>alert('是否删除');</script>");//跳到新也没在提示
//this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('是否删除');</script>");//当前有提示
RoomManager.DeleteRoomByRoomId(Convert.ToInt32(e.CommandArgument));
Response.Redirect("AllRoom.aspx");//重定向
} if (e.CommandName=="ed")//编辑
{
Page.Server.Transfer("EditRoom.aspx?roomId="+e.CommandArgument.ToString());
}
}
//斑马线效果
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{ //为选中的row添加样式
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='yellow'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
ImageButton imgbtn = (ImageButton)e.Row.FindControl("ImageButton1");//为删除按钮添加删除事件,
imgbtn.Attributes.Add("onclick", "return confirm('您确认删除');");//当确定后才跳到GridView1_RowCommand进行判断,否则不进入
}
}
///之外请为控件指定数据源
- GridView的使用和后台事件
- gridview的使用(后台代码)
- GridView的RowDataBound事件和DataBound事件
- GridView使用之RowCommand事件的使用
- GridView的简单使用,带有点击事件
- .NET 使用GridView控件的SelectIndexChanging事件
- GridView的分页事件和onRowCommand事件冲突的解决方法
- GridView的RowCommand事件和DataList的ItemCommand事件
- GridView的RowCommand事件和DataList的ItemCommand事件
- GridView 使用笔记 - 后台部分
- GridView 事件和用法
- GridView 事件和用法
- Asp.net之使用GridView控件的RowCommand事件实现修改和删除
- gridView和sqlDataSource的使用
- GridView和ArrayAdapter的使用
- [转]Raising An Event From CheckBox In A GridView (GridView中模板表的CheckBox的后台事件处理)
- 关于UseSubmitBehavior和OnClientClick同时使用,导致无法触发后台事件的问题
- GridView控件使用(下)——监听事件的使用
- 更上层楼:动态安装你的windows服务
- JNI技术与Android应用
- java.net.InetAddress类的应用
- bean:include的应用
- 字符串的strlen与sizeof
- GridView的使用和后台事件
- memcahe的安装、启功和关闭
- 用c#开发一个类似AutoCAD功能的程序
- 设为首页
- java网络编程概述
- Dwr结合SSH使用
- url中有加号
- 简单的多线程示例
- java图片缩放不失真(收集)