无法从转发器中删除任何行 [英] Can't delete any rows from repeater
本文介绍了无法从转发器中删除任何行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在ASP.NET中为我的网站制作了一个转发器,并尝试添加一个按钮,该按钮应该从转发器中删除一行,但是除了刷新页面之外,单击该按钮不会做任何事情。
转发器标记
< asp:Repeater ID = Repeater1 runat = 服务器 >
< ItemTemplate >
< div CL屁 = 详细信息 >
Namn:< asp:Label ID = Label1 runat = server 文本 =' <% #Eval( Namn)%> ' > '> < / asp:标签 > < br / >
地址 < asp:标签 ID = Label3 runat = server 文字 =' <% #Eval( 地址)%> ' > < / asp:标签 > < br / >
Telefonnummer < asp:标签 ID = Label4 runat = 服务器 文本 =' <% #Eval( Telefonnummer)%> ' > < / asp:标签 > < br / >
Mailaddres < asp:Label ID = Label5 runat = server 文本 =' <% #Eval( Epost-address)%> ' > < / asp:Label > < br / >
Meddelande:< asp:Label < span class =code-attribute> ID = Label6 runat = server 文字 =' <% #Eval( < span class =code-string> Kommentar)%> ' > < / asp:Label > < br < span class =code-keyword> / >
< asp:LinkButton ID = lnkDelete runat = server CommandArgument =' <% #Eval( Id)%> ' CommandName = 删除 < span class =code-attribute> onclientclick = 返回确认('您确定要删除?') > 删除< / asp:LinkButton >
< / div >
< span class =code-keyword>< / ItemTemplate >
< / asp:Repeater >
代码落后:
使用系统;
使用 System.Collections.Generic;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.SqlClient;
public partial class 管理员:System.Web.UI.Page
{
受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack )
{
BindRepeater();
}
}
受保护 void rpt_ItemCommand( object source,RepeaterCommandEventArgs e)
{
if (e.CommandName == 删除){
int id = Convert.ToInt32(e.CommandArgument);
deleteKlag(id);
}
}
私有 void deleteKlag( int id)
{
SqlConnection sqlcn = new SqlConnection( @ data source =(LocalDB)\ v11.0; attachdbfilename = | DataDirectory | \Database.mdf; integrated security = True; multipleactiveresultsets = True; application name = EntityFramework);
string str = DELETE FROM [表] WHERE [Id] = @Id;
SqlCommand sqlcm = new SqlCommand(str,sqlcn);
sqlcm.Parameters.AddWithValue( @ Id,id);
if (sqlcn.State == ConnectionState.Closed)
{
sqlcn.Open();
}
sqlcm.ExecuteNonQuery();
sqlcn.Close();
BindRepeater();
}
受保护 void BindRepeater()
{
SqlConnection con = new SqlConnection( @ data source = (LocalDB)\ v11.0; attachdbfilename = | DataDirectory | \Database.mdf; integrated security = True; multipleactiveresultsets = True; application name = EntityFramework);
string command = 从[选择*中]表]跨度>;
SqlCommand cmd = new SqlCommand(command,con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
Repeater1.DataSource = cmd.ExecuteReader();
Repeater1.DataBind();
}
}
有什么想法吗?
我尝试了什么:
我尝试过删除代码的不同版本,但没有一个有效。
解决方案
你缺少任何处理点击的代码,因此它会进行回发,没人监听......
在这里查看示例:
LinkButton。 OnClick方法(EventArgs)(System.Web.UI.WebControls) [ ^ ]
你需要附上你的活动,.net不当点击链接按钮时,本质上知道要调用哪个函数。
< asp:Repeater OnItemCommand =rpt_ItemCommand...
I have made a repeater for my site in ASP.NET and tried adding a button which should delete a row from the repeater, but clicking the button doesn't do anything besides refreshing the page.
Repeater markup
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="detail">
Namn: <asp:Label ID="Label1" runat="server" Text='<%#Eval("Namn") %>'>'></asp:Label><br />
Address <asp:Label ID="Label3" runat="server" Text='<%#Eval("Adress") %>'></asp:Label><br />
Telefonnummer <asp:Label ID="Label4" runat="server" Text='<%#Eval("Telefonnummer") %>'></asp:Label><br />
Mailaddres <asp:Label ID="Label5" runat="server" Text='<%#Eval("Epost-address") %>'></asp:Label><br />
Meddelande: <asp:Label ID="Label6" runat="server" Text='<%#Eval("Kommentar") %>'></asp:Label><br />
<asp:LinkButton ID="lnkDelete" runat="server" CommandArgument='<%#Eval("Id") %>' CommandName="Delete" onclientclick="return confirm('Are you sure you want to delete?')">Delete</asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
Code behind:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeater();
}
}
protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Delete") {
int id = Convert.ToInt32(e.CommandArgument);
deleteKlag(id);
}
}
private void deleteKlag(int id)
{
SqlConnection sqlcn = new SqlConnection(@"data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Database.mdf;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework");
string str = "DELETE FROM [Table] WHERE [Id] = @Id";
SqlCommand sqlcm = new SqlCommand(str, sqlcn);
sqlcm.Parameters.AddWithValue("@Id", id);
if (sqlcn.State == ConnectionState.Closed)
{
sqlcn.Open();
}
sqlcm.ExecuteNonQuery();
sqlcn.Close();
BindRepeater();
}
protected void BindRepeater()
{
SqlConnection con = new SqlConnection(@"data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Database.mdf;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework");
string command = "Select * from [Table]";
SqlCommand cmd = new SqlCommand(command, con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
Repeater1.DataSource = cmd.ExecuteReader();
Repeater1.DataBind();
}
}
Any ideas?
What I have tried:
I have tried different variations of the deleting code but none have worked.
解决方案
You missing any code to handle the click, so it does a postback, that no one listens to...
Check on sample here:
LinkButton.OnClick Method (EventArgs) (System.Web.UI.WebControls)[^]
You need to attach your event, .net doesn't intrinsically know which function to call when the link button is clicked.
<asp:Repeater OnItemCommand="rpt_ItemCommand" ....
这篇关于无法从转发器中删除任何行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文