在 FromView 控件中使用 DropDownList

发布于 2024-11-28 13:54:48 字数 1006 浏览 0 评论 0原文

我正在尝试让下拉列表控件在 FormView 中工作。我需要让列表成为某个表的过滤视图,并且仍然绑定到我正在编辑的数据中的字段。我尝试以编程方式设置项目数据,蚂蚁可以工作,但是数据绑定 不起作用,它尝试将 null 插入数据库。

这是我尝试过的代码。我也尝试在其他几个事件中做同样的事情,它仍然尝试将 null 插入数据库。

            <asp:DropDownList ID="lstManagers" runat="server" 
                OnDataBound ="ManagersLoad"
                SelectedValue='<%# Bind("UserName") %>' Width="100%" 
                DataSourceID="TimeOff" DataTextField="UserName" DataValueField="UserName">
            </asp:DropDownList>


Protected Sub ManagersLoad(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim lst As DropDownList = FormView1.FindControl("lstManagers")
    'get list of managers
    Using ef As New TimeOffData.TimeOffEntities
        For Each item As ListItem In lst.Items
            Dim li As ListItem = item
            item.Text = (From x In ef.TimeOffUsers Where x.UserName = li.Value Select x.FirstName & " " & x.LastName).FirstOrDefault
        Next
    End Using
End Sub

I'm trying to get a drop down list control to work in FormView. I need to have the list be a filtered view of a certain table and still be bound to a field in the data I'm editing. I've tried setting the item data programatically, ant that works but then the data binding
doesn't work, It tries to insert null into the database.

This is the code I've tried. I've also tried doing the same thing in several other events, it still tries to insert null into the database.

            <asp:DropDownList ID="lstManagers" runat="server" 
                OnDataBound ="ManagersLoad"
                SelectedValue='<%# Bind("UserName") %>' Width="100%" 
                DataSourceID="TimeOff" DataTextField="UserName" DataValueField="UserName">
            </asp:DropDownList>


Protected Sub ManagersLoad(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim lst As DropDownList = FormView1.FindControl("lstManagers")
    'get list of managers
    Using ef As New TimeOffData.TimeOffEntities
        For Each item As ListItem In lst.Items
            Dim li As ListItem = item
            item.Text = (From x In ef.TimeOffUsers Where x.UserName = li.Value Select x.FirstName & " " & x.LastName).FirstOrDefault
        Next
    End Using
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2024-12-05 13:54:48

我获取了控件的所有数据绑定内容,并决定手动完成它会更容易。我已将代码更改为这样

Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
    Dim lst As DropDownList = FormView1.FindControl("lstManagers")
    e.Values.Item("ManagerName") = lst.SelectedValue
End Sub

Protected Sub ManagersLoad(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim lst As DropDownList = FormView1.FindControl("lstManagers")
    'get list of managers
    Using ef As New TimeOffData.TimeOffEntities
        Dim mng = From x In ef.TimeOffUsers Where x.IsManager = True

        For Each item In mng
            lst.Items.Add(New ListItem(item.FirstName & " " & item.LastName, item.UserName))
        Next
    End Using
End Sub

I took all the data binding stuff of the control and just decide it would be easier to do it manually. I've changed the code to this,

Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
    Dim lst As DropDownList = FormView1.FindControl("lstManagers")
    e.Values.Item("ManagerName") = lst.SelectedValue
End Sub

Protected Sub ManagersLoad(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim lst As DropDownList = FormView1.FindControl("lstManagers")
    'get list of managers
    Using ef As New TimeOffData.TimeOffEntities
        Dim mng = From x In ef.TimeOffUsers Where x.IsManager = True

        For Each item In mng
            lst.Items.Add(New ListItem(item.FirstName & " " & item.LastName, item.UserName))
        Next
    End Using
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文