新手求教问题
我用一个gridview实现删除功能,不是自带的。怎么实现。而且,是在模版列放入一个ImageButton,来完成删除。是不是要写事件,请高手讲下。谢谢。 --------------------编程问答-------------------- gridview_RowCommand里写删除 --------------------编程问答-------------------- 把ImageButton放到GridView模板列里面,然后为ImageButton指定RowCommand名称,最后在GridView的RowCommand事件中写代码,if(e.RowCommand=="你指定的名字"){Do SomeThing} --------------------编程问答-------------------- 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+" --------------------编程问答--------------------
............ --------------------编程问答--------------------
UP 一下
--------------------编程问答-------------------- 要在commandName里面写成delete
然后再gridView里面的数据行的删除时间里面写方法 --------------------编程问答--------------------
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]
--------------------编程问答--------------------
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));
}
补充:.NET技术 , ASP.NET