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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Look for Javascript code in ASP.NET

Status
Not open for further replies.

NMDUC

Programmer
Mar 4, 2006
61
VN
Hi,
I have a textbox and a button. When user clicks button, it will popup a window asking "Do you want to saving it?". If yes, it will go on to store the data to the database. Please tell me how to generate the Javascript code like this in ASP.NET. Thank in advance.
 
What have you tried so far?

ASP.NET doesn't have to mean anything here. You want some client functionality in your pages and that is it. Try not thinking so hard about it.

google confirm save javascript

Any javascript problems should be sent to forum216

[sub]____________ signature below ______________
You may get help by means of code here. Just recall what happens to a rat when he eats the cheese off a rat trap while you're Ctrl+C/Ctrl+V'ing that code as your own[/sub]
 
if your using asp.net 2.0 you can use the ClientClick property to asign javascript if not you can assign an onclick attribute to the button

clientclick
Code:
//aspx
<asp:Button id="myButton" ... ClickClick="javascript: return confirm('Are you sure?');" />

//or code behind
myButton.ClientClick = "javascript: return confirm('Are you sure?');";

attribute
Code:
myButton.Attributes.Add("onclick", "javascript: return confirm('Are you sure?');"(;



Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank. But I want to do something before generating popup when user clicks such as checking the bad words... Thus, using ClientClick property might not be suitable.
 
NMDUC said:
Thank. But I want to do something before generating popup when user clicks such as checking the bad words...
??? spell checking has nothing to do with confirming an action. (your original post)

maybe I wasn't clear.
you set the ClientClick property/onclick attribute before the button is rendered. this usally happens in the Page.Load or GridView.ItemDataBound event. (same goes for Form and Repeater view controls)

if you want something else to happen client side before confirming the action place the javascript call before [tt]return confirm();[/tt]
it would look somethin like this
Code:
<asp:Button id="myButton" ... ClientClick="javascript: [put other JS function calls here;] return confirm('Are you sure?');" />

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Spell checking is just an example.

In the code that you gave me

<asp:Button id="myButton" ... ClientClick="javascript: [put other JS function calls here;] return confirm('Are you sure?');" />

To use it, the text box must be HTML control so that JS function can access it, right?

If I use ASP.NET's text box, our code can not use in the server side.
 
asp.net builds html. so a System.Web.UI.WebControls.Button will become a <input type="submit" /> when the page is rendered and vice verca with a request is posted back.

It sounds like your new to programming for the web or at least asp.net. If I'm mistaken I apologize.
with web applications the action either takes place on the client (javascript, html) or the server (.net). AJAX blurs this seperation, but it still exists. All events must start with the client. the server cannot respond to the client without a request.

it may help if we fully understand what you are trying to do and where (client/server) those actions occur. Could you provide an outline of how you want this process to work?




Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi jmeckley,
You are right. I am quite new to programming. I will explain later what I want to build.
I have a textbox and a button. When user clicks button, I will do something on the server side such as input validation. Then, it will popup a window asking "Do you want to saving it?". If yes, it will go on to store the data to the database.
Most of these actions are on the server side.

Button_Click
{
-input validation.
-....
-pop up Do you want to saving it?
-If yes, save it to DB.
}
 
ok, once you post back to the server you will need to return to the client to confirm (could be a javascript yes/no, or a lable with 2 buttons yes/no.

the simplest solution would probally be to use a Wizard (if your using asp.net 2.0)

step 1 enter data
on next step event (and step index = 1) validate data
step 2 confirm you want to save (finish step)
on complete save to database and either return to previous page, or show a completed page stating the data was saved.

web development and desktop development are very different. with desktop apps you have state information with technically the web is stateless so once information leaves the client or server all information about what just happened is lost. asp.net fudges this with view state, session and cache.

the fewer round trips from client to server the better because the user believes things are happening faster. AJAX is a buzz word you'll see alot of with web development. a blessing and a curse. MS created the MS AJAX Library which makes AJAX development easier.

do some research on the page life cycle this will be invaluable for future development. It may not click right away, but keep reading. Many pitfalls can be avoided with a base knowledge of the page life cycle.

and if you have any more questions post them. also check out the and forums.asp.net. I find tek-tips has a better response time, but asp is good when you have a very technical question since MS developers are always on.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top