GridView中的LinkBut​​ton不会触发RowCommand

时间:2014-05-27 19:43:28

标签: ajax gridview linkbutton findcontrol rowcommand

我正在动态构建数据表并将其绑定到VB代码隐藏中的gridview。我将gridview单元格数据转换为LinkBut​​ton控件,以允许单击该数据进行更新。

我正在使用AjaxControlToolkit来呈现模式弹出窗口来收集数据更新。完成这些更新后,我重建数据表并对gridview进行数据绑定。在Gridview.RowCreated中,我重复在回发时创建LinkBut​​ton控件。

在更新后显示页面时,当我单击LinkBut​​ton时,会发生回发但不会触发RowCommand。在调试时,我注意到当发生这种情况时,Page.FindControl()无法找到目标LinkBut​​ton控件。

返回页面时不触发RowCommand事件以触发模式弹出窗口的显示。如果我只是第二次按下LinkBut​​ton,可以使用Page.FindControl()找到LinkBut​​ton控件,并根据需要触发RowCommand。

我错过了什么?非常感谢您解决此问题的任何想法。我整天都在苦苦挣扎,而且我的想法已经不多了。

谢谢!

这是请求的代码:

<asp:GridView ID="CustomeGridview" GridLines="Vertical" Width="100%" runat="server"
          BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
          CellPadding="3" AutoGenerateColumns="true" ForeColor="Black" ShowHeader="False" Font-Names="Verdana"
          Font-Size="11px" OnRowCommand="CustomeGridview_RowCommand"
          OnRowDataBound="CustomeGridview_RowDataBound">
          <FooterStyle BackColor="#CCCCCC" />
          <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
          <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
          <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
          <AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Response.CacheControl = "no-cache"
    Response.Expires = -1
    Response.AddHeader("Pragma", "no-cache")

    If Page.IsPostBack = False Then
         ViewState("Left") = 3
         ViewState("Right") = 6
         generateManualDt()
    Else
         Dim controlName As String = Request.Params.Get("__EVENTTARGET")
         Dim ctl As Control = Page.FindControl(controlName)
    End If

    Page_Header_Label.Text = "Action Qualification Matrix - " & Session("CurrentServiceName")

    FormatColumns()

End Sub


Public Sub generateManualDt()

    Dim ManualDt As DataTable = BuildMainDataTable()

    ViewState("ManualDt") = ManualDt
    CustomeGridview.DataSource = ManualDt
    CustomeGridview.DataBind()

    SetJumpScrollButtons()

End Sub



Protected Sub CustomeGridview_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles CustomeGridview.RowDataBound

    Dim dt As DataTable = DirectCast(ViewState("ManualDt"), DataTable)
    Dim dtsnocol As DataTable = DirectCast(ViewState("dtsnocol"), DataTable)

    If e.Row.RowType = DataControlRowType.DataRow OrElse e.Row.RowType = DataControlRowType.Header Then
        For gvCol As Integer = 7 To dt.Columns.Count - 1
            e.Row.Cells(gvCol).Visible = False
        Next
        SetupGridRowControls(e, dtsnocol, dt)
    End If

End Sub

Protected Sub CustomeGridview_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles CustomeGridview.RowCreated

    Dim dt As DataTable = DirectCast(ViewState("ManualDt"), DataTable)
    Dim dtsnocol As DataTable = DirectCast(ViewState("dtsnocol"), DataTable)

    e.Row.Cells(0).Visible = False      ' Hide the QuestionID column
    If Page.IsPostBack Then
        If e.Row.RowType = DataControlRowType.DataRow OrElse e.Row.RowType = DataControlRowType.Header Then
            SetupGridRowControls(e, dtsnocol, dt)
        End If
    End If

End Sub



Private Sub SetupGridRowControls(ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs, ByRef dtsnocol As DataTable, ByRef mainDt As DataTable)

    Dim CurrSnoCount As Integer = 0
    If Not dtsnocol Is Nothing Then
        CurrSnoCount = dtsnocol.Rows.Count
    End If

    If e.Row.DataItemIndex = 0 Then         ' Scenario Number row

        Dim snoLabel As New Label()
        snoLabel.Text = "SCENARIO "
        snoLabel.ID = "snoHdrLabel"
        e.Row.Cells(2).Controls.Add(snoLabel)

        Dim addScenarioBtn = buildAddButton("AddScenario", "", "Click to add a new scenario column")
        e.Row.Cells(2).Controls.Add(addScenarioBtn)

    ElseIf e.Row.DataItemIndex = 1 Then     ' Question Header and Sno Descriptions
        Dim questLabel As New Label()
        questLabel.Text = "QUESTIONS "
        questLabel.ID = "questdrLabel"
        e.Row.Cells(2).Controls.Add(questLabel)

        Dim addQuestionBtn = buildAddButton("AddQuestion", "", "Click to add a new question row")
        e.Row.Cells(2).Controls.Add(addQuestionBtn)

        Dim cellText As String = ""
        Dim snoId As String = ""
        For snoRowIdx As Integer = 0 To CurrSnoCount - 1
            snoId = dtsnocol.Rows(snoRowIdx)("ScenarioID").ToString()
            cellText = dtsnocol.Rows(snoRowIdx)("Note").ToString()
            Dim lnkBtn = buildLinkButton("EditScenario", snoId, cellText)
            e.Row.Cells(snoRowIdx + 3).Controls.Add(lnkBtn)
        Next

    ElseIf e.Row.DataItemIndex > 1 Then     ' Question / Responses
        Dim cellText As String = ""
        Dim cellAddr As String = ""
        Dim snoId As String = ""
        Dim questId As String = mainDt.Rows(e.Row.DataItemIndex).Item(0).ToString.Trim
        For snoRowIdx As Integer = 0 To CurrSnoCount - 1
            snoId = dtsnocol.Rows(snoRowIdx)("ScenarioID").ToString.Trim
            cellAddr = String.Format("{0}:{1}", questId, snoId)
            cellText = mainDt.Rows(e.Row.DataItemIndex).Item(snoRowIdx + 3).ToString.Trim
            Dim lnkBtn = buildLinkButton("EditResponses", cellAddr, cellText)
            e.Row.Cells(snoRowIdx + 3).Controls.Add(lnkBtn)
        Next

    End If

End Sub


Private Function buildLinkButton(ByVal linkCmd As String, ByVal linkCmdArg As String, ByVal linkText As String) As LinkButton

    Dim lnkBtn As New LinkButton()
    lnkBtn.Text = linkText
    lnkBtn.CommandName = linkCmd
    lnkBtn.CommandArgument = linkCmdArg
    lnkBtn.ToolTip = "Click to update"
    lnkBtn.CssClass = "cell_link"        

    Return lnkBtn

End Function


Private Function buildAddButton(ByVal btnCmd As String, ByVal btnCmdArg As String, ByVal btnToolTip As String) As ImageButton

    Dim imgBtn As New ImageButton()
    imgBtn.CommandName = btnCmd
    imgBtn.CommandArgument = btnCmdArg
    imgBtn.ImageUrl = "~/Images/services/SmallAddBtn.gif"
    imgBtn.ToolTip = btnToolTip
    imgBtn.ID = String.Format("{0}_GenImageButton", btnCmd)

    Return imgBtn

End Function

0 个答案:

没有答案