如何做回发 Javascript、jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5491150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:25:13  来源:igfitidea点击:

how to do postback Javascript, jquery

javascriptjqueryasp.net

提问by G Gr

<asp:Button ID="btn" OnClientClick="if(confirm_delete()){
/* post back*/
}else{
return false;
};" OnClick="btnDelete_Click" runat="server" Text="delete"/>

Hi I have this code but I cant do postback for it, im not sure how to?

嗨,我有这个代码,但我不能为它做回发,我不知道怎么做?

is it:

是吗:

<script type="text/javascript"> 
        function CallServer() {
            __doPostBack('not sure what goes here','or here');
        }  
</script>

Then:

然后:

<asp:Button ID="btn" OnClientClick="if(confirm_delete()){
/CallServer()/
}else{
return false;
};" OnClick="btnDelete_Click" runat="server" Text="delete"/>

My other script:

我的另一个脚本:

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

EDIT:

编辑:

On the server side i dynamically add a div to my page with content from my database for each content there is a new div will be added, each div is then refrenced with idWallPosting (so i can call my delete function)

在服务器端,我将一个 div 动态添加到我的页面,其中包含来自我的数据库的每个内容的内容,将添加一个新的 div,然后使用 idWallPosting 引用每个 div(因此我可以调用我的删除函数)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using System.IO;

public partial class UserProfileWall : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //btn.Visible = false;
        string theUserId = Session["UserID"].ToString();
        PopulateWallPosts(theUserId);
    }
    private void PopulateWallPosts(string userId)
    {

        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("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))
            {
                //("SELECT 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 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));
                        // this line is responsible, problem here and my sqlsntax, im trying to set the SELECT idWallPosting for the div ID
                        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)
    //{

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

    //}

    protected void Button1_Click(object sender, EventArgs e)
    {
        string theUserId = Session["UserID"].ToString();
        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("INSERT INTO WallPosting (UserID, Wallpostings) VALUES (" + theUserId + ", '" + TextBox1.Text + "')", cn))
            {
                cmd.ExecuteNonQuery();
            }
        }
        PopulateWallPosts(theUserId);
    }
    protected void btn_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();
    }
}

On my asp.net html side i have:

在我的 asp.net html 方面,我有:

<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="return confirm_delete();" runat="server" 
        CssClass="Btn" Text="delete" onclick="btn_Click"/>
    <asp:TextBox ID="TextBox1" name="TextBox1" runat="server" Rows="3" 
        Height="47px" Width="638px"></asp:TextBox>
</p>
<p>
     <asp:Button ID="Button1" runat="server" Text="Post Message" Width="98px" 
        onclick="Button1_Click" />
    </p>
<p>
</p>
    <style type="text/css">
    img {border-width:0px; width:100px; height:100px;}
</style>
    <div id="test1" runat="server" />

    </div>

</asp:Content>

If you notice in my server side code I added this line:

如果您在我的服务器端代码中注意到我添加了这一行:

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

This works any time I click on my div the confirm_deleteis called.

每当我点击我的 div 时,这都会起作用confirm_delete

What I was trying to do with my asp.net button was when the div was clicked I could then call the onclick btnDelete_click.

我试图用我的 asp.net 按钮做的是当点击 div 时,我可以调用onclick btnDelete_click.

回答by Tim Schmelter

OnClientClick="return confirm_delete();"

That's it...

就是这样...

Edit: __doPostBack works also...

编辑:__doPostBack 也适用...

OnClientClick="if(confirm('delete?'))__doPostBack('btn',''); else return false;"

回答by Dave Rager

If you really are wanting to manually call __doPostBack(), the first parameter is the .NET generated name for the control. This can be gotten on the server side using Control.ClientID. The second parameter is any extra data that should be passed along in the request. Most of the time I see this field is an empty string.

如果您确实想手动调用__doPostBack(),第一个参数是 .NET 为控件生成的名称。这可以在服务器端使用Control.ClientID. 第二个参数是应该在请求中传递的任何额外数据。大多数时候我看到这个字段是一个空字符串。

__doPostBack('ctl100$controlName$id','');

The controlNameis the .NET class name of the control I believe, idis the ID you gave the control. To be sure, view the source of the page after it has been rendered in the browser and search for calls to __doPostBackand see how they are formatted.

controlName是我相信的控件的 .NET 类名,id是你给控件的 ID。可以肯定的是,在浏览器中呈现页面后查看页面的源代码并搜索对的调用__doPostBack并查看它们的格式。

回答by Rion Williams

By a postback in this case do you want to just refresh the page? If so then it would just be:

在这种情况下,通过回发您只想刷新页面吗?如果是这样,那么它只会是:

    location.reload();

in your case:

在你的情况下:

    <script type="text/javascript">
        function CallServer() 
        {
               location.reload();
        }
    </script>

Demo(A button click prompts the user to confirm - if they choose Yes, a post back occurs)

演示(单击按钮会提示用户确认 - 如果他们选择是,则会发生回发)

See demo here!

在这里查看演示!

回答by Adi

One method, not the best for sure: Add a button into an update panel and set it invisble. Then call click() method of the button.

一种方法,但不是最好的方法:在更新面板中添加一个按钮并将其设置为不可见。然后调用按钮的 click() 方法。

Somthing like this:

像这样的东西:

document.getElementById('button').click();