在GridView中使用Hiddenfield时显示空列

时间:2016-03-24 22:21:32

标签: c# asp.net

我的GridView中有一个HiddenField。出现一个非常小的空列,我怎样才能摆脱空列?

<asp:GridView ID="GridView1"  autogeneratecolumns="false" runat="server" 
        OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
        autogenerateSelectButton="true">
        <Columns>                                           
            <asp:BoundField datafield ="Song"  headertext="Song"/>                
            <asp:BoundField datafield ="Album"  headertext="Album"/>
            <asp:BoundField datafield ="Artist"  headertext="Artist"/>
            <asp:BoundField datafield ="Genre"  headertext="Genre"/>
            <asp:BoundField datafield ="Price"  headertext="Price"/>
            <asp:BoundField datafield ="Explicit Lyrics"  headertext="Explicit Lyrics"/>
            <asp:TemplateField>                    
            <ItemTemplate>
                <asp:HiddenField ID="HiddenField" runat="server" Value='<%# Eval("SongID")%>'/>
            </ItemTemplate>
            </asp:TemplateField>                            
        </Columns>
    </asp:GridView>

这是输出

enter image description here

5 个答案:

答案 0 :(得分:1)

我认为您之后添加了隐藏字段以获取ID。无需添加和隐藏列。使用datakey

答案 1 :(得分:0)

您可以添加:

<style>
.hidden {display:none;}
</style>

将这些属性添加到asp:HiddenFieldItemTemplate元素 ItemStyle-CssClass="hidden"HeaderStyle-CssClass="hidden :)让我知道它是否有用。

答案 2 :(得分:0)

您可以将GridView的CellPadding和CellSpacing属性设置为零:

<asp:GridView CellPadding="0" CellSpacing="0" ... >

如果您仍然看到该列,则可以将其宽度设置为零:

<asp:TemplateField ItemStyle-Width="0" HeaderStyle-Width="0" ... >

答案 3 :(得分:0)

GridView中的TemplateField用于表示在数据绑定控件中显示自定义内容的字段,以及用于存储非显示值的HiddenField。

您正在创建一个带有非显示值的自定义内容列,这会导致创建一个列为前端的代表视图的列。因此,如果要获取GridView的选定索引更改的值,则可以使用以下方法:

方法1:使用Datakey for grideview在selecetedIndexchanged方法中存储所需的数据列:

<asp:GridView ID="GridView1"  autogeneratecolumns="false" runat="server" 
        OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
        autogenerateSelectButton="true" DataKeyNames="SongID">
        <Columns>                                           
            <asp:BoundField datafield ="Song"  headertext="Song"/>                
            <asp:BoundField datafield ="Album"  headertext="Album"/>
            <asp:BoundField datafield ="Artist"  headertext="Artist"/>
            <asp:BoundField datafield ="Genre"  headertext="Genre"/>
            <asp:BoundField datafield ="Price"  headertext="Price"/>
            <asp:BoundField datafield ="Explicit Lyrics"  headertext="Explicit Lyrics"/>                           
        </Columns>
    </asp:GridView>

要在SelectedIndexChanged方法上访问该密钥:

int songId= Convert.ToInt32(GridView1.DataKeys[GridView1.SelectedIndex].Values);

方法2:在gridview中的任何列中添加隐藏字段,而不是特殊的列。

<asp:GridView ID="GridView1"  autogeneratecolumns="false" runat="server" 
        OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
        autogenerateSelectButton="true">
        <Columns>                                           
            <asp:BoundField datafield ="Song"  headertext="Song"/>                
            <asp:BoundField datafield ="Album"  headertext="Album"/>
            <asp:BoundField datafield ="Artist"  headertext="Artist"/>
            <asp:BoundField datafield ="Genre"  headertext="Genre"/>
            <asp:BoundField datafield ="Price"  headertext="Price"/>
            <asp:TemplateField headertext="Explicit Lyrics" >                    
            <ItemTemplate>
                <asp:Lable runat="server" ID="lblexp" Text='<%# Eval("Explicit Lyrics")%>'>
                <asp:HiddenField ID="HiddenField" runat="server" Value='<%# Eval("SongID")%>'/>
            </ItemTemplate>
            </asp:TemplateField>                            
        </Columns>
    </asp:GridView>

答案 4 :(得分:0)

在Gridview的RowDataBound函数上,添加以下代码以隐藏所需的列。

例如:

在aspx中,

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">

在后面的代码中,

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[cellno.].Visible = false;
    } 
   if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[cellno.].Visible = false;
    }
}