简体   繁体   English

sql c#删除div id的语法

[英]sql c# delete syntax for div id

Hey guys I have a delete button tied to some Javascript like so: 嗨,我有一个与某些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"/>

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? 我后面的代码的第一部分是动态添加div并将id =添加到idWallPosting,我认为我应该这样做,所以在删除它们时,我要做的就是引用div.ID,但不确定如何添加字符串将div id转换为字符串,以便将其传递给我的sql语法?

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: 必须从html端而不是服务器端获取,在执行时加载div时其输出如下所示:

(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? 我如何剥离ct100 contentplaceholder等,以获取最终编号(26),并将其作为字符串放置在我的删除按钮中?

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. 什么也没做。

If you are sending the div back to the server side as a control.. which is what it looks like you are trying to do (forgive me if I'm wrong) 如果您将div作为控件发送回服务器端..这就是您要尝试执行的操作(如果我错了,请原谅我)

From what it looks like you are setting the divs up so that when you click on them... it asks for confirmation before deletion? 从外观上看,您正在设置div,以便在单击它们时...在删除之前要求确认?

so every time you click on a div you are expecting a post back? 因此,每当您单击div时,您都希望获得回发信息?

Just to throw this out there... but you would be much better off using jquery and making an ajax call. 只是把它扔在那里...但是使用jquery并进行ajax调用会更好。

Update 更新资料

You could possibly use a hiden control... 您可以使用隐藏控件...

<input id="tsIdValue" type="hidden" runat="server" />

then server side you can get the value from your hidden control by using 然后在服务器端,您可以使用以下方法从隐藏控件中获取值:

tsId = tsIdValue.value;

When you build your div and you call the confirm_delete method you might be able to do: 当您构建div并调用confirm_delete方法时,您可以执行以下操作:

div.Attributes.Add("onclick", "return confirm_delete(this.id);"); 

When you click on the div... you would want to set the value of the hidden control to the id of the div via javascript. 当您单击div ...时,您想通过JavaScript将隐藏控件的值设置为div的ID。

 function confirm_delete(id)

    {
      if (confirm("Are you sure you want to delete this comment?")==true)
        tsIdValue.value = id;
        return true;
      else
        return false;
    }

You would still have to have javascript cause the post back using the __doPostBack() call 您仍然需要使用__doPostBack()调用来让javascript导致发回邮件

So when div is clicked you want to delete or you want to delete upon button click only? 因此,当单击div时,您要删除还是只单击按钮就删除?

If it is just button click then you can use CommandArgument to pass your id. 如果只是单击按钮,则可以使用CommandArgument传递您的ID。

If it is div click then again you need to pass the id to your confirm_delete to get refrence. 如果是div,则再次需要将ID传递给您的Confirm_delete以获取参考。

Edit:1 编辑: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. 查看您的代码和注释,别无选择,只能进行ajax回发,因为默认情况下,单击div不会像asp.net按钮那样触发回发。 And you want to execute the Button_Click event of div click which isn't right as well. 而且您想执行div click的Button_Click事件,这也不正确。

Although I don't see a good idea to have delete upon the entire div. 尽管我认为删除整个div并不是一个好主意。 It will be very annoying if users accidentally click on div and the message pops up. 如果用户不小心单击div并弹出消息,将非常烦人。

Edit 2: 编辑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());
                }
            }

With ASP.Net (before .net 4.0) it will auto append some stuff to the beginning of the id to ensure that it is unique. 使用ASP.Net(.net 4.0之前的版本),它将自动在ID的开头附加一些内容,以确保其唯一性。 So in short you won't be able to get rid of this entirely but you can split out the relevant part of your Id for use in you sql query. 简而言之,您将无法完全摆脱这一点,但是您可以将ID的相关部分拆分出来,以便在sql查询中使用。 I would do this like so: 我会这样做:

 //Note this is just for demonstration purposes you will need to actually get the Id of 
 //Id the control here
 string id = "ctl00_ContentPlaceHolder1_ContentPlaceHolder2_26";
 string[] idFragments = id.Split('_');
 id = idFragments[idFragments.Length - 1];

This splits the Id string on the underscore character then returns the last element in the resulting array. 这会在下划线字符上分割Id字符串,然后返回结果数组中的最后一个元素。 You can then use this in your sql query. 然后,您可以在SQL查询中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM