如何在`<asp:gridview>`?</asp:gridview>中添加一个特殊行

时间:2015-04-14 10:01:06

标签: asp.net .net gridview aspxgridview

我想在<asp:GridView>的底部添加一个特殊行。此行的模板与上面的行不同。我该怎么做?

enter image description here

2 个答案:

答案 0 :(得分:2)

如果您一直想要添加&#39;您可以使用页脚模板。行位于表格底部:

<asp:TemplateField>
    <HeaderTemplate>
    </HeaderTemplate>
    <ItemTemplate>
    </ItemTemplate>
    <FooterTemplate>

    </FooterTemplate>
</asp:TemplateField>

答案 1 :(得分:2)

感谢@Alan和@lzzy。我是用<FooterTemplate>制作的。

<!-- ASPX -->
<asp:GridView ID="UserGridView" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="True" OnRowDeleting="UserGridView_OnRowDeleting" 
            OnRowCommand="UserGridView_OnRowCommand" ShowFooter="true">
            <Columns>
                <asp:TemplateField HeaderText="Username" ItemStyle-Width="70">
                    <ItemTemplate><asp:Label ID="Username" runat="server" Text='<%# Bind("Username") %>'/> </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox runat="server" ID="NewUsername"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Gender" ItemStyle-Width="70">
                    <ItemTemplate><asp:Label ID="Gender" runat="server" Text='<%# Bind("Gender") %>'/></ItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="NewGender" runat="server" >
                            <asp:ListItem>Male</asp:ListItem>
                            <asp:ListItem>Female</asp:ListItem>
                        </asp:DropDownList>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button runat="server" Text="Delete" CommandName="Delete"/>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Button ID="AddUserButton" runat="server" Text="Add" CommandName="Add"/>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

// CSS
protected void UserGridView_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Add")
    {
        TextBox newUsername = (TextBox) UserGridView.FooterRow.FindControl("NewUsername");
        DropDownList newGender = (DropDownList)UserGridView.FooterRow.FindControl("NewGender");
        AddUser(newGender.SelectedValue, newUsername.Text);
        BindUserData();
    }
}

protected void UserGridView_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
    string username = e.Values["Username"].ToString();
    string gender = e.Values["Gender"].ToString();
    DeleteUser(gender, username);
    BindUserData();
}

我遇到的其他一些问题和关键问题。如果有人需要它们。


如何将数据绑定到<asp:Label>
使用<%# Bind("DataFieldName") %>

示例:

<ItemTemplate><asp:Label ID="Username" runat="server" Text='<%# Bind("Username") %>'/> </ItemTemplate>

为什么页脚没有显示?
ShowFooter="true"中设置<asp:GridView>


如何在页脚中获取对控件的引用?
示例:

TextBox newUsername = (TextBox)UserGridView.FooterRow.FindControl("NewUsername");

如何将AddUserButton绑定为默认按钮?
示例:

MainForm.DefaultButton = UserGridView.FooterRow.FindControl("AddUserButton").UniqueID;