blah blah blah is here! blah blah » Close

up1down
link

in ASP.NET, say we have some code something like this:

protected void RasinBread_Button_Click(object sender, EventArgs e)
{
if (GlobalClass.RemainingStock() == 3)
{
Response.Write("<script>alert('Sorry, there is no more stock left for this product.')</script>");
return;
}
}

say upon the condition is true and it displays that message. I would like to replace the title "Message from webpage" to some other string like "XYZ Error!" or just blank " ". How do I do that?

also, when user clicks the button, and upon the condition is true, the Message Box would be displayed, and the initial web browser page which the user first clicks that button would be blank. I would not want that to happen. I want the initial web browser page to display what it is when the user clicks that button. How do I do that?

thanks.

last answered one year ago

9 answers

link

Ah, I think I can see where the problem lies.

In Page_Load, try changing this line:

if (GlobalClass.RemainingStock() == minimum)

to this:
if (GlobalClass.RemainingStock() <= minimum)

thank you Vulpes very much. It works perfectly.

up1down
link

For security reasons, it's not possible to change the title of a javascript alert box. What appears in the title bar depends on which browser you're using.

It is possible to use a custom pop up box instead (there's a ton of them if you search on google) but IMO it's not worth the hassle - I'd just include the error type in the message itself:

Response.Write(@"<script>alert('XYZ ERROR!\r\n\r\nSorry, there is no more stock left for this product.')</script>");

On the second point, you'll need to avoid using Response.Write to execute the script. One way of doing this would be to add an attribute to Button1 so that the script executes automatically when it's clicked as long as the condition is true.

This can be done by removing all the code from the RasinBread_Button_Click eventhandler and placing the following in your Page_Load eventhandler:
protected void Page_Load(object sender, EventArgs e)
{
int count = Button1.Attributes.Count;

if (GlobalClass.RemainingStock() == 3)
{
if (count == 0)
{
// only add if it's not been added before
Button1.Attributes.Add("onclick", @"alert('XYZ ERROR!\r\n\r\nSorry, there is no more stock left for this product.');");
}
}
else if (count > 0)
{
// if it's been added before, remove it
Button1.Attributes.Clear();
}
}

I'm assuming, of course, that the RemainingStock() method will return an up to date value when the page is loaded, both initially and following each postback.

up0down
link

thank you Vulpes, it is good code. I learn a lot of things from you.
Could I know also what is the purpose of
int count = Button1.Attributes.Count; ? I actually remove that, and it serves my purpose well.

protected void Page_Load(object sender, EventArgs e)
{
GlobalClass.ReadFromBreadTable("1001");
if (GlobalClass.RemainingStock() == 0)
{
RasinBread_Button.Attributes.Add("onclick", @"alert('Zero Stock ERROR!\r\n\r\nSorry, there is no more stock left for this product.');");
}
}

Do I need the last part of clear the button's attributes?
Button1.Attributes.Clear();

vulpes
17279

The reason I added the 'count' variable is because the button's attributes collection is automatically persisted (along with the rest of the button's 'state') between postbacks. So, once the 'onclick' attribute has been added, it will stay there until it's removed. Consequently, I think you'll find that, once the remaining stock has fallen to zero and the attribute has been added, the alert box will continue to be displayed after each subsequent postback even though the stock has been replenished in the meantime.

up0down
link

I have the limitation of putting the code under the Page_Load, as I need the Qty_InsideMachine to be reduced by ONE upon the click on that button, hence I have the code under that button as:

protected void RasinBread_Button_Click(object sender, EventArgs e)
{
MySqlConnection myConn = new MySqlConnection(conn);
MySqlCommand MySqlCmd = new MySqlCommand();
myConn.Open();
GlobalClass.ReadFromBreadTable("1001");
if (GlobalClass.RemainingStock() == 0)
{
RasinBread_Button.Attributes.Add("onclick", @"alert('Zero Stock ERROR!\r\n\r\nSorry, there is no more stock left for this product.');");
return;
}
else
{
MySqlCmd = new MySqlCommand("UPDATE breads SET Qty_InsideMachine = Qty_InsideMachine - 1 WHERE BreadsID = 1001", myConn);
MySqlCmd.ExecuteNonQuery();
}
myConn.Close();
}

However, when I have the above code, once the Qty_InsideMachine == 0, and the user clicks that button, the error message does NOT display out. It is only when the user clicks the button the second time, then the error message comes out. Why is this so?

I still do not have a good understanding of postbacks and persistence for Webforms, I will need more readups. Any good website of such information on it?

up0down
link

If you do it like that then, when Qty_InsideMachine == 0, the RasinBread_Button will have already been clicked and so the attribute you've added won't be effective until the button is clicked again.

What you could do is to leave the code which adds the attribute in Page_Load and then change the code in the RasinBread_Button click eventhandler as follows:

protected void RasinBread_Button_Click(object sender, EventArgs e)
{
GlobalClass.ReadFromBreadTable("1001");
if (GlobalClass.RemainingStock() == 0)
{
// alert box should already have been displayed
return;
}

MySqlConnection myConn = new MySqlConnection(conn);
MySqlCommand MySqlCmd = new MySqlCommand();
myConn.Open();
MySqlCmd = new MySqlCommand("UPDATE breads SET Qty_InsideMachine = Qty_InsideMachine - 1 WHERE BreadsID = 1001", myConn);
MySqlCmd.ExecuteNonQuery();
myConn.Close();
}


ASP.NET is such a complex subject that you really need to buy a book to understand how the plumbing works. However, this article and it's associated links may help you to better understand the page lifecycle and how controls are persisted between postbacks using view state.

up0down
link

as what you have suggested, in the code below, the alert message comes out ONLY when the user clicks the SECOND time on the button, when the condition of Qty_InsideMachine = 0 is already true when the user clicks the button the FIRST time. I would want the alert message to come out upon the FIRST click once the condition Qty_InsideMachine = 0 is true.

protected void Page_Load(object sender, EventArgs e)
{
int count = RasinBread_Button.Attributes.Count;
GlobalClass.ReadFromBreadTable("1001");
if (GlobalClass.RemainingStock() == 0)
{
if (count == 0)
{
// only add if it's not been added before
RasinBread_Button.Attributes.Add("onclick", @"alert('XYZ ERROR!\r\n\r\nSorry, there is no more stock left for this product.');");
}
}
else if (count > 0)
{
// if it's been added before, remove it
RasinBread_Button.Attributes.Clear();
}
}

protected void RasinBread_Button_Click(object sender, EventArgs e)
{
GlobalClass.ReadFromBreadTable("1001");
if (GlobalClass.RemainingStock() == 0)
{
return;
}
MySqlConnection myConn = new MySqlConnection(conn);
MySqlCommand MySqlCmd = new MySqlCommand();
myConn.Open();
MySqlCmd = new MySqlCommand("UPDATE breads SET Qty_InsideMachine = Qty_InsideMachine - 1 WHERE BreadsID = 1001", myConn);
MySqlCmd.ExecuteNonQuery();
myConn.Close();
}

up1down
link

Hmm, I think the problem is that when the button is pressed Page_Load is running again BEFORE the code in the Click eventhandler and so the code which adds the attribute is getting out of step because the Qty_InsideMachine variable has not yet been decremented.

The only way I can think of to fix this is to anticipate the decrement and therefore add the attribute in Page_Load when Qty_InsideMachine is 1 if it's a postback but 0 otherwise.

So, try changing the code in Page_Load to the following. No change should be needed to the code in the Click handler:

protected void Page_Load(object sender, EventArgs e)
{
int count = RasinBread_Button.Attributes.Count;
int minimum = IsPostBack ? 1 : 0;
GlobalClass.ReadFromBreadTable("1001");

if (GlobalClass.RemainingStock() == minimum)
{
if (count == 0)
{
// only add if it's not been added before
RasinBread_Button.Attributes.Add("onclick", @"alert('XYZ ERROR!\r\n\r\nSorry, there is no more stock left for this product.');");
}
}
else if (count > 0)
{
// if it's been added before, remove it
RasinBread_Button.Attributes.Clear();
}
}

up0down
link

yes, it works. Thanks so much.
When Qty_InsideMachine = 0, it displays the alert message, upon the FIRST click of that button, which is what I want.
Could the code be further improved such that on subsequent clicks of that button (after the FIRST click), the alert message would also appear?

vulpes
17279

Well, as long as Qty_InsideMachine == 1 when the page loads (and so will be 0 when the button click handler runs), the alert message should still pop up on subsequent clicks. Is this not in fact what you're seeing?

up0down
link

No, this is NOT the fact I am seeing. When Qty_InsideMachine ==0, and I click the RasinBread button, the alert message comes out. After I click the OK button in the alert message, and then click the RasinBread button, that alert message does not come out again.

Feedback