在asp:Gridview中迭代edittitemtemplate文本框



我有一个网格视图显示数据内的模板字段,需要更多的信息,有关的记录,通过点击一个链接按钮显示。现在,我让"details"链接按钮通过调用gridview上的edit命令来显示信息,以便它切换到EditItemTemplate。在EditItemTemplate中,我有一个用于取消的链接按钮,然后是一个编辑按钮,当单击该按钮时,将显示带有update命令的更新按钮,但是我需要它遍历该行,并将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>

和事件的代码,使按钮出现我想要的方式,但我不确定如何通过edittitemtemplate迭代,或者即使这是我应该做的:

 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否则

您应该在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();
   }

我希望你现在可以自己练习隐藏/显示控件的逻辑。

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

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.关于占位符

的想法

相关内容

  • 没有找到相关文章

最新更新