Hi,

I've been trying to get this to work for hours to no avail (at least they're paying me :cheesy: ). I have a DropDownList that obtains its values from a database. The DropDownList will not retain it's values when a Postback is done (caused by a submit button being pressed), and I cannot use the .selectedindex or .selectedvalue properties in my coding, which are both very important. I think the reason this is happening is that the list binds the data to itself each time the page loads and the Load event occurs before all Postback Events, thus the dropdownlist is completely reset before the button's click event occurs. How can I work around this and use obtain the dropdownlist values I need?

-James Waltz

Recommended Answers

All 9 Replies

Hoo hoo I love these easy peasy ones!!

You need to check for postback, if first request (not a postback) bind the data
c#

if(!IsPostBack)
{
    dropDownList.DataSource = GetData(); //however your using ADO
    dropDownList.DataBind();
}

VB

If Not Page.IsPostback Then
    DropDownList.DataSource = GetData() //ADO blah
    DropDownList.DataBind()
End If

I tried something similar to this I think but I still couldn't obtain the value from the dropdown list and the dropdown list was blank after the PostBack. Though I'll try it again and see what happens I guess...

unlikely but is your veiwstate property disabled? for that page or just that control ?

also, if your control is inside another control (eg table) viewstate needs to be enabled for that too

I just checked and everything related to that DropDownList has the viewstate enabled.

1. Did you try the IsPostBack check again? it's not clear from your replies if you gave that a second try.

2. If you can post your aspx and your aspx.vb/cs code we have a chance of figuring out your problem.

I did try to add the IsPostBack check again, and it seems to conserve the DropDownList values, but not the selected information (.selectedvalue, .selectedtext, and .selectedindex). As far as my code, happy reading ;) :

.aspx page

<%@ Page Language="VB" MasterPageFile="~/lib/Normal.master" AutoEventWireup="false" CodeFile="reflinks.aspx.vb" Inherits="resources_reflinks" title="Reference Links - Zekiah Technologies Intranet" EnableViewState="true"%>
<asp:ContentID="Content1"ContentPlaceHolderID="Body"Runat="Server">
 
<asp:Table ID="tblLinks" runat="server" Width="99.5%" CellSpacing="0" BorderWidth="1" BorderColor="Black" BorderStyle="Solid">
<asp:TableHeaderRow BackColor="gray" ForeColor="white">
<asp:TableCell columnspan="6">
:: REFERENCE LINKS ::
</asp:TableCell>
</asp:TableHeaderRow>

<asp:Tablerow>
<asp:TableCell ColumnSpan="6">
&nbsp; <asp:Label ID="lblAddMessage" runat="server" Text="Label" ForeColor="red" Font-Bold="true" Visible="false"></asp:Label>
</asp:TableCell>
</asp:Tablerow>

<asp:TableRow>
<asp:TableCell HorizontalAlign="center" Width="50%" ColumnSpan="6">
Select a Topic:
<asp:DropDownList ID="TopicDrop" runat="server" DataSourceID="SqlDataSource1" DataTextField="ref_topic_name" DataValueField="ref_topic_id" AutoPostBack="True" AppendDataBoundItems="true">
<asp:ListItem Value="0" Selected="true" Text="- All Topics -" />
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="gray" ForeColor="white">
<asp:TableCell Width="10%">
&nbsp; 
</asp:TableCell>
<asp:TableCell HorizontalAlign="left" Width="10%">
TOPIC
</asp:TableCell>
<asp:TableCell HorizontalAlign="left" Width="40%">
LINK
</asp:TableCell>
<asp:TableCell HorizontalAlign="left" Width="20%">
DESCRIPTION
</asp:TableCell>
<asp:TableCell HorizontalAlign="left" Width="10%">
DATE
</asp:TableCell>
<asp:TableCell HorizontalAlign="left" Width="10%">
USER
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell ColumnSpan="6">
<asp:Label ID="lblNoLink" runat="server" Text="Label" ForeColor='Red' Font-Bold="true" Visible="false">There are no FAQs for your selected topic!</asp:Label>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<input id="btnAdd" type="button" value="Add" runat="server" class="btnSubmitType" onmouseover="this.className = 'btnSubmitHov'" onmouseout="this.className='btnSubmitType'" validationgroup="AddLink"/>
<asp:DropDownList ID="TopicDrop2" runat="server" DataTextField="ref_topic_name"
DataValueField="ref_topic_id" AppendDataBoundItems="true" ValidationGroup="AddLink">
<asp:ListItem Value="0" Selected="True" Text="- Topic -" />
</asp:DropDownList>
<asp:TextBox ID="txtLink" runat="server" ValidationGroup="AddLink"></asp:TextBox>
<asp:TextBox ID="txtDescription" runat="server" ValidationGroup="AddLink"></asp:TextBox>&nbsp;
<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:sysObjDBConnectionString %>"
SelectCommand="SELECT * FROM REFERENCE_TOPIC ORDER BY ref_topic_name">
</asp:SqlDataSource>
 
</asp:Content>

.aspx.vb

PartialClass resources_reflinks
Inherits System.Web.UI.Page
Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
If Not IsPostBack Then
TopicDrop2.DataSource = SqlDataSource1
TopicDrop2.DataBind()
ShowLinks(0)
End If
End Sub
 
Sub ShowLinks(ByVal Topic As Integer)
Dim Conn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
Dim CmdLink As New Data.SqlClient.SqlCommand("SELECT t.ref_topic_name, r.ref_link_url, r.ref_link_desc, r.ref_link_update_dt, ua.lastName, ua.firstName FROM REFERENCE_LINK as r LEFT OUTER JOIN REFERENCE_TOPIC as t ON r.ref_topic_id = t.ref_topic_id INNER JOIN userAccounts as ua ON r.userUID = ua.userUID", Conn)
Dim DRLink As Data.SqlClient.SqlDataReader
Dim TR As TableRow
Dim TC As TableCell
Dim lnk As HyperLink
Conn.Open()
If Topic <> 0 Then
CmdLink.CommandText = "SELECT t.ref_topic_name, r.ref_link_url, r.ref_link_desc, r.ref_link_update_dt, ua.lastName, ua.firstName FROM REFERENCE_LINK as r LEFT OUTER JOIN REFERENCE_TOPIC as t ON r.ref_topic_id = t.ref_topic_id INNER JOIN userAccounts as ua ON r.userUID = ua.userUID WHERE r.ref_topic_id = " & Topic
End If
DRLink = CmdLink.ExecuteReader
If DRLink.Read Then
Dim CColor As Drawing.Color = Drawing.Color.LightBlue
Dim OColor As Drawing.Color = Drawing.Color.White
Dim TColor As Drawing.Color
Do
TR = New TableRow
TR.BackColor = CColor
TR.Font.Size = FontUnit.Smaller
TC = New TableCell
TC.HorizontalAlign = HorizontalAlign.Left
TC.Text = "&nbsp;"
TR.Cells.Add(TC)
TC = New TableCell
TC.HorizontalAlign = HorizontalAlign.Left
TC.Text = DRLink(0)
TR.Cells.Add(TC)
TC = New TableCell
TC.HorizontalAlign = HorizontalAlign.Left
lnk = New HyperLink
lnk.Text = DRLink(1)
lnk.NavigateUrl = DRLink(1)
TC.Controls.Add(lnk)
TR.Cells.Add(TC)
TC = New TableCell
TC.HorizontalAlign = HorizontalAlign.Left
TC.Text = DRLink(2)
TR.Cells.Add(TC)
TC = New TableCell
TC.HorizontalAlign = HorizontalAlign.Left
TC.Text = Left(DRLink(3), 10)
TR.Cells.Add(TC)
TC = New TableCell
TC.HorizontalAlign = HorizontalAlign.Left
TC.Text = DRLink(5) & ", " & DRLink(4)
TR.Cells.Add(TC)
tblLinks.Rows.Add(TR)
TColor = CColor
CColor = OColor
OColor = TColor
Loop While DRLink.Read
lblNoLink.Visible = False
Else
lblNoLink.Visible = True
End If
'Footer for Reference additions
TR = New TableFooterRow
TR.BackColor = Drawing.Color.Gray
'***Add Button***
TC = New TableCell
TC.HorizontalAlign = HorizontalAlign.Left
TC.Controls.Add(btnAdd)
TC.HorizontalAlign = HorizontalAlign.Center
TR.Cells.Add(TC)
TC = New TableCell
'***Add DropDown for topic***
TC.HorizontalAlign = HorizontalAlign.Left
TC.Controls.Add(TopicDrop2)
TR.Cells.Add(TC)
'***Add link textbox for adding links***
TC = New TableCell
txtLink.Width = Unit.Percentage(80.0)
TC.HorizontalAlign = HorizontalAlign.Left
TC.Controls.Add(txtLink)
TR.Cells.Add(TC)
'***Add description textbox for adding links***
TC = New TableCell
txtDescription.Width = Unit.Percentage(80.0)
TC.HorizontalAlign = HorizontalAlign.Left
TC.Controls.Add(txtDescription)
TR.Cells.Add(TC)
'***Spaces***
TC = New TableCell
TC.HorizontalAlign = HorizontalAlign.Left
TC.Text = "&nbsp;"
TR.Cells.Add(TC)
TC = New TableCell
TC.HorizontalAlign = HorizontalAlign.Left
TC.Text = "&nbsp;"
TR.Cells.Add(TC)
tblLinks.Rows.Add(TR)
DRLink.Close()
Conn.Close()
lblAddMessage.Visible = False
End Sub
Protected Sub TopicDrop_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TopicDrop.SelectedIndexChanged
ShowLinks(Val(TopicDrop.SelectedValue))
End Sub
Protected Sub btnAdd_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.ServerClick
ShowLinks(Val(TopicDrop.SelectedValue))
If TopicDrop2.SelectedIndex = 0 Then
lblAddMessage.Visible = True
lblAddMessage.Text = "Please select a topic below for your link."
ElseIf txtLink.Text = "" Then
lblAddMessage.Visible = True
lblAddMessage.Text = "Please input a URL below for your link."
ElseIf txtDescription.Text = "" Then
lblAddMessage.Visible = True
lblAddMessage.Text = "Please write a short description below for your link."
Else
Dim Conn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
Dim Cmd As New Data.SqlClient.SqlCommand("INSERT REFERENCE_LINKS (ref_topic_id, ref_link_url, ref_link_desc, ref_link_update_dt, userUID) VALUES(" & TopicDrop2.SelectedValue & ",'" & txtLink.Text & "','" & txtDescription.Text & "', CURRENT_TIMESTAMP, " & User.Identity.Name & ")", Conn)
'Cmd.ExecuteNonQuery()
lblAddMessage.Visible = True
lblAddMessage.Text = "Link added successfully!"
End If
End Sub
Protected Sub TopicDrop2_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles TopicDrop2.DataBinding
End Sub
Protected Sub TopicDrop2_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles TopicDrop2.DataBound
End Sub
EndClass

Looks like I'm having the same problem on another page with another list-related control. It seems that lists do not want retain their selected value information on for postbacks (unless they initiate the postback). It might have something to do with the fact that the controls are being binded to data each time the page is loaded; however, when I tried to keep it from doing this, the control still did not retain the pertinent information. -_-

Just now figured it out after about 2 days of trying things. The problem was caused by the fact that I was programmatically 'moving' the lists into a table. When that happens, I think the control is being copied, minus the selected values. It works perfectly if the list is already inside its containing control, in this case the table.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.