Reputation: 6080
Can any one help me with this code:
ASP:
<script type="text/javascript">
function confirm_delete(div.ID)// problem here{
if (confirm('Are you sure you want to delete?')) {
__doPostBack('DivClicked', div.ID);
// not sure if javascript will pick up on the string from server side code
}
else {
return false;
}
}
Page Load code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
//It is a postback so check if it was by div click
string target = Request["__EVENTTARGET"];
if (target == "DivClicked")
{
String.Format(div.ID) = Request["__EVENTARGUMENT"];
// problem converting div.ID from Javascript to string (because of the dot)
Response.Write(String.Format(div.ID));
// same problem here
}
}
string theUserId = Session["UserID"].ToString();
PopulateWallPosts(theUserId);
}
Code Behind:
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));
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(" + div.ID + ");");
// send the div id to javascript, not sure this line will work
div.Style["clear"] = "both";
test1.Controls.Add(div);
}
}
}
}
}
Upvotes: 0
Views: 869
Reputation: 5084
Garrith,
Just like with your last question you want your javascript to look like:
<script type="text/javascript">
function clickTheDiv() {
var Sender = window.event.srcElement;
if(confirm("are you sure?"))
{
__doPostBack('DivClicked', Sender.ID)
}
}
</script>
...and for your div just do this:
div.Attributes.Add("onclick", "clickTheDiv();"
//(with all your other work around this)
You should be able to work out the rest of what you need here.
Cheers,
CEC
Upvotes: 2
Reputation: 532435
There are quite a few problems with your code. First, the argument to a javascript function must be a legal variable name. You are using a property reference on a variable. Second, I suspect that you may be using numeric ids for your DIV ids. Note that they have to start with a letter and must be unique. It's possible yours are this way, but given that they seem to refer to comment ids it seems likely that they may be fully numeric. Third, you probably should be passing back the id of the control in the __EVENTTARGET parameter, that's what the frame work expects. Fourth, just return false if it isn't confirm. That will cancel the normal action. If it is confirmed, you're doing the postback manually and still want the normal action cancelled. Note that it is a full postback, not AJAX so you should return a full page. Fifth, you need to put quotes around the id when you write out the javascript. Lastly, there are much better ways to do this, but it would take more than just one answer to show you how. Look for information on using AJAX with web forms.
ASPX
function confirm_delete(id) {
if (confirm('Are you sure you want to delete?')) {
__doPostBack(id,'');
}
return false;
}
Page Load
if (Page.IsPostBack)
{
string target = Request["__EVENTTARGET"];
if (target.StartsWith("comment"))
{
// remove comment string to get actual id
string id = target.Replace( "comment", "" );
// now do something with the id, like delete the comment?
}
}
// finish the page
string theUserId = Session["UserID"].ToString();
PopulateWallPosts(theUserId);
Code-behind
while (reader.Read())
{
var div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div.Attributes["class"] = "test";
div.ID = String.Format("comment{0}", reader.GetString(0));
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))));
// the single quotes in the line below are critical
div.Attributes.Add("onclick", "return clickTheButton('" + div.ID + "');");
div.Style["clear"] = "both";
test1.Controls.Add(div);
Upvotes: 1
Reputation: 6080
Javascript wont do a postback from c# code in asp.net
The answer to this question:
<script type="text/javascript">
function confirm_delete(id){
if (confirm('Are you sure you want to delete?')) {
__doPostBack('DivClicked', id);
}
else {
return false;
}
}
</script>
code behind
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);
}
}
Upvotes: 0