嗨,我有一个与某些Java脚本绑定的删除按钮,如下所示:
<script type="text/javascript">
function confirm_delete()
{
if (confirm("Are you sure you want to delete this comment?")==true)
return true;
else
return false;
}
</script>
<p>
<asp:Button ID="btn" OnClientClick="if(confirm_delete()){/* post back*/}else{return false;};" OnClick="btnDelete_Click" runat="server" Text="delete"/>
在后面的代码中,我有这个:
using (OdbcCommand cmd = new OdbcCommand("SELECT idWallPosting, wp.WallPostings, p.PicturePath FROM WallPosting wp LEFT JOIN User u ON u.UserID = wp.UserID LEFT JOIN Pictures p ON p.UserID = u.UserID WHERE wp.UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
{
using (OdbcDataReader reader = cmd.ExecuteReader())
{
test1.Controls.Clear();
while (reader.Read())
{
System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div.Attributes["class"] = "test";
div.ID = String.Format("{0}", reader.GetString(0));
// the Div ID is set to my idWallPosting in my WallPosting Table
Image img = new Image();
img.ImageUrl = String.Format("{0}", reader.GetString(2));
img.AlternateText = "Test image";
div.Controls.Add(img);
div.Controls.Add(ParseControl(String.Format("   " + "{0}", reader.GetString(1))));
div.Attributes.Add("onclick", "return confirm_delete();");
div.Style["clear"] = "both";
test1.Controls.Add(div);
}
}
}
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
//serverside code if confirm was pressed to delete from WallPosting table based on Div ID.
}
我动态地在后面的代码的第一部分添加了div并向idWallPosting添加了id =,我想我应该这样做,所以在删除它们时,我要做的就是引用div.ID,但不确定如何添加字符串将div id转换为字符串,以便可以将其传递给我的sql语法?
必须从html端而不是服务器端获取,在执行时加载div时其输出如下所示:
(萤火虫)
<div id="ctl00_ContentPlaceHolder1_ContentPlaceHolder2_26"><div>
我将如何剥离ct100 contentplaceholder等,以获取最终编号(26)并将其作为字符串放置在我的删除按钮中?
编辑:(帕特里克)
protected void btnDelete_Click(object sender, EventArgs e)
{
idWallPosting = Div.ID;
//serverside code if confirm was pressed.
using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand("DELETE FROM WallPosting WHERE idWallPosting = "+idWallPosting+")", cn))
{
cmd.ExecuteNonQuery();
}
}
PopulateWallPosts(); //error here
}
编辑:
protected void btnDelete_Click(object sender, EventArgs e)
{
string id = "ctl00_ContentPlaceHolder1_ContentPlaceHolder2_26";
string[] idFragments = id.Split('_');
id = idFragments[idFragments.Length - 1];
//serverside code if confirm was pressed.
using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand("DELETE FROM WallPosting WHERE idWallPosting = " + id + ")", cn))
{
cmd.ExecuteNonQuery();
}
}
//PopulateWallPosts();
}
什么也没做。
最佳答案
因此,当单击div时,您要删除还是只单击按钮就删除?
如果只是单击按钮,则可以使用CommandArgument传递您的ID。
如果是div,则再次需要将ID传递给您的Confirm_delete以获取参考。
编辑:1
查看您的代码和注释,除了做ajax postback之外,别无其他方法,因为默认情况下,单击div不会像asp.net按钮那样触发回发。而且您想执行div click的Button_Click事件,这也不正确。
尽管我认为删除整个div并不是一个好主意。如果用户不小心单击div并弹出消息,将非常烦人。
编辑2:
如果有帮助,请参考以下示例:
<div id="testdiv" runat="server">Hello world</div>
//Pass record id to your confirm_delete
int id = 10;
testdiv.Attributes.Add("onclick", "return confirm_delete(" + id.ToString() + ");");
//Updated confirm_delete function
<script type="text/javascript">
function confirm_delete(id){
if (confirm('Are you sure you want to delete?')) {
__doPostBack('DivClicked', id);
}
else {
return false;
}
}
</script>
//This goes in your Page_Load code-behind
if (Page.IsPostBack)
{
//It is a postback so check if it was by div click
string target = Request["__EVENTTARGET"];
if (target == "DivClicked")
{
string id = Request["__EVENTARGUMENT"];
//Call you delete function passing record id
Response.Write(id.ToString());
}
}
关于c# - sql C#删除div id的语法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5489991/