selectedvalue postback problem

Discussion in 'ASP .Net' started by Bernard Borsu, Jul 4, 2007.

  1. I've a problem with selectedvalue from a dropdownlist. I've migrated a web
    page from asp.net 1.1 to 2.0. Everything worked fine in asp.net 1.1.

    When postback occurs, no values left in the dropdownlist : selectedvalue =
    nothing, itemcollection is empty.

    Enableviewstate is true for the ddl and all the parents of the ddl.
    Databinding is conditioned with 'if not page.ispostback'

    I really don't understand where is the problem. I made another web migration
    from 1.1 to 2.0 with another web page and in this case everything works
    fine.

    If someone have an idea, i'm really really really interested because i've
    searched the solution for this day long. :-((

    Thanks, in advance !
     
    Bernard Borsu, Jul 4, 2007
    #1
    1. Advertisements

  2. Bernard Borsu

    Guest Guest

    Paste some code
     
    Guest, Jul 4, 2007
    #2
    1. Advertisements

  3. Portion of code in the aspx page :
    ....
    <tr>

    <td class="C1">

    <asp:Literal ID="oLLieuRepos" EnableViewState="False" Runat="server" />

    <br>

    <asp:DropDownList ID="oLieuRepos" Width="90%" Runat="server" />

    </td>

    <td class="C2">

    <asp:Literal ID="oLHLieuRepos" EnableViewState="False" Runat="server" />

    </td>

    </tr>

    .....

    Portion of code in the codebehind file :

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load

    If Page.IsPostBack Then

    Dim nI As Integer

    For nI = 1 To oNCS.Value

    NouveauBlocConjoint(nI)

    Next

    Else

    oLieuRepos.DataTextField = "Fune_NomLocalise"

    oLieuRepos.DataValueField = "Fune_Id"

    oPage.BD.Charge(IIf(lPFGP, "Proc_OPFunerariumsEPF", "Proc_FunerariumsEPF"),
    oLieuRepos, New SqlParameter("@EPF", Utilisateur()))

    End If

    End Sub

    Charge() is my own function executing SQL request dans binding data in the
    DDL.
     
    Bernard Borsu, Jul 5, 2007
    #3
  4. Another information :

    When the dropdownlist is statis, so defined in the aspx page, no problem on
    the postback.
    This problem occurs only when the dropdownlist is filled dynamically by a
    SQL request in the codebehind file.
     
    Bernard Borsu, Jul 5, 2007
    #4
  5. Bernard Borsu

    Guest Guest

    Bernard,

    Every time you build controls dynamically, you are responsible for
    recreating them on postback. I suspect you way of thinking is once control
    has been instantiated dynamically the entire control is kept in the
    viewstate. I am right? Well, this is not true. Controls store only some of
    the information required for rebuilding. If control has not been instantiated
    on postback, it’s obvious if it does not exist it cannot restore its state
    from viewstate. Hence, if you want to build DataList dynamically, handle
    itemcreated event to instantiate the controls for every row. I prepared a
    fully working example to show you how it should be done.

    -- aspx page code --

    <asp:DataList ID="dataList" runat="server" />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" />

    -- code beside / behind --

    Imports System.Data
    Imports System.Collections.Generic

    Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub dataList_ItemDataBound( _
    ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
    Handles dataList.ItemDataBound

    With e.Item

    If .ItemType = ListItemType.Item Or _
    .ItemType = ListItemType.AlternatingItem Then

    Dim row As DataRow = CType(.DataItem, DataRowView).Row

    Dim label As Label = .FindControl(LabelId)
    label.Text = row("Name")

    Dim textBox As TextBox = .FindControl(TextBoxId)
    textBox.Text = row("Date")

    End If

    End With

    End Sub

    Protected Sub dataList_ItemCreated(_
    ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
    Handles dataList.ItemCreated

    With e.Item

    If .ItemType = ListItemType.Item Or _
    .ItemType = ListItemType.AlternatingItem Then

    ' label
    Dim label As New Label()

    label.ID = LabelId
    label.ForeColor = Drawing.Color.Blue

    .Controls.Add(label)

    ' text box
    Dim textBox As New TextBox

    textBox.ID = TextBoxId
    textBox.Width = New Unit(200)

    .Controls.Add(textBox)

    End If

    End With

    End Sub

    Protected Sub Page_Load( _
    ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then

    dataList.DataSource = GetData()
    dataList.DataBind()

    End If

    End Sub

    Private Const TextBoxId As String = "txt"
    Private Const LabelId As String = "lbl"

    Private Function GetData() As DataTable

    Dim table As New DataTable()
    Dim row As DataRow

    table.Columns.Add("Id", GetType(Integer))
    table.Columns.Add("Name", GetType(String))
    table.Columns.Add("Date", GetType(DateTime))

    For i As Integer = 1 To 10

    row = table.NewRow()

    row(0) = i
    row(1) = "Name" + i.ToString()
    row(2) = DateTime.Now.AddMinutes(i)

    table.Rows.Add(row)

    Next

    Return table

    End Function

    Protected Sub btnSubmit_Click(_
    ByVal sender As Object, _
    ByVal e As System.EventArgs) _
    Handles btnSubmit.Click

    Dim dates As New List(Of DateTime)

    For Each item As DataListItem In dataList.Items

    Dim textBox As TextBox = item.FindControl(TextBoxId)
    dates.Add(DateTime.Parse(textBox.Text))

    Next


    End Sub

    End Class

    hope this helps
     
    Guest, Jul 5, 2007
    #5
  6. Thanks for this explanation. I'm totally agree with it. But it's not really
    the good context.

    I think my details were badly explained.

    I'm not building my dropdownlist dynamically. I fill my dropdownlist
    dynamically.

    When listitems of my dropdownlist are define in the aspx page, i've no
    problem to use the selectedvalue property when the postback event occurs.

    At the opposite, When listitems of my dropdownlist are created in the
    codebehind file, in the page_load (conditioned by not ispostback()), i can't
    use the selectedvalue property when the postback event occurs. All the items
    of the ddl are not presents. It seems that loadviewstate did not operate for
    this control. It was the case in asp.net 1.1. I use exactly the same code
    i've used in asp.net 1.1 and the result is not the same in asp.net 2.0.

    Sorry but my english is not very fine.

    Is it more clear ?

     
    Bernard Borsu, Jul 5, 2007
    #6
  7. Bernard Borsu

    Guest Guest

    Hi again,

    That's exactly what i was trying to explain. You have to follow the way i
    presented in my previous post. Items are restored from the viewstate, but you
    have to recreate the dropdown list first, and they it'll take care of
    repopulating items collection itself. The only requirement is the control
    tree must be the same (meaning, the structure of the datalist has to be
    exactly as it was before viewstate was serialized). Try to amend your code
    using my example and you'll see dropdownlist will contain the same items as
    before postback. If you have any more questions, i'll be pleased to answer.

    regards
    --
    Milosz


     
    Guest, Jul 5, 2007
    #7
  8. Hi !

    In this case, this is not the real reason of the problem. I've tried to test
    the ddl in a simple page with the minimum of elements. In this case,
    everything works fine :

    Aspx page :

    <%@ Page Language="vb" AutoEventWireup="false"
    MasterPageFile="/Cadres/Enaos.Master" CodeBehind="SaleteBis.aspx.vb"
    Inherits="PFGP_Partenaires.SaleteBis" %>

    <asp:Content ID="oC" ContentPlaceHolderID="oH" runat="server">

    <asp:DropDownList ID="oLieuRepos" runat="server" />

    <asp:Button ID="oOK" runat="server" />

    </asp:Content>

    Codebehind file :

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load

    If IsPostBack Then

    Response.Write(oLieuRepos.SelectedIndex & "/" & oLieuRepos.SelectedValue &
    "/" & oLieuRepos.EnableViewState & "<br>")

    Else

    BD.Charge("Proc_OPFunerariumsEPF", oLieuRepos, New SqlParameter("@EPF",
    Utilisateur()))

    With oLieuRepos

    With .Items()

    ..Add(New ListItem(Ressource("OptReposDomicile"), -1))

    ..Add(New ListItem(Ressource("OptReposAutreFunerarium"), -2))

    ..Add(New ListItem(Ressource("OptReposAutre"), -3))

    End With

    End With

    End If

    End Sub

    In fact, the bad result is due to 2 form tag in the page. One is the aspx
    form tag and the second one is for searching an item in the database. If i
    remove this search form tag, in the postback, the selectedvalue property
    return the good value and the ddl items are stil present.

    Many thanks for your attention.

     
    Bernard Borsu, Jul 5, 2007
    #8
  9. Last detail : problem due to 2 form tags, but one without method "post"
    explicitly declared.

    So, now everything functinable with on pure html form with method post
    declared and on aspx form

     
    Bernard Borsu, Jul 5, 2007
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.