Tyler Adkins
Tyler Adkins

Reputation: 25

ASP.NET Losing listbox binding on viewchange?

Ok so what seems like a basic problem is getting the better of me and my exstensive google efforts have come up short. Perhaps I don't understand enough to ask the right questions.

Here's my problem:

I have a formview control, or rather a series of them, each page displaying entry from previous forms, for a higher level access to approve/edit as needed. So, on form "B", I have the contents of form "A" and the blank part of "B" to filled out...So two seperate fromviews on the page.."A" and "B"

That works fine, the issue is when I change the mode to edit previous entry. So if I have a button or the default linkbutton to change from ReadOnly to Edit I not only lose bindings but any efforts to counteract that have left me with issues when I postback.

DUE TO LENGTH I'M LEAVING SOME CODE OUT

On my button I'm using FormView2.ChangeMode(FormViewMode.Edit) to change view, the default link button I've not changed

Bindings on my listboxes are setup like:

If Not Page.IsPostBack Then
    'pulling bindings from table
    cmd = New OleDbCommand("SELECT * FROM mslToi", objCon)
    objReader = cmd.ExecuteReader
    lst1.DataValueField = "listing"
    lst1.DataTextField = "listing"
    lst1.DataSource = objReader
    lst1.DataBind()

    'pre-selecting input data from form "A"
    cmd = New OleDbCommand("SELECT [type_of_injury] FROM S2childToi WHERE ID = " & qs & "", objCon)
    objReader = cmd.ExecuteReader
        Do While objReader.Read

            For Each y As ListItem In lst1.Items
                If y.Text = objReader.Item(0) Then
                    y.Selected = True
                End If
            Next
        Loop

end if

In the page load event.

MARKUP FOR THE FORMVIEW AS ASKED

<asp:FormView ID="FormView2" runat="server" 
    Width="100%" DataSourceID="AccessDataSource4">

<ItemTemplate>
</ItemTemplate>

<EditItemTemplate>
</EditItemTemplate>

</asp:FormView>

'''that is the short and sweet of the formview markup as requested. It may also be worth noting that it doesn't matter what mode I start in, if I change modes it equals same result'''

That works fine so far...it's when I change view to Edit that my listbox appears to no longer be bound (controls appear but have no content). My thought is that obviously I'm blocking out my code from postback events (I have a reason for this). I can use this code (without the If Not Page.IsPostBack) to force the selections and bindings but whenever I postback they will defualt to the table data, which can't happen, each listbox needs to postback so I can check for a certain selection. So what happens is the user input is trumped. Short and sweet.

I'm sorry that I can't explain better, any advice is much appreciated. If I can asnwer any questions or post code let me know.

Upvotes: 1

Views: 597

Answers (1)

pinoy_ISF
pinoy_ISF

Reputation: 1182

Try this:

<asp:FormView ID="FormView1" runat="server">
    <ItemTemplate>
        <asp:ListBox ID="ListBoxReadonly" runat="server"></asp:ListBox>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:ListBox ID="ListBoxEdit" runat="server"></asp:ListBox>
    </EditItemTemplate>
</asp:FormView>

Then, in your FormView's databound event, bind the data into your listbox depending on the current view.

Protected Sub FormView1_DataBound(sender As Object, e As EventArgs) Handles FormView1.DataBound
    Dim myListBox As ListBox

    If FormView1.CurrentMode = FormViewMode.ReadOnly Then
        myListBox = DirectCast(FormView1.FindControl("ListBoxReadonly"), ListBox)
    ElseIf FormView1.CurrentMode = FormViewMode.Edit Then
        myListBox = DirectCast(FormView1.FindControl("ListBoxEdit"), ListBox)
    End If

    If myListBox IsNot Nothing Then
        myListBox.DataValueField = "listing"
        myListBox.DataTextField = "listing"
        myListBox.DataSource = GetListingData()
        myListBox.DataBind()

        ' your pre-select code here...
    End If
End Sub

Upvotes: 1

Related Questions