Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Button-OnClientClick

Status
Not open for further replies.

dvimm

Programmer
Apr 26, 2007
23
US
Hello,
I have a textbox and button . when I click the button, it should check if the record related to textbox is in the database, and if not, it should pop up the message and ask if we want to add the record to database or not. we get yes/no from the pop up and do chain of events accordingly.
need to do something like

button_click
{
check if record exists
if not exists
ask for confirmation and
add if the confirmation is yes.
}

I have javacript code in .net

private void jsConfirmAdd()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script language=\"javascript\" type=\"text/javascript\">" + Environment.NewLine);
sb.Append("function ConfirmLoanAdd()" + Environment.NewLine);
sb.Append("{" + Environment.NewLine);
//sb.Append(" var confirm=window.confirm('This Loan doesnt exist.Do you want to add?');" + Environment.NewLine);
sb.Append("if (confirm('This Loan doesnt exist.Do you want to add?'))" + Environment.NewLine);
sb.Append("{" + Environment.NewLine);
sb.Append("return true;" + Environment.NewLine);
sb.Append("}" + Environment.NewLine);
sb.Append("else" + Environment.NewLine);
sb.Append("{" + Environment.NewLine);
sb.Append("return false;" + Environment.NewLine);
sb.Append("}" + Environment.NewLine);
sb.Append("}" + Environment.NewLine);
sb.Append("</script>" + Environment.NewLine);

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "validation", sb.ToString());
btnSearch.OnClientClick = "return ConfirmLoanAdd()";
}


and I have btnclick event

at what point do i add value to onclientclick so that message box in above function pops up?
In other words do click and onclientclick go together. By the time i want to call the js function, i already clicked the button.

Please advice the better way to do this.
Thanks
 
here is the life cycle of your request is classic asp.net.
1. user requests the page
2. server renders page for user
3. user enters iformation and submits
4. server gets request, checks if record exists.
4.1. if record does exists, goto step 5.
4.2. if record does not exist append JS to page to get user response.
4.2.1. if user says no do nothing. if user says yes send request back to server.
4.2.2. if user says yes insert data into database.
5. render page.

This is much easier with a desktop app because you can keep the state of the object. there are no round trips from the gui to the code behind.

Remeber webpages are stateless so once a request is completed all knowledge of the request is discarded by the server.

there are a few ways to work around this, all of them require moderate coding.
1. use html controls instead of javascript on post back to ask save yes/no. this is actually easier then dealing with javascript. if used in conjunction with MS AJAX this would give the appearance of a desktop like functionality.
2. use a webservice to save/confirm actions so your not posting the page back and forth.
3. add a checkbox so the user decides whether to add the record or not before posting back. This doesn't require AJAX and elminates round trips to the server.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks ca8msm and Jason. I will try those.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top