【问题标题】:Iterating through EditItemTemplate textboxes in asp:Gridview在 asp:Gridview 中遍历 EditItemTemplate 文本框
【发布时间】:2015-09-25 14:17:56
【问题描述】:

我有一个在模板字段中显示数据的网格视图,它需要通过单击链接按钮来显示有关要显示的记录的更多信息。现在,我让“详细信息”链接按钮通过调用 gridview 上的编辑命令来显示信息,以便它切换到 EditItemTemplate。在 EditItemTemplate 中,我有一个用于取消的链接按钮,然后是一个编辑按钮,单击该按钮时会显示带有更新命令的更新按钮,但我需要它遍历该行并将 EditItemTemplate 中的所有文本框设置为 ReadOnly=false 所以以便在选择更新命令之前对其进行编辑。以下是代码摘要:

<ItemTemplate>
    *basic information displayed*
    <asp:LinkButton runat="server" CommandName="Edit" Text="Details"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
    *A bunch of readonly textboxes that display the extra information*
    <asp:LinkButton runat="server" CommandName="Update" Text="Update" Visible="false"></asp:LinkButton> 
    <asp:LinkButton runat="server" Text="Edit" OnClick="editButton_Click"></asp:LinkButton>
</EditItemTemplate>

以及使按钮按我想要的方式显示的事件代码,但我不确定如何遍历 EditItemTemplate,或者即使这是我应该做的:

 Protected Sub editButton_Click(sender As Object, e As EventArgs)
        sender.FindControl("updateButton").Visible = True
        sender.FindControl("editbutton").Visible = False
        For Each t In ?EditItemTemplate?
            Dim textbox = TryCast(t, System.Web.UI.WebControls.TextBox)
            If textbox IsNot Nothing Then
                textbox.ReadOnly = False
            End If
        Next
End Sub

所以我的问题是如何让它工作,或者我应该如何设置 GridViewCommands 否则

【问题讨论】:

  • 也许利用 GridView 提供的默认机制会更有意义。每个需要可编辑的列都有一个项目模板和一个编辑模板,只需使用“编辑”按钮触发“编辑”命令。这样 GridView 中的相应行将使用编辑模板呈现每一列,这正是您所需要的

标签: asp.net vb.net gridview


【解决方案1】:

您应该在EditItemTemplate 中使用PlaceHolder。将所有控件/链接按钮放在此占位符内。

<EditItemTemplate>

<asp:PlaceHolder ID="TestPlaceHolder" runat="server">

// Sample Link Buttons
    <asp:LinkButton runat="server" CommandName="Update" Text="Update"
         Visible="false"></asp:LinkButton> 
    <asp:LinkButton runat="server" Text="Edit" OnClick="editButton_Click"></asp:LinkButton>
// Sample Text Box
   <asp:TextBox runat="server" ID="FirstName" ...>...</TextBox>

</asp:PlaceHolder>

</EditItemTemplate>

处理GridView的RowEditing事件。在编辑事件处理程序中,首先找到占位符,然后使用PlaceHolder's Controls 属性遍历控件...

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {
     // Get the Placeholder for the row being edited.  
        PlaceHolder testPlacehldr =
        GridView.Rows[e.NewEditIndex].FindControl("TestPlaceholder") as PlaceHolder;
       // Iterate over the controls
         if(testPlacehldr.Controls.Count > 0)
             {    
                foreach (Control ctrl in testPlacehldr.Controls)
                   {
                       if (ctrl is LinkButton)
                           { 
                              LinkButton lnkBtn = ctrl as LinkButton
                                if(lnkBtn.Text== "Update")
                                  {
                                    lnkBtn.Visible = false;
                                  }
                                // More IF conditions follow....
                           }

                   if (ctrl is TextBox)
                       { 
                        TextBox txtBox = ctrl as TextBox;
                        if(txtBox.ID == "FirstName")// use any property of TexBox you prefer
                                  {
                                    txtBox.ReadOnly= true;
                                  }
                           // More IF conditions follow....
                          }
                   }    
             }         
        //At the End, set the EditIndex and Bind the data
          GridView1.EditIndex = e.NewEditIndex;
          BindGridViewData();
   }

我希望您现在可以自己锻炼逻辑以隐藏/显示控件。

【讨论】:

  • 占位符肯定有帮助,我自己的问题是如何通过编辑按钮单击上的这些控件来重申,以使文本框显示为可写。您可以从按钮单击事件中调用 GridViewEditEventArgs 吗?怎么样?
【解决方案2】:

所以我想出了如何通过使用 EditItemTemplate 中的占位符来做到这一点(在 vb 中需要它),这是它背后的代码:

Protected Sub editButton_Click(sender As Object, e As EventArgs)
    sender.FindControl("editbutton").Visible = False
    sender.FindControl("updateButton").Visible = True

    Dim testPlacehldr As PlaceHolder = sender.FindControl("TestPlaceholder")
    If testPlacehldr.Controls.Count > 0 Then
        Dim btn As LinkButton = sender.FindControl("editButton")
        If btn.Visible = False Then
            For Each ctrl As Control In testPlacehldr.Controls
                If ctrl.GetType Is GetType(TextBox) Then
                    Dim box As TextBox = TryCast(ctrl, TextBox)
                    box.ReadOnly = False
                End If
            Next
        End If
    End If
End Sub

这很适合我需要它做的事情。归功于用户 R.C.关于占位符的想法

【讨论】:

    猜你喜欢
    • 2011-06-19
    • 1970-01-01
    • 2016-09-28
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多