Cant pass value when BoundField visible = false

daarsk

New member
Joined
Jun 7, 2014
Messages
3
Programming Experience
5-10
Please humour me, why does the following not work unless I set the BoundField visible to true (line 3 HTML)?

HTML:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" rowcommand="Gridview1_RowCommand" >
   <Columns>
      <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" Visible="false" />
      <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
      <asp:buttonfield buttontype="Button" commandname="Select" headertext="Select" text="Select"/>        
   </Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:webdbConnectionString %>" SelectCommand="SELECT [Id], [Name] FROM [Manager]" >
</asp:SqlDataSource>
 
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource2" >
   <Columns>
      <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
      <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
   </Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:webdbConnectionString %>" >
   <SelectParameters>
      <asp:Parameter Name="Id" />
   </SelectParameters>
</asp:SqlDataSource>

VB.NET:
Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

   If e.CommandName = "Select" Then

      Dim index As Integer = Convert.ToInt32(e.CommandArgument)
      Dim row As GridViewRow = GridView1.Rows(index)
      Dim item As New ListItem()

      SqlDataSource2.SelectParameters("Id").DefaultValue = Server.HtmlDecode(row.Cells(0).Text)
      SqlDataSource2.SelectCommand = "SELECT [Id], [Name] FROM [Manager] WHERE (Id = @Id)"

   End If

End Sub
 
To anyone trying to fix the same problem

change boundfield to
HTML:
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id">
   <HeaderStyle CssClass="HiddenColumn" />
   <ItemStyle CssClass="HiddenColumn" />
</asp:BoundField>

add to css
VB.NET:
.HiddenColumn
{
   display:none;
}

works a charm :)
 
Back
Top