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

Autopostback on DDL not allowing javascript

Status
Not open for further replies.

beachboy58

IS-IT--Management
Jan 4, 2011
3
0
0
US
I am new to the forums and have a problem that has been nagging me. I've been searching high and low and can't find a good answer.

I have a dropdown list that signifies the status of an order. I have a condition to check when a certain status is selected that I want a javascript alert window to pop up to remind the user to check for a certain document.

It is written like this
ClientScript.RegisterStartupScript(Page.GetType(), "ErrorAlert", "alert('This is Clay County, remember to include CITY TAXES');", True)

This never gets executed because of the autopostback on the DDL. How can I make sure this popup window appears before the postback happens.

Thanks in advance
 
ClientScript.RegisterStartupScript registers a page's startup script. What I think you want to do is create a javascript function to show the alert, then attach that function to the dropdown list's onChange event.

Try this:

In the page's Page_Load event, put the following code:

DropDownList1.Attributes.Add("onChange", "CheckValue(this.form.DropDownList1);")

Note: use the actual ID of your dropdown list in place of DropDownList1. Also, use the actual name of your javascript function (I used CheckValue).

Next, put the javascript function between the <head></head> tags:

<script type="text/javascript">

function CheckValue(dropdown)
{

var SelIndex = dropdown.selectedIndex;
var SelText = dropdown.options[SelIndex].text;

if(SelText=="2")
{
alert("Are you sure?");
}


}
</script>

I used a dropdown list, with autopostback enabled, and this popped up an alert when I selected an item with text of "2". The alert popped up before the postback, then the postback occurred after the alert is closed.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
That would probably work if I only needed to check the value of the dropdownlist but I have other values I need to check before I display the alert. They are values that I need to get by running a subroutine. Can I check these values and then execute the javascript function somehow?

Thanks,
Todd
 
All of the controls on your form are rendered as HTML elements, and their values can be read by a javascript function. You can either read the values you need to check from existing, visible controls, or put hidden controls on the form to "pass" the values to a javascript function. Once the values are checked in the javascript, you can set the value of another hidden control (with runat=server) to pass the result back to the server in the page's postback where you can use it in VB.

While the above is perfectly valid and should work, it is a rather "old school" way of doing something like this. I don't have much experience with it, but I believe that AJAX could work well for this situation.

Here's a link to an AJAX tutorial, if you are interested:



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
I will give that a shot and let you know. I haven't done a lot with javascript so it is a bit of a struggle.

Thanks again,
Todd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top