如何通过代码隐藏从LinqDataSource获取记录值
我有一个LinqDataSource可以检索一条记录如何通过代码隐藏从LinqDataSource获取记录值,linq,code-behind,linqdatasource,Linq,Code Behind,Linqdatasource,我有一个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
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>
编辑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
嗯,我不确定是否有更好的答案,但由于没有人回答,我认为这是一个解决办法,似乎是可行的,尽管它没有我想象的那么漂亮
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
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