删除功能

第一种实现删除方式

1.删除弹出确认框,一般可以采取LinkButton,在其OnClientClick事件里添加

                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" />
                    <asp:BoundField DataField="Name" HeaderText="人员名称" />
                    <asp:BoundField DataField="Leader" HeaderText="直接领导" />
                    <asp:BoundField DataField="Leader2" HeaderText="间接领导" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="lbtnDelete" OnClientClick="return confirm('你确认要删除?')" CommandName="del"
                                runat="server" Text="删除"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="lbtnEdit" CommandName="ed" OnClientClick='<%# "ShowEdit("+Eval("ID")+")" %>'
                                runat="server" Text="编辑"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>

 

注意: Delete Button的CommandName不要设置为Delete,否则有可能会出现错误 ,其Delete的操作代码可以写在RowCommand里,如

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "del")
            {
                GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                string sLoginName = row.Cells[1].Text;
                string sID = row.Cells[0].Text;
                RemoveUser(sLoginName);
                if (!DeletePeoInfoItem(Convert.ToInt32(sID)))
                {
                    AlterMessage("删除不成功,请稍后再试!");
                }
                BindGridView();
            }
            if (e.CommandName == "ed")
            {
                BindGridView();
            }
        }
        catch (Exception ex)
        {
            string sMessage = ex.Message;
            AlterMessage("操作不成功,请稍后再试!");
        }
    }

 

第二种实现删除方式:

下面的是另一种方式实现删除,其操作性及灵活性比较强,当多个GridView的时候,其会将更加之方便

  前台代码:

                                    <asp:TemplateField HeaderText="" >
<ItemTemplate>
<asp:Button ID="btnDelete" Text="删除" CssClass="btn" runat="server" OnClick="btnDelete_Click" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" width="100"/>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>

 

 后台代码:

protected void btnDelete_Click(object sender, EventArgs e)
{
Button btnDelete = ((Button)((sender as Button).Parent.Parent as GridViewRow).FindControl("btnDelete"));
Label LabelID = ((Label)((sender as Button).Parent.Parent as GridViewRow).FindControl("LabelID"));
Label lblFormTypeDo = ((Label)((sender as Button).Parent.Parent as GridViewRow).FindControl("lblFormTypeDo"));
SPList list = null;
string strTitle = lblFormTypeDo.Text;
。。。。。。。。。。。
}

 

分页:

做法: GridView启用内置的分页代码,要设置AllowPaging属性

    private void GVPropertiesSetting()
{
this.GridView1.PageSize = 15;
this.GridView1.AllowPaging = true;
this.GridView1.AutoGenerateColumns = false;
this.GridView1.PagerSettings.Mode = PagerButtons.NumericFirstLast;
this.GridView1.PagerSettings.FirstPageText = "首页";
this.GridView1.PagerSettings.LastPageText = "末页";
this.GridView1.PagerSettings.NextPageText = "下一页";
this.GridView1.PagerSettings.PreviousPageText = "上一页";
this.GridView1.DataKeyNames = new string[] { "ID" };

}

 同时还要写其PageIndexChanging方法代码,如

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
if (ViewState["DataSource"] != null)
{
this.GridView1.DataSource = (DataTable)ViewState["DataSource"];
this.GridView1.DataBind();
}
}

 

编辑功能
 当其有编辑的功能时,要实时刷新,可以采取让其编辑的OnClientClick事件window.showModalDialog,其服务器事件OnClick重新加载数据,事例如下:

其中GetSelectValue,是后台的一个方法

     function ShowEditNode() {
window.showModalDialog('Organizedeal.aspx?ID=' + <%=GetSelecteValue() %>+ '&action=edit', window, 'dialogWidth:400px;dialogHeight:200px;status:no;help:no;');
}
<asp:ImageButton ID="imgBtnEdit" runat="server" ImageUrl="images/edit.gif" BorderStyle="None"
ToolTip="编辑项" OnClick="imgBtnEdit_Click" OnClientClick="ShowEditNode();"/>

 

    public string GetSelecteValue()
{
if (ViewState["SelectedID"] != null)
{
return ViewState["SelectedID"].ToString();
}
return string.Empty;

}

 编辑事件,如果要绑定其一些值,可以像如下那样绑定

                        <ItemTemplate>
 <asp:LinkButton Text="编辑" runat="server" ID="lbnEdit"
OnClick="lbnEdit_Click" OnClientClick='<%# "ShowEdit(\""+Eval("Code")+"\")" %>'/>

</ItemTemplate>

 

posted on 2012-03-26 14:51  gzh4455  阅读(284)  评论(0编辑  收藏  举报