如果行为空,ASP.NET GridView显示文本框

如果行为空,ASP.NET GridView显示文本框,asp.net,Asp.net,我的GridView数据绑定到一个SQL数据连接,该连接几乎总是返回数据。因此,使用EmptyDataTemplate对我没有帮助,因为GridView永远不应该为空。但我希望前几行是可编辑的,以便用户可以向GridView添加新信息。因此,我精心编制了Select语句,使其始终返回3个空行。我希望这些行包含文本框而不是标签。但这是: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="Fals

我的GridView数据绑定到一个SQL数据连接,该连接几乎总是返回数据。因此,使用EmptyDataTemplate对我没有帮助,因为GridView永远不应该为空。但我希望前几行是可编辑的,以便用户可以向GridView添加新信息。因此,我精心编制了Select语句,使其始终返回3个空行。我希望这些行包含文本框而不是标签。但这是:

<asp:GridView ID="GridView1" runat="server" 
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
        BorderColor="White" BorderStyle="Solid" 
        onselectedindexchanged="GridView1_SelectedIndexChanged" ShowFooter="False" 
        ViewStateMode="Disabled">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <input type="checkbox" id ="CheckBox1" class="checkbox" />
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Serial" SortExpression="Serial">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Serial") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Model" SortExpression="Model">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Model") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="From Store">
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" CssClass="dropdownlist" 
                        DataSourceID="SqlDataSource2" DataTextField="Store" 
                        SelectedValue='<%# Bind("Store") %>'>
                    </asp:DropDownList>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="To Store">
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server" CssClass="dropdownlist" 
                        DataSourceID="SqlDataSource2" DataTextField="Store">
                    </asp:DropDownList>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
        </Columns>
        <FooterStyle HorizontalAlign="Center" ForeColor="White" BorderColor="White" BorderStyle="Solid" />
        <HeaderStyle ForeColor="White" BorderColor="White" BorderStyle="Solid" />
        <RowStyle BorderColor="White" BorderStyle="Solid" ForeColor="White" />            
    </asp:GridView>

产生以下结果:


其中前3行具有不可编辑的标签,而不是文本框。我想做的是可能的吗?

在模板字段中添加
这使gridview的页脚行成为可以添加新行的位置。当然,您需要将项目放入
中,但工作与
相同,因此为了满足我的特定目的,我决定创建一个与此分离的表,其中有一个带有文本框的空行。然后是一个按钮,该按钮使用JQuery从该表中获取值,并将它们附加到DataGrid中不可编辑的行中。从DataGrid中删除了标题行,使其看起来像同一个表

如果您一直强制这三行处于编辑模式,网格应该在相应的列中显示标签的文本框。检查GridView1.EditIndex属性库。你有任何例子或链接,因为我真的不知道你的意思吗?这将只添加一个可编辑的行,虽然正确?我需要多个。它将只创建一个可编辑行,正确。然而,从用户的角度来看,在顶部有三行的方法是令人困惑的:1。混合现有数据(底部数据)和不存在数据(三个空行)2。不存在的数据有一个复选框,当他们试图对那些选定的不存在的项执行操作时,这可能会导致问题。另外,有一行意味着你只需要控制一行而不是三行验证器(如果你正在使用验证器)。只有我的两分钱。我同意你的观点,并考虑过以这种方式使用页脚。但我的用户可能需要添加多个项目。因此,如果有一种方法可以让我将FooterRow作为一个数据行,并在填充后将其转换为一个DataRow,这样一个新的空白和可编辑的FooterRow就变得可用了,那将是理想的。但是,由于用户输入的信息不会立即插入或更新到SqlDataSource中,因此我无法重新绑定GridView来实现这一点。