sql c#删除div id的语法 [英] sql c# delete syntax for div id

查看:40
本文介绍了sql c#删除div id的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我有一个与某些 Javascript 相关联的删除按钮,如下所示:

 <p><asp:Button ID="btn" OnClientClick="if(confirm_delete()){/* 回传*/}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)){使用 (OdbcDataReader reader = cmd.ExecuteReader()){test1.Controls.Clear();而 (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));//在我的 WallPosting 表中将 Div ID 设置为我的 idWallPostingImage img = new Image();img.ImageUrl = String.Format("{0}", reader.GetString(2));img.AlternateText = "测试图片";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){//服务器端代码,如果按下确认从基于 Div ID 的 WallPosting 表中删除.}

动态背后的代码的第一部分添加 div 并将 id = 添加到 idWallPosting 我想我应该这样做,所以当要删除它们时,我所要做的就是引用 div.ID 但我不确定如何添加一个字符串以将 div id 转换为字符串,以便我可以将其传递给我的 sql 语法?

这必须从 html 端获取,而不是从服务器端获取,当执行加载 div 时,其输出如下所示:

(萤火虫)

我将如何去除 ct100 contentplaceholder 等以获取最后的编号 (26) 并将其作为字符串放在我的删除按钮中?

(帕特里克)

protected void btnDelete_Click(object sender, EventArgs e){idWallPosting = Div.ID;//服务器端代码,如果确认被按下.使用 (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();//这里出错}

protected void btnDelete_Click(object sender, EventArgs e){string id = "ctl00_ContentPlaceHolder1_ContentPlaceHolder2_26";string[] idFragments = id.Split('_');id = idFragments[idFragments.Length - 1];//服务器端代码,如果确认被按下.使用 (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=gymwebsite2;User=root;Password=commando;")){cn.Open();使用 (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 按钮那样默认触发 postback.并且你想执行 div click 的 Button_Click 事件,这也是不对的.

虽然我认为在整个 div 上删除不是一个好主意.如果用户不小心点击了 div 并弹出消息,那将是非常烦人的.

编辑 2:

如果有帮助,这里是一个示例:

<div id="testdiv" runat="server">Hello world</div>//将记录ID传递给您的confirm_deleteint id = 10;testdiv.Attributes.Add("onclick", "return confirm_delete(" + id.ToString() + ");");//更新了confirm_delete函数<script type="text/javascript">函数confirm_delete(id){if (confirm('你确定要删除吗?')) {__doPostBack('DivClicked', id);}别的 {返回假;}}//这在你的 Page_Load 代码隐藏中如果 (Page.IsPostBack){//这是一个回发所以检查它是否是通过div点击string target = Request["__EVENTTARGET"];如果(目标==DivClicked"){string id = Request["__EVENTARGUMENT"];//调用你的删除函数传递记录IDResponse.Write(id.ToString());}}

Hey guys I have a delete button tied to some Javascript like so:

    <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"/>

In the code behind I have this:

      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.

}

The first part of the code behind dynamicallys adds divs and adds the id = to idWallPosting I thought I should do this so when it came to delete them, all I had to do was refrence the div.ID but im unsure how to add in a string to convert the div id to string so I can pass that to my sql syntax?

This has to be taken from the html side not from server side the output of which looks like this when the divs are loaded on execution:

(firebug)

<div id="ctl00_ContentPlaceHolder1_ContentPlaceHolder2_26"><div>

How would I strip of the ct100 contentplaceholder etc etc to get the very end number (26) and place that in my delete button as a string?

EDIT: (Patrick)

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

}

Edit:

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();

}

Does nothing.

解决方案

So when div is clicked you want to delete or you want to delete upon button click only?

If it is just button click then you can use CommandArgument to pass your id.

If it is div click then again you need to pass the id to your confirm_delete to get refrence.

Edit:1

Looking at your code and comments there is no other way but to do an ajax postback because clicking on div isn't going to trigger postback by default like asp.net button. And you want to execute the Button_Click event of div click which isn't right as well.

Although I don't see a good idea to have delete upon the entire div. It will be very annoying if users accidentally click on div and the message pops up.

Edit 2:

Here is a sample if that helps:

<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());
                }
            }

这篇关于sql c#删除div id的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆