JavaScript的不会做在asp.net C#code回发 [英] Javascript wont do a postback from c# code in asp.net
问题描述
好吧设法得到code工作,如没有错误,但它不会做一个帖子回来后香港专业教育学院证实,当我点击的div:
Ok managed to get the code working, as in no errors but it wont do a post back after ive confirmed when I click on the div?:
<script type="text/javascript">
function confirm_delete(id){
if (confirm('Are you sure you want to delete?')) {
__doPostBack('DivClicked', id);
}
else {
return false;
}
}
背后code:
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("   " + "{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);
}
}
任何理由为什么code心不是执行回发?
Any Reason why the code isnt executing a postback?
推荐答案
现在的问题是,因为 __ doPostBack
没有定义。这不是定义的,因为你没有把你的页面上的任何控件是一个 IPostBackEventHandler
,如的LinkButton
是。
The problem is because __doPostBack
is not defined. It's not defined because you didn't put any control on your page that is a an IPostBackEventHandler
, like LinkButton
is.
在为 Page.IsPostBack
是真实的,你必须要么:
In order to Page.IsPostBack
to be true, you have to either:
- 重新archtecture您的应用程序,让你实施控制,实现
IPostBackEventHandler
,当然,把它放在你的网页上 - 您把一个虚拟的
IPostBackEventHandler
您的网页上。一个不会做任何事情。像的LinkButton
。
- Re-archtecture your application so that you implement a control that implements
IPostBackEventHandler
and, of course, put it on your page - You put a dummy
IPostBackEventHandler
on your page. One that doesn't do anything. Like aLinkButton
.
无论哪种方式,ASP.NET将确定在客户端上的 __ doPostBack
功能,您的code将最终回传
Either way ASP.NET will define the __doPostBack
function at client side and your code will finally postback
这篇关于JavaScript的不会做在asp.net C#code回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!