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>
                    &nbsp;<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>
                    &nbsp;<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进行判断,否则不进入
        }

    }

///之外请为控件指定数据源

原创粉丝点击