asp.net 在GridView控件上实现修改
时间:2011-04-07 来源:MyBeN
先来看一下效果
步骤一:先创建一个GridView 在编辑列中将此字段转换为TemplateField
View Code<asp:GridView ID="GridUpdaMoney" runat="server"步骤二:添加事件RowCancelingEdit View Code
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>
protected void GridUpdaMoney_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)步骤三:添加RowUpdating事件 View Code
{
GridUpdaMoney.EditIndex = -1;
BindGridUpdaMoney(ViewState["PersonCardNo"].ToString());
}
protected void GridUpdaMoney_RowUpdating(object sender, GridViewUpdateEventArgs e)步骤四:添加RowEditing事件 View Code
{
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());
}
protected void GridUpdaMoney_RowEditing(object sender, GridViewEditEventArgs e)完成,为了不忘记就记录一下...
{
GridUpdaMoney.EditIndex= e.NewEditIndex;
BindGridUpdaMoney(ViewState["PersonCardNo"].ToString());
}
相关阅读 更多 +