2014-05-06 67 views
1

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

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 

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

+0

检查下面的解决方案,为任何帮助只是让我知道 – Developerzzz

回答

0

我终于找到了其他的ItemTemplate这种方法:

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

它的工作!

0

使用Hiddenfield

试试下面的代码insted的在firest列

<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到要显示

+0

当使用HiddenField,并没有RowCreated事件,列保持可见! http://image.noelshack.com/fichiers/2014/19/1399361323-sans-titre2.png – Krishnak

+0

亲爱的你必须删除txtbox和链接控件表单,然后网格kidly请在隐藏字段后显示我的代码 – Developerzzz

+0

' asp:TemplateField AccessibleHeaderText =“id_session”HeaderText =“id_session”> 这是我的代码现在,我找到了另一种方式继续,但我想看看如何工作用你的解决方案! – Krishnak

0

而不是“GridView1.Columns(0)。可见=假”改变你这样的代码,

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