gpt4 book ai didi

c# - sql c# 删除 div id 的语法

转载 作者:太空宇宙 更新时间:2023-11-03 10:26:00 24 4
gpt4 key购买 nike

大家好,我有一个删除按钮绑定(bind)到一些 Javascript,如下所示:

    <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("&nbsp&nbsp&nbsp;" + "{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 并将 id = 添加到 idWallPosting 我想我应该这样做,所以当要删除它们时,我所要做的就是引用 div.ID 但我不确定如何添加一个字符串以将 div id 转换为字符串,以便我可以将其传递给我的 sql 语法?

这必须从 html 端获取,而不是从服务器端获取,当执行时加载 div 时,其输出看起来像这样:

( Firebug )

<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传递你的身份证。

如果它是 div click,那么您需要再次将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com