【问题标题】:Javascript wont do a postback from c# code in asp.netJavascript 不会从 asp.net 中的 c# 代码进行回发
【发布时间】:2011-07-26 13:28:34
【问题描述】:

Ok 设法让代码正常工作,因为没有错误,但在我点击 div 时确认后它不会回复?:

<script type="text/javascript">
    function confirm_delete(id){
        if (confirm('Are you sure you want to delete?')) {
            __doPostBack('DivClicked', id);
        }
        else {
            return false;
        }
    }

后面的代码:

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

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            //It is a postback so check if it was by div click (NOT WORKING because the javascript isnt posting back)
            string target = Request["__EVENTTARGET"];
            if (target == "DivClicked")
            {
                string id = Request["__EVENTARGUMENT"];
                //Call my delete function passing record id
                Response.Write(String.Format(id)); //just a test 
            }
        }
        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));
                        string id = Convert.ToString(div.ID);
                        //store the div id as a string
                        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(" + id + ");");
                        // send the div id to javascript
                        div.Style["clear"] = "both";
                        test1.Controls.Add(div);

                    }
                }
            }
        }
    }



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

代码不执行回发的任何原因?

【问题讨论】:

  • 你确定confirm函数返回true吗?
  • 没有影响
  • “此代码未执行回发”是指“有帖子但 Page.IsPostBack 为假”?
  • 好吧,我的意思是什么也没发生,我点击确定,没有响应被触发(页面加载),就像正常的回发一样,我得到了一个页面重新加载
  • 如果您重新加载页面,那么您不能说没有任何反应。发生了一些事情,页面正在重新加载。未正确引发 Page_Load 事件与未回发的页面不同。那么页面重新加载了吗?

标签: c# javascript asp.net html


【解决方案1】:

问题是因为__doPostBack 没有定义。它没有被定义,因为你没有在你的页面上放置任何控件,它是一个 IPostBackEventHandler,就像 LinkButton 一样。

为了使Page.IsPostBack 为真,您必须:

  • 重新构建您的应用程序,以便您实现一个实现 IPostBackEventHandler 的控件,当然,将它放在您的页面上
  • 你在你的页面上放了一个虚拟的IPostBackEventHandler。一个什么都不做的人。喜欢LinkButton

无论哪种方式,ASP.NET 都会在客户端定义 __doPostBack 函数,您的代码最终会回发

【讨论】:

  • woooooHOOOOO 现在一切正常!我什至得到我的回应! 27!哇,这是我单击的评论的 div id 天才的东西是链接按钮,为什么它不同?为什么它持有 IPostback 事件处理程序?不知道那到底是什么以及为什么 java 连接到它
  • 这是因为 ASP.NET 架构会在生命周期中搜索实现 IPostBackEventHandler 的控件,以确定是否声明 __doPostBack
猜你喜欢
  • 1970-01-01
  • 2017-06-19
  • 2013-09-25
  • 2010-09-21
  • 2011-06-28
  • 1970-01-01
  • 2012-12-09
  • 2012-01-04
  • 1970-01-01
相关资源
最近更新 更多