Answered by:
Dropdown list shows first item when page loads

Question
-
User-460338909 posted
Hi all,
I populate by code a dropdown list with listitems and when the page loads the first item in the dropdown list
is showing. Furthermore the item that is showing in the dropdown list cannot be selected and fire the postback
event. I tried by setting the selectedindex of the dropdown list to -1 but I cannot get it to show nothing in it and
fire the postback event even when I select the first item. Any ideas on what should I do?Thanks in advance,
/\/ikos
Friday, July 26, 2013 9:53 AM
Answers
-
User1256425868 posted
You can add one more Item in your Dropdown after adding all the items fron database.
Dim li As New ListItem li.Text = "Select" li.Value = "0" cmbDiamerisma.Items.Insert(0,li)
Show every time you will get "Select" as first item on your dropdown list.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 26, 2013 10:29 AM
All replies
-
User-993404089 posted
Can you please post some code.There could be several reasons for this?
Friday, July 26, 2013 9:56 AM -
User-460338909 posted
My code is this:
Dim lngMaxPeriod As Long Using conn As SqlConnection = New SqlConnection(Common.GetConnectionString("KoinoConnectionString")) Try conn.Open() Dim cmndSelect As New SqlCommand("SELECT MAX(PeriodosID) AS MaxPeriodID FROM dbo.[80_Periodoi] " & _ " GROUP BY PeriodosKoinEkdoth HAVING (PeriodosKoinEkdoth = 1)", conn) lngMaxPeriod = cmndSelect.ExecuteScalar() hidPeriod.Value = lngMaxPeriod cmndSelect.CommandText = "SELECT dbo.[80_Diamerismata].DiamID, dbo.[80_Diamerismata].DiamName, dbo.[80_Enoikoi].EnoikName " & _ "FROM dbo.[80_Enoikoi] INNER JOIN " & _ " dbo.[90_Diamerismata_Enoikoi] ON dbo.[80_Enoikoi].EnoikID = dbo.[90_Diamerismata_Enoikoi].EnoikID " & _ " RIGHT OUTER JOIN dbo.[80_Diamerismata] ON dbo.[90_Diamerismata_Enoikoi].DiamID = dbo.[80_Diamerismata].DiamID " & _ " WHERE (dbo.[80_Enoikoi].PeriodosStart <=" & (lngMaxPeriod + 1).ToString & _ " ) AND (dbo.[80_Enoikoi].PeriodosEnd >= " & (lngMaxPeriod + 1).ToString & " ) OR " & _ " (dbo.[80_Enoikoi].PeriodosStart IS NULL) AND (dbo.[80_Enoikoi].PeriodosEnd IS NULL)" & _ " ORDER BY dbo.[80_Diamerismata].DiamID" Dim ds As New DataSet Dim daSel As New SqlDataAdapter(cmndSelect) daSel.Fill(ds, "DiamEnoik") If Not Me.IsPostBack Then cmbDiamerisma.Items.Clear() End If For ii As Integer = 0 To ds.Tables("DiamEnoik").Rows.Count - 1 Dim tr As New HtmlTableRow Dim td1 As New HtmlTableCell td1.Attributes.Add("style", "font-style:italic; font-size:14px") td1.InnerText = ds.Tables("DiamEnoik").Rows(ii).Item("DiamName") If Not Me.IsPostBack Then Dim li As New ListItem li.Text = ds.Tables("DiamEnoik").Rows(ii).Item("DiamName") li.Value = ds.Tables("DiamEnoik").Rows(ii).Item("DiamID") cmbDiamerisma.Items.Add(li) End If tr.Cells.Add(td1) Dim td2 As New HtmlTableCell td2.Attributes.Add("style", "font-style:italic; font-size:14px; width:200px") td2.InnerText = IIf(ds.Tables("DiamEnoik").Rows(ii).Item("EnoikName").Equals(DBNull.Value), "Δεν έχει συμπληρωθεί", _ ds.Tables("DiamEnoik").Rows(ii).Item("EnoikName")) tr.Cells.Add(td2) tblDiamEnoikoi.Rows.Add(tr) Next ds.Dispose() daSel.Dispose() cmndSelect.Dispose() conn.Close() Catch ex As Exception If conn.State = Data.ConnectionState.Open Then conn.Close() End Try End Using
cmbDiamerisma is the dropdown list. Common is a module in App_Code that has some common shared functions.Friday, July 26, 2013 10:03 AM -
User-460338909 posted
I have to add that when I select another item in the dropdown list, the postback event occurs and then I can select the first item. But when there is only one item in the list then it gets very tricky... I can't think of nothing else to do!
Friday, July 26, 2013 10:21 AM -
User1256425868 posted
You can add one more Item in your Dropdown after adding all the items fron database.
Dim li As New ListItem li.Text = "Select" li.Value = "0" cmbDiamerisma.Items.Insert(0,li)
Show every time you will get "Select" as first item on your dropdown list.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 26, 2013 10:29 AM -
User-993404089 posted
Are you trying to populate the dropdown with a list of values on the load and then when a user changes the item in the drop down then you want to fire a postback to the server?
If so can you provide the html section that has your dropdown control.
Friday, July 26, 2013 10:37 AM -
User-1225287840 posted
Try adding the below code (cmbDiamerisma.DataBind()),
Next cmbDiamerisma.DataBind() ds.Dispose() daSel.Dispose() cmndSelect.Dispose()
Friday, July 26, 2013 10:45 AM -
User-460338909 posted
Yes the code I embeded bellow is in the Page_Load event. The HTML for the Dropdownlist is as follows:
<table> <tr> <td class="style7"> <span style = "color:Red; font-size:14px; font-weight:bold "> Διαμέρισμα </span> </td> <td align = "left" > <asp:DropDownList ID="cmbDiamerisma" runat="server" AutoPostBack="True" Height="16px" Width="61px"> </asp:DropDownList> </td> </tr>
I manage then the cmbDiamerisma_SelectedIndexChanged event.
Friday, July 26, 2013 11:13 AM -
User-460338909 posted
That is a very good suggestion MDubey1987. I might do that if I do not get it done otherwise. Thanks!!!
Friday, July 26, 2013 11:14 AM -
User2049726087 posted
You have to write the binding code as given below within page load event If(! IsPostback) { // code for binding drop down }Friday, July 26, 2013 1:02 PM -
User-1961616419 posted
read the properly working article on: How to fill DropDownList from Sql server database in asp.net
Friday, July 26, 2013 1:53 PM -
User-782344923 posted
Hi buddy,
Place your Dropdown list binding code in not postback tag as
protected void Page_Load(object sender, eventargs e) { if (!IsPostBack) // Means page is loading first time and on cause post back this condtion is fire { // Place your binding dropdownlist code here } }
Hope you understand the concept.
Cheers
Friday, July 26, 2013 3:10 PM