How to get record value from LinqDataSource via Code Behind

Posted by rockinthesixstring on Stack Overflow See other posts from Stack Overflow or by rockinthesixstring
Published on 2010-03-29T03:13:44Z Indexed on 2010/03/29 3:23 UTC
Read the original article Hit count: 837

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about linqdatasource

Related posts about LINQ