如何在OnInit函数中插入回调函数?

时间:2018-09-26 05:53:58

标签: asp.net vb.net devexpress

我有一个包含可点击行的gridview列。单击该行时,将出现一个弹出的网格视图。例如:当我单击该行时,将根据ID出现弹出的gridview。 **行猫ID-弹出猫ID的gridview **

我的代码适用于显示所有ID的弹出gridview,但是当我如下插入asp.net时,单击asp.net,然后弹出popview不弹出,并且当我在ASPxHyperlink_Init中插入回调函数时,就这样

link.ClientSideEvents.Click = String.Format("function(s, e) {{ OnMoreInfoClick('{0}');UpdateSplitGrid.PerformCallback(""" & Eval("LotID").ToString & """); }}", sno)

然后显示此错误

System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

我的代码如下:

ASP.NET标记

   function OnMoreInfoClick(contentUrl) {

                popupControl.Show();

            }

   <dx:GridViewDataTextColumn  VisibleIndex="8" Caption="Detail" EditFormSettings-Visible="False" >
                                     <DataItemTemplate>
                                        <dx:ASPxHyperLink ID="ASPxHyperLink" runat="server" OnInit="ASPxHyperLink_Init" >
                                            <ClientSideEvents Click= '<%# "function(s,e) { UpdateSplitGrid.PerformCallback(""" & Eval("LotID").ToString & """); }" %>'></ClientSideEvents>
                                        </dx:ASPxHyperLink>
                                     </DataItemTemplate>
                                    </dx:GridViewDataTextColumn>

VB.NET

    Protected Sub ASPxHyperLink_Init(sender As Object, e As EventArgs)

        Dim link As ASPxHyperLink = CType(sender, ASPxHyperLink)

        Dim templateContainer As GridViewDataItemTemplateContainer = CType(link.NamingContainer, GridViewDataItemTemplateContainer)

        Dim rowVisibleIndex As Integer = templateContainer.VisibleIndex
        Dim sno As String = templateContainer.Grid.GetRowValues(rowVisibleIndex, "sno").ToString()
        Dim SplitTable As String = templateContainer.Grid.GetRowValues(rowVisibleIndex, "Detail").ToString()
        ' Dim contentUrl As String = String.Format("{0}?sno={1}", sno)

        link.Text = "Split Table"

        link.NavigateUrl = "javascript:void(0);"

        link.ClientSideEvents.Click = String.Format("function(s, e) {{ OnMoreInfoClick('{0}');}}", sno)

    End Sub



   Protected Sub UpdateSplitGrid_CustomCallback(sender As Object, e As ASPxGridViewCustomCallbackEventArgs)

        Dim mssql_string As String = "Data Source=xx;Initial Catalog=xx;User Id=xx;Password=xx;Provider=SQLNCLI10"
        Dim con As New OleDbConnection(mssql_string)
        Dim dt1 As New DataTable
        Dim xdapter As New OleDbDataAdapter

        con.Open()

        Dim cmd As New OleDbCommand("select * from PTHOME.dbo.SplitTable where LotID = '" + e.Parameters + "'", con)

        xdapter.SelectCommand = cmd

        xdapter.Fill(dt1)

        con.Close()

        UpdateSplitGrid.DataSource = dt1
        UpdateSplitGrid.DataBind()

    End Sub

请对此进行指导,谢谢。

1 个答案:

答案 0 :(得分:0)

此行代码抛出InvalidOperationException,因为Click客户端事件既不支持Bind()也不支持Eval()的数据绑定:

<ClientSideEvents Click= '<%# "function(s,e) { UpdateSplitGrid.PerformCallback(""" & Eval("LotID").ToString & """); }" %>'></ClientSideEvents>

但是,据我所知,您可以创建一个将超链接作为GridViewDataItemTemplateContainer并在其中放置索引的方法:

Protected Function GetRowIndex(ByVal link As DevExpress.Web.ASPxHyperLink) As Integer
    Return (TryCast(link.NamingContainer, DevExpress.Web.GridViewDataItemTemplateContainer)).VisibleIndex
End Function

然后您可以在Click事件中将其用于客户端,如下例所示:

<ClientSideEvents Click='<%# "function(s,e) { UpdateSplitGrid.PerformCallback(""" & GetRowIndex(Container).ToString & """); }" %>'>
</ClientSideEvents>

附加说明:

CustomCallback事件处理程序内部,如果要根据其列名获取某些行值,请在下面使用此行:

Dim row As Object = UpdateSplitGrid.GetRowValues(Convert.ToInt32(e.Parameters), "LotID");

然后,您可以使用提供的值(请注意,使用SqlParameter而不是字符串串联来插入查询值)。

类似的问题(使用ASPxMenu代替ASPxHyperLink

ASPxGridView - How to pass a parameter to the PerformCallback method from the markup expression