GridView中的ASP.NET LinkBut​​ton导致完全回发

时间:2014-12-02 21:47:28

标签: asp.net gridview ajaxcontroltoolkit linkbutton

这个真的让我疯了。我已经阅读并尝试了大部分的解决方法,但仍然会回复!!

所有标记都是从代码隐藏中动态生成的,并从init事件插入到母版页的一部分中。

有一系列嵌套选项卡,选项卡内容大部分是GridView中的数据。每个Gridview都在其自己的更新面板中设置。每次单击GridView中的LinkBut​​ton时,都会有一个完整的回发导致选项卡重置(GridView外部的任何按钮和UpdatePanel中的任何按钮都不会导致完整的回发。

LinkBut​​tons就像这样生成

 Dim Select_Field As New CommandField
        With Select_Field
            .HeaderText = "View Transactions"
            .SelectText = "View Transactions"
            .ShowSelectButton = True
        End With
        GV.Columns.Add(Select_Field)

注册ToolKitScriptManager

Private Sub AssessmentsMain_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    Try
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim vID As Integer = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ID"))
            Dim LB As LinkButton = CType(e.Row.Cells(4).Controls(0), LinkButton)
            LB.ID = "AssMain_" & vID
            AjaxControlToolkit.ToolkitScriptManager.GetCurrent(Me).RegisterAsyncPostBackControl(LB)
        End If
    Catch ex As Exception
        Dim vError As New SendError
        vError.MailError("840", PageName, ex)
        ShowError()
    End Try
End Sub

并且生成的一行标记就像这样

<tr class="GridView" style="color:#333333;background-color:#F7F6F3;">
<td>10</td>
<td>Adam White</td>
<td>4224 Res Road</td>
<td align="right">$6,850.65</td>
<td>
<a id="ctl00_ContentPlaceHolder1_AssessmentsMainGV_ctl02_AssMain_10"  href="javascript:__doPostBack(&#39;ctl00$ContentPlaceHolder1$AssessmentsMainGV&#39;,&#39;Select$0&#39;)" style="color:#333333;">View Transactions</a>

   

GridView类

Public Class HAS_Gridview
    Inherits GridView
    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)
        CellPadding = 4
        GridLines = WebControls.GridLines.None
        ForeColor = Drawing.ColorTranslator.FromHtml("#333333")
        ClientIDMode = UI.ClientIDMode.AutoID
        With MyBase.FooterStyle
            .BackColor = Drawing.ColorTranslator.FromHtml("#E2DED6")
            .Font.Bold = True
            .ForeColor = Color.White
        End With
        RowStyle.BackColor = Drawing.ColorTranslator.FromHtml("#F7F6F3")
        RowStyle.ForeColor = Drawing.ColorTranslator.FromHtml("#333333")
        PagerStyle.HorizontalAlign = WebControls.HorizontalAlign.Center
        PagerStyle.ForeColor = Color.Black
        PagerStyle.BackColor = Drawing.ColorTranslator.FromHtml("#E2DED6")
        With MyBase.SelectedRowStyle
            .BackColor = Drawing.ColorTranslator.FromHtml("#E2DED6")
            .Font.Bold = True
            .ForeColor = Drawing.ColorTranslator.FromHtml("#333333")
        End With
        With MyBase.HeaderStyle
            .BackColor = Drawing.ColorTranslator.FromHtml("#E2DED6")
            .Font.Bold = True
            .ForeColor = Color.Black
            .CssClass = "GridView"
        End With
        EditRowStyle.BackColor = Drawing.ColorTranslator.FromHtml("#999999")
        With MyBase.AlternatingRowStyle
            .BackColor = Color.White
            .ForeColor = Drawing.ColorTranslator.FromHtml("#284775")
            .CssClass = "GridView"
        End With
        With MyBase.RowStyle
            .CssClass = "GridView"
        End With
    End Sub

End Class

3 个答案:

答案 0 :(得分:0)

您是否尝试过以编程方式添加触发器而不是在AjaxControlToolkit中注册:

Dim trigger As New AsyncPostBackTrigger()
trigger.ControlID = LB.ID
trigger.EventName = "Click"
MyUpdatePanel.Triggers.Add(trigger)

答案 1 :(得分:0)

回传是由生成的锚标记标记中的href="javascript:__doPostBack(&#39;ctl00$ContentPlaceHolder1$AssessmentsMainGV&#39;,&#39;Select$0&#39;)"引起的。

尝试定义自己的_doPostBack。

如果您尚未检查post apost b,则可以通过以下几种方式进行检查。

答案 2 :(得分:0)

最后,我能让这个工作的唯一方法是

为LinkBut​​ton模板创建一个类

Public Class LinkButtonTemplate
    Implements ITemplate
    Private m_ColumnName As String
    Event LinkButtonItem_Clicked(sender As LinkButton, e As EventArgs)
    Public Property ColumnName() As String
        Get
            Return m_ColumnName
        End Get
        Set(ByVal value As String)
            m_ColumnName = value
        End Set
    End Property
    Public Sub New()
    End Sub
    Public Sub New(ByVal ColumnName As String)
        Me.ColumnName = ColumnName
    End Sub
    Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
        Dim LB As New LinkButton()
        With LB
            .ID = "LB_" & ColumnName
            .Text = ColumnName
            .OnClientClick = "LinkButtonClick(this);"
       End With
      container.Controls.Add(LB)
    End Sub
 End Class

添加此javascript,以便将ID发布到隐藏字段并单击隐藏按钮

Private Sub LoadLinkButtonClick()
    Try
        Dim SB As New StringBuilder
        SB.Append("function LinkButtonClick(LinkButton){ ")
        SB.Append("setTimeout(function() { ")
        SB.Append("$get('" & GridViewLBClicked.ClientID & "').click(); ")
        SB.Append(" }, 300); ")
        SB.Append("var sendingID = LinkButton.id; ")
        SB.Append("document.getElementById('" & HiddenField1.ClientID & "').value = sendingID; ")
        SB.Append("} ")
        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "LoadLinkButton", SB.ToString, True)
    Catch ex As Exception
        Dim vError As New SendError
        vError.MailError("1229", PageName, ex)
    End Try
End Sub

在RowDataBound事件上捕获我们需要的ID并将其添加到LinkBut​​ton ID(现在我们知道发送GridView和所选行)

Private Sub AssessmentsMain_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    Try
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim vID As Integer = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ID"))
            Dim LB As LinkButton = CType(e.Row.Cells(4).Controls(0), LinkButton)
            LB.ID = "AssMain_" & vID
        End If
    Catch ex As Exception
        Dim vError As New SendError
        vError.MailError("840", PageName, ex)
        ShowError()
    End Try
End Sub

从隐藏按钮单击,我们可以恢复进行异步回发所需的所有数据

Private Sub RaiseLinkButton_Click(sender As Object, e As EventArgs)
    Try
        Dim vFunction As New Functions
        Dim vValue As String = HiddenField1.Value
        Dim vSplit = vValue.Split("_")
        Dim i As Integer = 0
        For Count As Integer = 0 To vSplit.Length - 1
            i += 1
        Next
        Dim DGName As String = vSplit(i - 2)
        Select Case DGName
            Case "AssMain"
                Dim MV As MultiView = vFunction.FindControlRecursive(BodyMain, "AssessmentMultiView")
                Dim GV As CustomControl.HAS_Gridview = vFunction.FindControlRecursive(BodyMain, "AssessmentDetailGV") 
                With GV
                    .DataSource = AssessmentsDetail_ReturnDataSet()
                    .DataBind()
                    .PageIndex = 0
                End With

                MV.ActiveViewIndex = 1

                Dim vPanel As UpdatePanel = vFunction.FindControlRecursive(BodyMain, "AssessmentUpdatePanel")
                vPanel.Update()


        End Select

    Catch ex As Exception
        Dim vError As New SendError
        vError.MailError("953", PageName, ex)
        ShowError()
    End Try
End Sub