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="VB" runat="server">
Sub Page_Load(obj As Object,ea As EventArgs)
Dim objDS As DataSet
Dim objConn As SQLConnection
Dim objDapter As SQLDataAdapter
objConn=New SQLConnection("Server=..........."
objDapter=New SQLDataAdapter("SELECT CName=CName+'-'+CCode FROM Company",objConn)
objDS=new DataSet()
objDapter.Fill(objDS,"Comp"
cname.DataSource=objDS.Tables("Comp".DefaultView
cname.DataBind()
End Sub
Sub selectCName(obj As Object,ea As EventArgs)
Dim strCName
strCName=cname.SelectedItem.Value
Response.Write("You have chosen : " & strCName)
End Sub
</script>
<body>
<form runat="server">
<aspropDownList id="cname" AutoPostBack="true" DataTextField="CName" OnSelectedIndexChanged="selectCName" runat="server">
</aspropDownList>
</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
<script language="VB" runat="server">
Sub Page_Load(obj As Object,ea As EventArgs)
Dim objDS As DataSet
Dim objConn As SQLConnection
Dim objDapter As SQLDataAdapter
objConn=New SQLConnection("Server=..........."
objDapter=New SQLDataAdapter("SELECT CName=CName+'-'+CCode FROM Company",objConn)
objDS=new DataSet()
objDapter.Fill(objDS,"Comp"
cname.DataSource=objDS.Tables("Comp".DefaultView
cname.DataBind()
End Sub
Sub selectCName(obj As Object,ea As EventArgs)
Dim strCName
strCName=cname.SelectedItem.Value
Response.Write("You have chosen : " & strCName)
End Sub
</script>
<body>
<form runat="server">
<aspropDownList id="cname" AutoPostBack="true" DataTextField="CName" OnSelectedIndexChanged="selectCName" runat="server">
</aspropDownList>
</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