如何通过代码隐藏从 LinqDataSource 获取记录值

发布于 2024-08-27 14:48:01 字数 3653 浏览 3 评论 0原文

我有一个可检索单个记录的 LinqDataSource。

Protected Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
    Dim BizForSaleDC As New DAL.BizForSaleDataContext
    e.Result = BizForSaleDC.bt_BizForSale_GetByID(e.WhereParameters("ID")).FirstOrDefault
End Sub

我希望能够使用 Page_Load 函数检索所述数据源的值。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    'Get the right usercontrol'
    Dim ctrl As UserControl
    Select Case DataBinder.Eval(LINQDATASOURCE_SOMETHING.DataItem, "AdType")
        Case 1 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 2 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 3 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case 4 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case Else : ctrl = Nothing
    End Select

    'set the control to visible'
    ctrl.Visible = True
End Sub

但显然上面的代码不起作用......我只是想知道是否有办法让它工作。

这是完整的标记

<body>
    <form id="form1" runat="server">
    <asp:LinqDataSource ID="LinqDataSource1" runat="server">
        <WhereParameters>
            <asp:QueryStringParameter ConvertEmptyStringToNull="true" Name="ID" QueryStringField="ID"
                Type="Int32" />
        </WhereParameters>
    </asp:LinqDataSource>
    <uc:Default ID="Default1" runat="server" Visible="false" />
    <uc:ValuPro ID="ValuPro1" runat="server" Visible="false" />
    </form>
</body>
</html>

基本上发生的情况是启用适当的用户控件,并且该用户控件继承 LinqDataSource 并显示适当的信息。

编辑:现在可以使用下面的代码,但是,由于我真的不想多次访问数据库来获取相同的信息,所以我更愿意从数据源获取值。

'Query the database'
Dim _ID As Integer = Convert.ToInt32(Request.QueryString("ID"))
Dim BizForSaleDC As New BizForSaleDataContext
Dim results = BizForSaleDC.bt_BizForSale_GetByID(_ID).FirstOrDefault

'Get the right usercontrol'
Dim ctrl As UserControl
Select Case results.AdType
    Case 1 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
    Case 2 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
    Case 3 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
    Case 4 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
    Case Else : ctrl = Nothing
End Select

编辑2:这似乎是一种解决方法,但我想知道是否有更干净的方法

Private AdType As String
Private isSold As Boolean

Protected Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
    Dim BizForSaleDC As New DAL.BizForSaleDataContext
    e.Result = BizForSaleDC.bt_BizForSale_GetByID(e.WhereParameters("ID")).FirstOrDefault

    AdType = e.Result.AdType
    isSold = e.Result.isSold
End Sub


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

    'Get the right usercontrol'
    Dim ctrl As UserControl
    Select Case AdType
        Case 1 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 2 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 3 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case 4 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case Else : ctrl = Nothing
    End Select

    ctrl.Visible = True
    'Display SOLD if item is sold'
    ctrl.FindControl("SoldDiv").Visible = isSold

End Sub

I've got a LinqDataSource that retrieves a single record.

Protected Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
    Dim BizForSaleDC As New DAL.BizForSaleDataContext
    e.Result = BizForSaleDC.bt_BizForSale_GetByID(e.WhereParameters("ID")).FirstOrDefault
End Sub

I'd like to be able to retrieve the values of said DataSource using the Page_Load function.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    'Get the right usercontrol'
    Dim ctrl As UserControl
    Select Case DataBinder.Eval(LINQDATASOURCE_SOMETHING.DataItem, "AdType")
        Case 1 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 2 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 3 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case 4 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case Else : ctrl = Nothing
    End Select

    'set the control to visible'
    ctrl.Visible = True
End Sub

But obviously the code above doesn't work... I'm just wondering if there's a way to make it work.

Here is the full markup

<body>
    <form id="form1" runat="server">
    <asp:LinqDataSource ID="LinqDataSource1" runat="server">
        <WhereParameters>
            <asp:QueryStringParameter ConvertEmptyStringToNull="true" Name="ID" QueryStringField="ID"
                Type="Int32" />
        </WhereParameters>
    </asp:LinqDataSource>
    <uc:Default ID="Default1" runat="server" Visible="false" />
    <uc:ValuPro ID="ValuPro1" runat="server" Visible="false" />
    </form>
</body>
</html>

Basically what happens is the appropriate usercontrol is enabled and that usercontrol inherits the LinqDataSource and displays the appropriate information.

EDIT: It's working now with the code below, however, since I'm really not into hitting the database multiple times for the same info, I'd prefer to get the value from the DataSource.

'Query the database'
Dim _ID As Integer = Convert.ToInt32(Request.QueryString("ID"))
Dim BizForSaleDC As New BizForSaleDataContext
Dim results = BizForSaleDC.bt_BizForSale_GetByID(_ID).FirstOrDefault

'Get the right usercontrol'
Dim ctrl As UserControl
Select Case results.AdType
    Case 1 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
    Case 2 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
    Case 3 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
    Case 4 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
    Case Else : ctrl = Nothing
End Select

EDIT 2: This appears to be a work around, but I'd like to know if there's a cleaner way

Private AdType As String
Private isSold As Boolean

Protected Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
    Dim BizForSaleDC As New DAL.BizForSaleDataContext
    e.Result = BizForSaleDC.bt_BizForSale_GetByID(e.WhereParameters("ID")).FirstOrDefault

    AdType = e.Result.AdType
    isSold = e.Result.isSold
End Sub


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

    'Get the right usercontrol'
    Dim ctrl As UserControl
    Select Case AdType
        Case 1 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 2 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 3 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case 4 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case Else : ctrl = Nothing
    End Select

    ctrl.Visible = True
    'Display SOLD if item is sold'
    ctrl.FindControl("SoldDiv").Visible = isSold

End Sub

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

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

发布评论

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

评论(1

不知在何时 2024-09-03 14:48:01

好吧,我不确定是否有更好的答案,但由于没有人回答,我认为这是一种解决方法,而且似乎可行,尽管它并不像我想象的那么漂亮。

Private AdType As String ''# a property to store the Ad Type'
Private isSold As Boolean ''# a property to store whether or not the Ad is sold'

''# when the linq datasource is activated, it pulls the data out of the db'
''# and sets the two properties'
Protected Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
    Dim BizForSaleDC As New DAL.BizForSaleDataContext
    e.Result = BizForSaleDC.bt_BizForSale_GetByID(e.WhereParameters("ID")).FirstOrDefault

    AdType = e.Result.AdType
    isSold = e.Result.isSold
End Sub

''# when the page loads, the properties are already set'
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    ''# Get the right usercontrol'
    Dim ctrl As UserControl
    Select Case AdType ''# using the AdType property from above to decide which UserControl to enable'
        Case 1 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 2 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 3 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case 4 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case Else : ctrl = Nothing
    End Select

    ctrl.Visible = True
    ''# Display SOLD if item is sold'
    ctrl.FindControl("SoldDiv").Visible = isSold ''# using the isSold property from above to display the "SOLD" overlay'

End Sub

Well I'm not sure if there's a better answer out there, but since no one answered, I figured this as a work around and it seems to work, though it's not as pretty as I thought it should be.

Private AdType As String ''# a property to store the Ad Type'
Private isSold As Boolean ''# a property to store whether or not the Ad is sold'

''# when the linq datasource is activated, it pulls the data out of the db'
''# and sets the two properties'
Protected Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
    Dim BizForSaleDC As New DAL.BizForSaleDataContext
    e.Result = BizForSaleDC.bt_BizForSale_GetByID(e.WhereParameters("ID")).FirstOrDefault

    AdType = e.Result.AdType
    isSold = e.Result.isSold
End Sub

''# when the page loads, the properties are already set'
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    ''# Get the right usercontrol'
    Dim ctrl As UserControl
    Select Case AdType ''# using the AdType property from above to decide which UserControl to enable'
        Case 1 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 2 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 3 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case 4 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case Else : ctrl = Nothing
    End Select

    ctrl.Visible = True
    ''# Display SOLD if item is sold'
    ctrl.FindControl("SoldDiv").Visible = isSold ''# using the isSold property from above to display the "SOLD" overlay'

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文