后台 protected void Relist_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Message mess = e.Item.DataItem as Message;
Button btn = e.CommandArgument as Button;
if (btn != null)
{
if(e.CommandName == "Edit")
{
Response.Redirect("Test.aspx");
}
if (e.CommandName == "Delete")
{
string connectionString = WebGlobals.Config.DefaultConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
string del = String.Format("Delete * from [message] where title='{0}'", mess.Title);
SqlCommand command = new SqlCommand(del, connection);
connection.Open(); command.ExecuteReader();
connection.Close();
}
}
}
}
前台:
<asp:Button ID="Edit" runat="server" OnClientClick="Edit" CommandArgument='<%# Eval("ID") %>' CommandName="Edit" Text="编辑" />
<asp:Button ID="Delete" runat="server" OnClientClick="Delete" CommandArgument='<%# Eval("ID") %>' CommandName="Delete" Text="删除" />运行时提示: Microsoft JScript 运行时错误: 'Edit' 未定义 'Delete' 未定义请问我哪儿出错了呢?
{
Message mess = e.Item.DataItem as Message;
Button btn = e.CommandArgument as Button;
if (btn != null)
{
if(e.CommandName == "Edit")
{
Response.Redirect("Test.aspx");
}
if (e.CommandName == "Delete")
{
string connectionString = WebGlobals.Config.DefaultConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
string del = String.Format("Delete * from [message] where title='{0}'", mess.Title);
SqlCommand command = new SqlCommand(del, connection);
connection.Open(); command.ExecuteReader();
connection.Close();
}
}
}
}
前台:
<asp:Button ID="Edit" runat="server" OnClientClick="Edit" CommandArgument='<%# Eval("ID") %>' CommandName="Edit" Text="编辑" />
<asp:Button ID="Delete" runat="server" OnClientClick="Delete" CommandArgument='<%# Eval("ID") %>' CommandName="Delete" Text="删除" />运行时提示: Microsoft JScript 运行时错误: 'Edit' 未定义 'Delete' 未定义请问我哪儿出错了呢?
CommandName="Edit"
这两个事件没有在后台定义啊
CommandName="Edit"
这两个删掉吧
很明显是JS错误,你删后台函数搞什么飞机~
你要控制什么嗫?
后台只要你有写 CommandName="Edit" 这一句就可以
<asp:Button ID="Edit" runat="server" OnClientClick="Edit" CommandArgument=' <%# Eval("ID") %>' CommandName="Edit" Text="编辑" />
这是在热评repeater里的一个按钮,我想通过这个编辑页面,e.CommandName == "Edit" 这样不可以选定吗?
<asp:Button ID="Edit" runat="server" OnClientClick="Edit" CommandArgument=' <%# Eval("ID") %>' //这里必需直接指定 CommandName="Edit" Text="编辑" />
Edit方法未定义,JS错误!
删除了OnClientClick="Edit"
和OnClientClick="delete"
没有报错了。
但是却无法完成编辑和删除功能,请各位高手指明。
把这段代码复制过去
<asp:Button ID="Edit" runat="server" CommandArgument=' <%# Eval("ID") %>' CommandName="Edit" Text="编辑" />
<asp:Button ID="Delete" runat="server" CommandArgument=' <%# Eval("ID") %>' CommandName="Delete" Text="删除" />
应修改为 Button btn = e.CommandSource as Button;
这样才能获取数据源,btn才能不为空,特此结贴。后台 protected void Relist_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Message mess = e.Item.DataItem as Message;
Button btn = e.CommandArgument as Button;
if (btn != null)
{
if(e.CommandName == "Edit")
{
Response.Redirect("Test.aspx");
}
if (e.CommandName == "Delete")
{
string connectionString = WebGlobals.Config.DefaultConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
string del = String.Format("Delete * from [message] where title='{0}'", mess.Title);
SqlCommand command = new SqlCommand(del, connection);
connection.Open(); command.ExecuteReader();
connection.Close();
}
}
}
}
前台:
<asp:Button ID="Edit" runat="server" CommandArgument=' <%# Eval("ID") %>' CommandName="Edit" Text="编辑" />
<asp:Button ID="Delete" runat="server" CommandArgument=' <%# Eval("ID") %>' CommandName="Delete" Text="删除" />