【问题标题】:FormView Layout窗体视图布局
【发布时间】:2013-11-01 13:35:10
【问题描述】:

过去这让我发疯了几次,但我总是捏造它并继续前进。今天我想尝试修复它!

我经常在插入模式下创建一个GridView 来显示数据,并在其正下方创建一个FormView,其中InsertItemTemplate 中的控件对应于GridView 中的列,因此看起来FormView 只是GridView 的空行等待新数据输入。这很好用。

问题是我永远无法将FormView 中的“列”(实际上只是文本框)的宽度与上面的GridView 中的列的宽度相对应。

在下面的示例中,如您所见,GridViewFormView 的宽度均为 100%,而且,果然,当页面呈现时,它们的宽度完全相同(我给出了FormView 一个要检查的边框)。然而,即使FormView 中文本框的宽度与GridView 中列的宽度相同,它们也不会以这种方式显示。文本框稍宽,当您到达最右边的一列时,累积效应意味着对齐方式已经结束。

我猜这个问题与边框宽度或其他正在渲染的隐藏元素有关,但我无法弄清楚。我不得不说,对齐的数量似乎超过了边框会影响事物的几个像素。

<asp:GridView ID="gvTPR" runat="server" DataSourceID="SQLTPR" AutoGenerateColumns="false" DataMember="DefaultView" DataKeyNames="TPRID" Width="100%" >
<RowStyle HorizontalAlign="Center" />
<EditRowStyle BackColor="#ccffff" />
<HeaderStyle BackColor="#013b82" ForeColor="White" />
<Columns>
<asp:BoundField DataField="TPREnteredAt" HeaderText="Entered At" ReadOnly="True" SortExpression="TPREnteredAt" ItemStyle-Width="24%" ControlStyle-Width="90%" />
<asp:BoundField DataField="TPRTemp" HeaderText="Temp" ReadOnly="True" SortExpression="TPRTemp" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRPulse" HeaderText="Pulse" ReadOnly="True" SortExpression="TPRPulse" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRRespiration" HeaderText="Respiration" ReadOnly="True" SortExpression="TPRRespiration" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRPCV" HeaderText="PCV" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRTP" HeaderText="TP" ItemStyle-Width="12%" ControlStyle-Width="90%" />
<asp:CommandField ButtonType="Button" InsertVisible="False" ShowEditButton="True" ItemStyle-Width="16%" UpdateText="Save" ControlStyle-Width="60px" />
</Columns>
<EmptyDataTemplate>
No TPR records exist
</EmptyDataTemplate>
</asp:GridView>
<asp:FormView ID="fvTPR" runat="server" DataSourceID="SQLTPR" DefaultMode="Insert" Width="100%" >
<InsertItemTemplate>
<asp:textbox ID="lblEnteredAt" runat="server" Text="Will be added automatically" Width="24%" />
<asp:TextBox ID="txtTemp" runat="server" Text='<%# Bind("TPRTemp")%>' Width="12%" />
<asp:TextBox ID="txtPulse" runat="server" Text='<%# Bind("TPRPulse")%>' Width="12%" />
<asp:TextBox ID="txtRespiration" runat="server" Text='<%# Bind("TPRRespiration")%>' Width="12%" />
<asp:TextBox ID="txtPCV" runat="server" Text='<%# Bind("TPRPCV")%>' Width="12%" />
<asp:TextBox ID="txtTP" runat="server" Text='<%# Bind("TPRTP")%>' Width="12%" />                
<asp:Button ID="btnAddTPR" runat="server" Text="Save" Width="5%" />
</InsertItemTemplate>
</asp:FormView>

呈现如下:

【问题讨论】:

    标签: asp.net gridview formview


    【解决方案1】:

    如何使用 GridView 页脚添加功能?这样您就可以将所有列都放在同一个表中,并且可以解决定位问题。

    下面是您的 gridview 应该是什么样子的示例:

    <asp:GridView ID="gvTPR" runat="server" DataSourceID="SQLTPR" AutoGenerateColumns="False" ShowFooter="True" DataKeyNames="TPRID" Width="100%" EnableModelValidation="True">
    <RowStyle HorizontalAlign="Center" />
    <EditRowStyle BackColor="#ccffff" />
    <HeaderStyle BackColor="#013b82" ForeColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="Entered At" SortExpression="TPREnteredAt">
            <EditItemTemplate>
                <asp:TextBox ID="lblEnteredAt" runat="server" Text='<%# Eval("TPREnteredAt") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("TPREnteredAt") %>'></asp:Label>
            </ItemTemplate>
            <ControlStyle Width="90%" />
            <ItemStyle Width="24%" />
            <FooterTemplate>
                <asp:TextBox ID="lblEnteredAt" runat="server" Text="Will be added automatically" Width="24%" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Temp" SortExpression="TPRTemp">
            <EditItemTemplate>
                <asp:TextBox ID="txtTemp" runat="server" Text='<%# Eval("TPRTemp") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("TPRTemp") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtTemp" runat="server" Text='<%# Bind("TPRTemp")%>' Width="12%" />
            </FooterTemplate>
            <ControlStyle Width="90%" />
            <ItemStyle Width="12%" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Pulse" SortExpression="TPRPulse">
            <EditItemTemplate>
                <asp:TextBox ID="txtPulse" runat="server" Text='<%# Eval("TPRPulse") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("TPRPulse") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtPulse" runat="server" Text='<%# Bind("TPRPulse")%>' Width="12%" />
            </FooterTemplate>
            <ControlStyle Width="90%" />
            <ItemStyle Width="12%" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Respiration" SortExpression="TPRRespiration">
            <EditItemTemplate>
                <asp:TextBox ID="txtRespiration" runat="server" Text='<%# Eval("TPRRespiration") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Bind("TPRRespiration") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtRespiration" runat="server" Text='<%# Bind("TPRRespiration")%>' Width="12%" />
            </FooterTemplate>
            <ControlStyle Width="90%" />
            <ItemStyle Width="12%" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="PCV">
            <EditItemTemplate>
                <asp:TextBox ID="txtPCV" runat="server" Text='<%# Bind("TPRPCV") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label5" runat="server" Text='<%# Bind("TPRPCV") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtPCV" runat="server" Text='<%# Bind("TPRPCV")%>' Width="12%" />
            </FooterTemplate>
            <ControlStyle Width="90%" />
            <ItemStyle Width="12%" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="TP">
            <EditItemTemplate>
                <asp:TextBox ID="txtTP" runat="server" Text='<%# Bind("TPRTP") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label6" runat="server" Text='<%# Bind("TPRTP") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtTP" runat="server" Text='<%# Bind("TPRTP")%>' Width="12%" />
            </FooterTemplate>
            <ControlStyle Width="90%" />
            <ItemStyle Width="12%" />
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="lnkbtnEdit" runat="Server" Text="Edit" CommandName="Edit"
                    CausesValidation="false"></asp:Button>
    
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Button ID="lnkbtnUpdate" runat="Server" Text="Save" CommandName="Update"
                    CausesValidation="true"></asp:Button><br />
                <asp:LinkButton ID="lnkbtnCancel" runat="Server" Text="Cancel" CommandName="Cancel"
                    CausesValidation="false"></asp:LinkButton>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:LinkButton ID="lnkbtnInsert" runat="Server" Text="Save" CommandName="Insert"
                    CausesValidation="true"></asp:LinkButton>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
    <EmptyDataTemplate>
        No TPR records exist
    </EmptyDataTemplate>
    </asp:GridView>
    

    希望这会有所帮助!

    问候, 乌罗斯

    【讨论】:

    • 完美,谢谢!既然你提到它,我想我以前做过,但是谢谢你提醒我这么好!
    • 其实GridView控件不支持原生插入,其实还需要做更多的工作,不过我自己能搞定。
    猜你喜欢
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多