如何通过代码隐藏从 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我不确定是否有更好的答案,但由于没有人回答,我认为这是一种解决方法,而且似乎可行,尽管它并不像我想象的那么漂亮。
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.