【问题标题】:GridView and Hidden FieldsGridView 和隐藏字段
【发布时间】:2023-03-31 00:52:01
【问题描述】:

我有一个 Web 应用程序,使用 VB/ASPX,在我的 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>

这部分有效,之后我将第一列“id_session”隐藏在我的 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 代码中获取这个隐藏列的值,我正在尝试不同的方式但没有>

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 

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

【问题讨论】:

  • 检查下面的解决方案,如果有任何帮助,请告诉我

标签: asp.net vb.net gridview


【解决方案1】:

使用隐藏区域

尝试以下代码,在最火的列中使用文本框

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

它不会显示在 UI 上,您可以轻松地从中访问值 使用此代码,您不需要网格的 RowCreated 事件

亲爱的删除此代码

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

并将隐藏字段放入您要显示的其他项目模板中

【讨论】:

  • 当使用 HiddenField 并且没有 RowCreated 事件时,该列保持可见! image.noelshack.com/fichiers/2014/19/1399361323-sans-titre2.png
  • 亲爱的你必须删除 txtbox 和链接控件表单然后网格孩子请在放置隐藏字段后显示你的代码
  • &lt;asp:TemplateField AccessibleHeaderText="id_session" HeaderText="id_session"&gt; &lt;EditItemTemplate&gt; &lt;asp:HiddenField ID="hf_sessionId" runat="server" Value='&lt;%# Bind("id_session") %&gt;' /&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:HiddenField ID="hf_sessionId" runat="server" Value='&lt;%# Bind("id_session") %&gt;' /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; 这是我的代码,我找到了另一种方法,但我想看看如何使用您的解决方案!
【解决方案2】:

像这样更改您的代码,而不是“GridView1.Columns(0).Visible = False”,

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

【讨论】:

    【解决方案3】:

    我终于找到了这个方法:

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

    它工作!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 2012-08-09
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      相关资源
      最近更新 更多