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

OnSelectedIndexChanged 1

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
IN
I am populating a DropDownList with the company names existing in a SQL Server DB table. What I want is when a user selects 1 of the company names from the DropDownList, he should be shown a message that he has chosen 'this' company. This is what I have done:

<script language=&quot;VB&quot; runat=&quot;server&quot;>
Sub Page_Load(obj As Object,ea As EventArgs)
Dim objDS As DataSet
Dim objConn As SQLConnection
Dim objDapter As SQLDataAdapter

objConn=New SQLConnection(&quot;Server=...........&quot;)
objDapter=New SQLDataAdapter(&quot;SELECT CName=CName+'-'+CCode FROM Company&quot;,objConn)

objDS=new DataSet()
objDapter.Fill(objDS,&quot;Comp&quot;)

cname.DataSource=objDS.Tables(&quot;Comp&quot;).DefaultView
cname.DataBind()
End Sub

Sub selectCName(obj As Object,ea As EventArgs)
Dim strCName
strCName=cname.SelectedItem.Value
Response.Write(&quot;You have chosen : &quot; & strCName)
End Sub
</script>
<body>
<form runat=&quot;server&quot;>
<asp:DropDownList id=&quot;cname&quot; AutoPostBack=&quot;true&quot; DataTextField=&quot;CName&quot; OnSelectedIndexChanged=&quot;selectCName&quot; runat=&quot;server&quot;>
</asp:DropDownList>
</form>

Now when I execute the above code, I find that irrespective of the company name selected by the user, cname.SelectedItem.Value always remains equal to the first company name that is displayed in the DropDownList. For e.g. suppose the DropDownList gets populated with the following 4 company names : Comp1 Ltd., Comp2 Ltd., Comp3 Ltd. & Comp4 Ltd. When the page loads for the first time, Comp1 Ltd. is displayed in the DropDownList. Now if a user selects, say, Comp4 Ltd., still the user is shown : You have chosen Comp1 Ltd. where as it should actually show : You have chosen Comp4 Ltd. How do I ensure that cname.SelectedItem.Value is assigned that company name which has been selected by the user?

Thanks,

Arpan
 
I was having that problem too... Easily fixed though:

Sub Page_Load(obj As Object,ea As EventArgs)
IF NOT page.ispostback THEN
...code that populates the list...
END IF
 
Hi,

Thanks for your response. Your advice has really turned out to be very helpful. But what does this Page.IsPostBack do? I know that IsPostBack tells whether a form on one page is posted to the same page or not but I couldn't exactly follow how this Page property solved my problem.

Thanks once again for your help & co-operation,

Regards,

Arpan
 
It works because it only populates the list the first time the page is loaded. When information is posted back to the page, you don't want the list re-populated because then it essentially resets itself, perhaps losing valuable information along the way.

To use your code as an example, one possible flow could go like this.

Code:
1. List is populated
2. User picks, say, third item in the list.
3. Information is posted back causing Page_Load to be called
4. List is re-populated, which means the selected item is now the default selected item, giving you an incorrect result.

As you can see, by causing the list to be populated each time, it will usually give you the incorrect result. Checking the state of IsPostBack prevents this from happening.
 
That was indeed a great explanation!!!

Thanks once again,

Regards,

Arpan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top