ASP.NET在viewchange上丢失列表框绑定?

时间:2012-10-09 20:11:13

标签: asp.net vb.net data-binding formview

好的,所以看起来像一个基本的问题就是让我变得更好,而我的谷歌努力已经缩短了。也许我不太了解提出正确的问题。

这是我的问题:

我有一个formview控件,或者更确切地说是一系列,每个页面都显示以前表单中的条目,以便根据需要进行更高级别的批准/编辑访问。因此,在表格“B”上,我有“A”形式的内容和“B”的空白部分填写...所以页面上的两个单独的fromviews ...“A”和“B”

工作正常,问题是当我更改模式以编辑上一个条目时。因此,如果我有一个按钮或默认链接按钮从ReadOnly更改为Edit我不仅会丢失绑定,而且任何抵消的努力都会让我在回发时遇到问题。

由于长度我离开了一些代码

在我的按钮上我使用FormView2.ChangeMode(FormViewMode.Edit)来更改视图,默认链接按钮我没有改变

我的列表框上的绑定设置如下:

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

在页面加载事件中。

以书面形式确认

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

<ItemTemplate>
</ItemTemplate>

<EditItemTemplate>
</EditItemTemplate>

</asp:FormView>

'''这是formview标记的简短和甜蜜的要求。值得注意的是,无论我开始使用哪种模式都无关紧要,如果我改变模式它就等于相同的结果'''

到目前为止工作正常...当我将视图更改为编辑时,我的列表框似乎不再被绑定(控件出现但没有内容)。我的想法显然是我在回发事件中阻止了我的代码(我有理由这样做)。我可以使用这段代码(没有If Not Page.IsPostBack)强制选择和绑定但是每当我回发它们都会默认为表数据,这是不可能的,每个列表框都需要回发,所以我可以检查一下选择。所以会发生什么是用户输入被打败。简短又甜蜜。

对不起,我无法更好地解释,任何建议都非常感谢。如果我能回答任何问题或邮政编码,请告诉我。

1 个答案:

答案 0 :(得分:1)

试试这个:

<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>

然后,在FormView的数据绑定事件中,根据当前视图将数据绑定到列表框中。

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