通过asp:Gridview

时间:2015-09-25 14:17:56

标签: asp.net vb.net gridview

我有一个gridview在模板字段中显示数据,需要通过单击链接按钮显示有关要显示的记录的更多信息。现在,我有"详细信息" linkbutton通过调用gridview上的edit命令显示信息,以便切换到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

2 个答案:

答案 0 :(得分:1)

您应该在EditItemTemplate中使用PlaceHolder。将所有Controls / LinkBut​​ton放在此占位符中。

<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();
   }

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

答案 1 :(得分:1)

所以我想通过在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.关于占位符的想法