Asp.net Frequently Asked Questions
CoderSource.net
Asp.net Frequently Asked Questions - Article by azamsharp
Level: BeginnerType: Article
Rating: Page: 2 of 2

Date: 6/26/2005 12:00:00 AM

Environment: Asp .Net, Windows, IIS

Q4: Is that possible to not to fire validation when a button is clicked?

This is one of the most commonly asked questions on www.asp.net forums.

The answer is very simple just set the CausesValidation property of the button to false and so that button will not fire the validation.

Q5: How can I redirect to another page after the exception has thrown?

Have you ever tried redirecting your page in the catch block. It won't work if you don't supply the correct parameters. Let's say you try something like this:

 
try

{

// Do some work here and suppose the exception is thrown

}

catch(Exception ex)

{

Response.Redirect("ErrorPage.aspx");

}

You should be expecting that you will be redirected to the "ErrorPage.aspx" page but unfortunately this will throw "System.Threading" exception. If you want to redirect to the "ErrorPage.aspx" than you will have to do something like this:

 
try

{

// Do some work here and suppose the exception is thrown

}

catch(Exception ex)

{

Response.Redirect("ErrorPage.aspx",true);

}

As you can see that I have provided the second parameter to "true" for the Response.Redirect method which terminates the execution of the current page and redirects to the new page successfully.

Q6: How can I populate one dropdownlist based on the selected of another dropdownlist?

This is one of the most frequently asked questions. Suppose you have a dropdownlist and you want that when you select any item from that dropdownlist then another dropdownlist should be populated. I have created two dropdownlist "ddlParent" and "ddlChild". Let's see how this be done.

First let's populate the "ddlParent":

 
if(!Page.IsPostBack)

{

SqlDataAdapter ad = new SqlDataAdapter("SELECT PersonID FROM Person",myConnection);

DataSet ds = new DataSet();

ad.Fill(ds,"Person");

ddlParent.DataSource = ds;

ddlParent.DataTextField = "PersonID";

ddlParent.DataValueField = "PersonID";

ddlParent.DataBind();

}

As you can see that populated the dropdownlist was not that hard. Now in the selectedIndexChanged event of the "ddlParent" you will implement the code that will fill the "ddlChild". Let's see how this is accomplished:

 
private void ddlParent_SelectedIndexChanged(object sender, System.EventArgs e)

{

int personID = Convert.ToInt32(ddlParent.SelectedItem.Value);

string query = "SELECT PhoneNumber,PhoneID FROM Phone WHERE PersonID = "+personID;

SqlDataAdapter ad = new SqlDataAdapter(query,myConnection);

DataSet ds = new DataSet();

ad.Fill(ds,"Phone");

ddlChild.DataSource = ds;

ddlChild.DataTextField = "PhoneNumber";

ddlChild.DataValueField = "PhoneID";

ddlChild.DataBind();

}

 

If you want to instantly fill the child dropdownlist when the item is selected from the parent than make the "IsPostBack" property of the "ddlParent" to true.

I hope you like the article happy coding.


Attachments

Project Files Faq Samples

1 2

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us