之前用gridview控件都是用默认的编辑啊更新等默认功能,今天上网找了些资料,尝试了下用自定义模板实现编辑更新取消删除功能。以下是我最近做的眼镜网后台留言列表,实例图片如下:
点击编辑时出现textbox和checkbox:
以下为各文件的代码,其中红色为应注意地方.蓝色为文件,有理解错误的地方请高手指出,小弟感激不尽
gridview.aspx文件
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id" CellPadding="5" Width="700px" AutoGenerateColumns="false"
OnRowDeleting="GridView1_OnDeleteCommand">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="delete" runat="server" Text="删除" CommandName="delete" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="用户">
<ItemTemplate>
<%# Eval("name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="留言">
<ItemTemplate>
<%# Eval("question") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="回复">
<ItemTemplate>
<%# Eval("answer") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtanswer" runat="server" Text='<%# Eval("answer") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="审核">
<ItemTemplate>
<%# Eval("pass") %>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="pass_check" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button runat="server" Text="编辑" CommandName="edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button runat="server" Text="更新" CommandName="Update" />
<asp:Button ID="cancel" runat="server" Text="取消" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#4380CC" ForeColor="White" Font-Names="幼圆" />
<RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" Font-Size="15px" />
<AlternatingRowStyle BackColor="#DCDEDC" />
<PagerStyle HorizontalAlign="Center" />
</asp:GridView>
mytry.cs文件
1.public DataTable getdate(){返回dataset}
2.public void Update(string answer,int pass,string pk){} //更新
3.public void Delete(string strID){} //删除
gridview.aspx.cs文件
mytry objmytry = new mytry(); //实例化
void binddr()
{
this.GridView1.DataSource = objmytry.getdate();
this.GridView1.DataBind(); //数据绑定
}
protected void GridView1_OnRowCommand(object sender, GridViewEditEventArgs e) //触发编辑事件
{
GridView1.EditIndex = e.NewEditIndex; //切换到编辑页
binddr(); //数据绑定
}
protected void GridView1_OnCancelCommand(object sender, GridViewCancelEditEventArgs e)//触发取消事件
{
GridView1.EditIndex = -1; //后退一页
binddr(); //数据绑定
}
protected void GridView1_OnUpdateCommand(object sender, GridViewUpdateEventArgs e) //触发更新事件
{
string pk = GridView1.DataKeys[e.RowIndex].Value.ToString(); //获取当前行主键id
GridViewRow gvr = GridView1.Rows[e.RowIndex]; //实例化一行
string stranswer = ((TextBox)gvr.FindControl("txtanswer")).Text; //获取textbox里面的值
try
{
bool is_checked = ((CheckBox)gvr.FindControl("pass_check")).Checked; //获取checkbox的值
if (is_checked == true)
{
int a=Convert.ToInt32(is_checked); //把bool型转为整型
objmytry.Update(stranswer, a , pk);
string c = Request.UrlReferrer.ToString();
Response.Redirect(c); //返回上次页面
}
else
{
int b = Convert.ToInt32(is_checked);
objmytry.Update(stranswer, b , pk);
string c = Request.UrlReferrer.ToString();
Response.Redirect(c );
}
}
catch (Exception ex)
{
Alter(ex.Message);
}
}
protected void GridView1_OnDeleteCommand(object sender, GridViewDeleteEventArgs e) //触发删除事件
{
string pk = GridView1.DataKeys[e.RowIndex].Value.ToString();
try
{
objmytry.Delete(pk);
Alter("删除成功!");
binddr();
}
catch (Exception ex)
{
Alter(ex.Message);
}
}
private void Alter(string strmessage) //弹出对话框
{
Response.Write("<script>alert('" + strmessage + "');</script>");
}