新手求教问题

ls2320518 2010-09-23 09:11:12
我用一个gridview实现删除功能,不是自带的。怎么实现。而且,是在模版列放入一个ImageButton,来完成删除。是不是要写事件,请高手讲下。谢谢。
...全文
83 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuhuiwqvb 2010-09-24
  • 打赏
  • 举报
回复

code=C#]
页面代码:(例子哈)
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:ImageButton ID="ibdelete" ImageUrl="~/img/bt_del.gif" runat="server" />
</ItemTemplate>
</asp:TemplateField>
后台代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[6].Controls.Count > 0)//该图片按钮在Gridview的第7列,所以是cell[6]
{
ImageButton imgbtndel = (ImageButton)e.Row.Cells[6].FindControl("ibdelete");

imgbtndel.Attributes.Add("onclick", "return confirm('确认删除吗?');");

}
}
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
long chcid = long.Parse(this.GridView1.Rows[e.RowIndex].Cells[0].Text);
try
{
if (chancebll.deletechance(chcid) > 0)
{
Page.ClientScript.RegisterStartupScript(typeof(string), "msg", "alert(\"删除成功!\");", true);
Initsal_chance();
}
}
catch
{
Page.ClientScript.RegisterStartupScript(typeof(string), "msg", "alert(\"删除失败!\");", true);
}
}

[/code]
shazhibox 2010-09-24
  • 打赏
  • 举报
回复
学习中。。。。。。。。。
wwfgu00ing 2010-09-24
  • 打赏
  • 举报
回复
HTML code<%@ Import Namespace="System.IO" %>

<table style="width: 98%; background-color: #e7e3e7;" cellspacing="1" cellpadding="4"
align="center" id="ajaxtable">
<tbody>
<tr align="center" class="head">
<td style="width: 5%;">
操作
</td>
<td width="50%">
文件名
</td>
<td style="width: 30%;">
生成日期
</td>
</tr>
<% foreach (FileInfo f in files) { %>
<tr align="center" class="dinfo" style="background-color: #D8D8D8;">
<td>
<a href="javascript:delF('<%=f.Name %>','delfile',<%=ModelID %>)">删除</a>
</td>
<td>
<a href="/<%=FilePath + "/" + ModelID + "/" + f.Name %>" target="_blank">
<%=f.Name %>
</a>
</td>
<td>
<%=f.LastWriteTime.ToString("yyyy-MM-dd HH:mm") %>
</td>
</tr>
<%} %>
</tbody>
</table>



后台
C# code
protected List<FileInfo> files = new List<FileInfo>();
protected string FilePath = ConfigurationManager.AppSettings["FileDir"];

protected void Page_Load(object sender, EventArgs e) {
if (!Request.IsAuthenticated)
return;
string strTemplatePath = Server.MapPath(string.Format("/{0}", FilePath));
DirectoryInfo dir = new DirectoryInfo(strTemplatePath);
if (dir.Exists) {
files.AddRange(dir.GetFiles("*.xlsx").OrderByDescending(t => t.LastWriteTime));
}

handyzhu 2010-09-23
  • 打赏
  • 举报
回复
要在commandName里面写成delete
然后再gridView里面的数据行的删除时间里面写方法
zhangaijiang 2010-09-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 huangwenquan123 的回复:]
Gridview设置datakeynames或设置ImageButton的CommandArgument绑定主键
Imagebutton设置CommandName
if(CommandName=="delete")
{
int id = Convert.Toint32(e.CommandArgument);
//int id = GridView1.DataKeys[e.Row.……
[/Quote]

UP 一下

一切为了你 2010-09-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 huangwenquan123 的回复:]
Gridview设置datakeynames或设置ImageButton的CommandArgument绑定主键
Imagebutton设置CommandName
if(CommandName=="delete")
{
int id = Convert.Toint32(e.CommandArgument);
//int id = GridView1.DataKeys[e.Row.……
[/Quote]
............
huangwenquan123 2010-09-23
  • 打赏
  • 举报
回复
Gridview设置datakeynames或设置ImageButton的CommandArgument绑定主键
Imagebutton设置CommandName
if(CommandName=="delete")
{
int id = Convert.Toint32(e.CommandArgument);
//int id = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
...
}
(2)ajax
function(id)
{
$.ajax({
url:"xx.ashx",
type:"post",
data:"ID="+id+"",
success:function(result){
//....
}
})
}
xx.ashx
int id = Convert.Toint32(Request.Form["ID"])
...
delete from table where ID="+id+"
FocusNT 2010-09-23
  • 打赏
  • 举报
回复
把ImageButton放到GridView模板列里面,然后为ImageButton指定RowCommand名称,最后在GridView的RowCommand事件中写代码,if(e.RowCommand=="你指定的名字"){Do SomeThing}
zftow110 2010-09-23
  • 打赏
  • 举报
回复
gridview_RowCommand里写删除

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧