sql c#删除div id的语法

时间:2011-03-30 17:42:14

标签: c# javascript asp.net mysql sql

嘿伙计们,我有一个删除按钮绑定到一些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时输出如下所示:

(萤火虫)

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

}

什么都不做。

3 个答案:

答案 0 :(得分:2)

如果您将div作为控件发送回服务器端......这就是您想要做的事情(如果我错了,请原谅我)

看起来你正在设置divs,这样当你点击它们时......它会在删除之前要求确认?

所以每次点击一个div你都期待回帖?

只是把它扔到那里......但你最好还是使用jquery并进行ajax调用。

<强>更新

您可以使用hiden控件...

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

然后在服务器端,您可以使用

从隐藏控件中获取值
tsId = tsIdValue.value;

当您构建div并调用confirm_delete方法时,您可以执行以下操作:

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

当你点击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;
    }

您仍然必须使用javascript导致回复使用__doPostBack()调用

答案 1 :(得分:2)

因此,当单击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());
                }
            }

答案 2 :(得分:1)

使用ASP.Net(在.net 4.0之前),它会自动将一些内容添加到id的开头,以确保它是唯一的。因此,总之,您将无法完全摆脱这一点,但您可以拆分您的Id的相关部分,以便在您的SQL查询中使用。我会这样做:

 //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];

这会将下划线字符上的Id字符串拆分,然后返回结果数组中的最后一个元素。然后,您可以在SQL查询中使用它。