在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一:修改
先来看一下效果
步骤一:先创建一个GridView 在编辑列中将此字段转换为TemplateField
View Code
<asp:GridView ID="GridUpdaMoney" runat="server"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" EmptyDataText="暂时没有数据" AutoGenerateColumns="False" DataKeyNames="id" onrowcancelingedit="GridUpdaMoney_RowCancelingEdit" onrowediting="GridUpdaMoney_RowEditing" onrowupdating="GridUpdaMoney_RowUpdating"> <RowStyle ForeColor="#000066"/> <Columns> <asp:TemplateField HeaderText="工资申报年月份"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("C_Year") %>' onFocus="WdatePicker()"></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("C_Year") %>'></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center"/> </asp:TemplateField> <asp:TemplateField HeaderText="标准工资"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("C_BasicMoney") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("C_BasicMoney") %>'></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center"/> </asp:TemplateField> <asp:TemplateField HeaderText="申请状态"> <ItemTemplate> <asp:Label ID="status" runat="server" Text='<%# GetStatus(Eval("status").ToString()) %>'></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center"/> </asp:TemplateField> <asp:TemplateField HeaderText="修改" ShowHeader="False"> <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:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="编辑"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="White" ForeColor="#000066"/> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"/> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White"/> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"/> </asp:GridView> 步骤二:添加事件RowCancelingEdit
View Code
protectedvoid GridUpdaMoney_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{ GridUpdaMoney.EditIndex =-1; BindGridUpdaMoney(ViewState["PersonCardNo"].ToString()); } 步骤三:添加RowUpdating事件
View Code
protectedvoid GridUpdaMoney_RowUpdating(object sender, GridViewUpdateEventArgs e)
{ int id = Convert.ToInt32(GridUpdaMoney.DataKeys[e.RowIndex].Value); TextBox year = GridUpdaMoney.Rows[e.RowIndex].FindControl("TextBox1") as TextBox; TextBox money = GridUpdaMoney.Rows[e.RowIndex].FindControl("TextBox2") as TextBox; BasicMoney bm =new BasicMoney(); bm.Id = id; bm.Year = Convert.ToDateTime(year.Text.Trim()); bm.BasicMoney1 = Convert.ToDecimal(money.Text.Trim()); int result = BasicMoney.UpdaBasicMoneyById(bm); if (result >0) { JS.Alert(Page, "修改成功"); } else { JS.Alert(Page, "修改失败"); } GridUpdaMoney.EditIndex =-1; BindGridUpdaMoney(ViewState["PersonCardNo"].ToString()); } 步骤四:添加RowEditing事件
View Code
protectedvoid GridUpdaMoney_RowEditing(object sender, GridViewEditEventArgs e)
{ GridUpdaMoney.EditIndex= e.NewEditIndex; BindGridUpdaMoney(ViewState["PersonCardNo"].ToString()); } 二:删除(2012-2-9日修改随笔)
删除和添加效果
View Code
<asp:GridView Width="100%" ID="GridView2" runat="server" 删除代码:
View Code
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e) 添加代码:(注要显示添加的行必须设置属性 ShowFooter="True" )
View Code
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e) 完成,为了不忘记就记录一下...
|
请发表评论