GridView和隐藏字段

时间:2014-05-06 06:38:05

标签: asp.net vb.net gridview

我在VB / ASPX中有一个Web应用程序,我的aspx文件中有一个带有SqlDataSource的GridView填充。像这样:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" SkinID="dataGrid" onrowcommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField AccessibleHeaderText="id_session" HeaderText="id_session">
            <EditItemTemplate>
                <asp:TextBox ID="txt_id_session" runat="server" Text='<%# Bind("id_session") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lbl_id_session" runat="server" Text='<%# Bind("id_session") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>




    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Formation_2014ConnectionString %>" 
        SelectCommand="SELECT s.id_session, f.libelle_formation, s.date_debut_session, s.date_fin_session, COUNT(p.id_personne) AS Expr1 FROM Sessions AS s LEFT OUTER JOIN Participe AS p ON p.id_session = s.id_session AND p.actif = 1 RIGHT OUTER JOIN Formation AS f ON f.id_formation = s.id_formation WHERE (s.date_fin_session > GETDATE()) OR (s.date_fin_session < GETDATE()) OR (S.date_fin_session = GETDATE()) GROUP BY s.id_session, f.libelle_formation, s.date_debut_session, s.date_fin_session" >
    </asp:SqlDataSource>

此部分有效,之后我隐藏了第一列,&#34; id_session&#34;在我的CodeBehind中使用此代码:

 Protected Sub OnRowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowCreated
        GridView1.Columns(0).Visible = False
        'e.Row.Cells(0).Visible = False 'This way delete my paging
        (Requête)
    End Sub

之后,我必须在我的vb代码中获取此隐藏列的值,我尝试不同的方式,但没有&gt;&lt;

Dim id_session = GridView1.SelectedRow.Cells(0).Controls(0).ToString 'Return System.Web.UI.Literalcontrol
Dim id_session = GridView1.SelectedRow.RowIndex 'Return number of line 

对不起我的英文,我是法国人!

3 个答案:

答案 0 :(得分:0)

使用Hiddenfield

尝试以下代码,在firest列中使用textbox

<asp:HiddenField ID="hf_sessionId" runat="server" Value='<%# Bind("id_session") %>' />

它不会显示给用户界面,您可以轻松地从中访问该值 使用此代码,您不需要网格的RowCreated事件

亲爱的删除此代码

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

并将hiddenfield放入要显示的其他itemtemplate

答案 1 :(得分:0)

而不是&#34; GridView1.Columns(0).Visible = False&#34;像这样改变你的代码,

GridView1.Columns(0).style("display")="none"

答案 2 :(得分:0)

我终于找到了这个方法:

Dim id_session = (CType(GridView1.SelectedRow.Cells(0).Controls(1), Label)).Text

工作!