Page view counter

gridview column w/visible=false no longer contains cell data in beta 2

Last post 10-15-2008 10:01 AM by vasireddybharath. 59 replies.

Sort Posts:

  • gridview column w/visible=false no longer contains cell data in beta 2

    04-18-2005, 6:38 PM
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 10:42 AM
    • San Jose, California
    • Posts 3,539
    • Points 23,712
    • Moderator
      TrustedFriends-MVPs

    If you make a simple gridview with 2 columns and make both columns visible, you cn use the code below to get the value.  When you set visible="false" in the .aspx file, the value is no longer populated and str0 returns an empty string.

    So, this did retrieve the data in beta 1 but does not now.  How can I force it to retrieve the data and now show the value.  I need the data for foreign key relations I am managing myself.

     

    thanks, -Peter

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    GridView gv = (GridView)sender;

    int index = gv.SelectedIndex;

    GridViewRow gvw = gv.Rows[index];

    string str0 = (string)gvw.Cells[0].Text;

    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    04-20-2005, 5:20 PM
    • Loading...
    • phuff
    • Joined on 06-11-2002, 12:39 PM
    • Redmond, WA
    • Posts 547
    • Points 2,680

    For security reasons, we no longer databind fields that aren't visible, as you've noticed.  This was because often users thought making a field invisible prevented its contents from getting to the client, when in fact the values were present in ViewState.

    If you need a value of a particular field but don't want to display it, add it to your DataKeyNames property and you can access the value through the DataKeys property.  This has the added benefit of saving off the real value of the field rather than the ToString() representation of the value.

    Polita Paulus

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    04-20-2005, 6:00 PM
    • Loading...
    • vby
    • Joined on 08-19-2004, 3:17 PM
    • Alexandria, VA
    • Posts 23
    • Points 115

    I understand your reasons to not databind hidden fields and use DataKeys array instead. But it looks like there is a problem with this approach. DataKeys array doesn't get populated if I use paging and I process postback not from the first page. The scenario looks like this... I load page with a GridView. If I click some row (CommandButton or LinkButton) right away, then DataKeys array gets populated and everything is fine, but if I click on page link first and after page gets reloaded try to click on some row - here we go, DataKeys.Count is 0! Is this a bug?

  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    04-20-2005, 6:24 PM
    • Loading...
    • phuff
    • Joined on 06-11-2002, 12:39 PM
    • Redmond, WA
    • Posts 547
    • Points 2,680
    Are you setting the DataSource property rather than the DataSourceID property?  If so, then yes, it's a bug that's been fixed, but it didn't make it into Beta 2.  Subsequent builds will have it.  My apologies.
    Polita Paulus

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    04-20-2005, 6:36 PM
    • Loading...
    • vby
    • Joined on 08-19-2004, 3:17 PM
    • Alexandria, VA
    • Posts 23
    • Points 115

    Right, it doesn't work only if DataSource property assgined.

    My workaround for this bug is to use custom commands and bind value of id to CommandArgument property.

    I described my solution here: http://forums.asp.net/900598/ShowPost.aspx#900598

     

  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    04-20-2005, 7:02 PM
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 10:42 AM
    • San Jose, California
    • Posts 3,539
    • Points 23,712
    • Moderator
      TrustedFriends-MVPs
    I'm hoping I can set visible="true" and make the column width 0.  I don't need to hide foreign key type values form the viewstate variable
    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    04-20-2005, 8:01 PM
    • Loading...
    • phuff
    • Joined on 06-11-2002, 12:39 PM
    • Redmond, WA
    • Posts 547
    • Points 2,680

    Setting table cell width to less than the space it takes to render out its content will not work in most browsers- it will just auto-resize to the smallest size it needs to render your content.

    As long as you're using the DataSourceID property instead of the DataSource property, setting DataKeyNames should work fine for you.  DataKeyNames was set up exactly to make this scenario easy.

    Polita Paulus

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    05-05-2005, 12:12 AM
    • Loading...
    • dagnar
    • Joined on 09-26-2003, 9:03 PM
    • Posts 82
    • Points 385

    DataKeyNames works fine for a Select event but I don't think that will work for an Edit event(or does it?).  Is there a solution for Edits?...I guess the same question goes for Deletes as well

     

    thanks in advance.

  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    05-05-2005, 1:42 AM
    • Loading...
    • phuff
    • Joined on 06-11-2002, 12:39 PM
    • Redmond, WA
    • Posts 547
    • Points 2,680

    DataKeyNames should work all the time, even for edits and deletes.  DataKeys should be populated in the RowUpdating and RowDeleting events.  It will also be available in the Keys property in the EventArgs of the two events above.  If you don't want those fields in the Keys dictionary passed to the data source, you can remove them from the Keys dictionary in a handler for those events.

    Polita Paulus

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    05-05-2005, 4:51 PM
    • Loading...
    • dagnar
    • Joined on 09-26-2003, 9:03 PM
    • Posts 82
    • Points 385

    My question was bad.  What I meant to ask is that in the case of Select events you can use the GridView.SelectedValue to retrieve the selected key value.  What would be the equivalent for Edit and Deletes?  Also I needed to handle edits in the RowEditing event, RowUpdating is a little too late for my purpose.

    Thanks again.

  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    05-05-2005, 5:00 PM
    • Loading...
    • phuff
    • Joined on 06-11-2002, 12:39 PM
    • Redmond, WA
    • Posts 547
    • Points 2,680
    You can use gridView.DataKeys[gridView.EditIndex] to get the data keys for the current edit item.  You can use a similar indexing for the RowDeleting event, or you can use the Keys property on the event args.
    Polita Paulus

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    05-05-2005, 5:27 PM
    • Loading...
    • dagnar
    • Joined on 09-26-2003, 9:03 PM
    • Posts 82
    • Points 385

    It worked out except instead of using the gridview.EditIndex inside of the RowEditing event i used the GridViewEditEventArgs.NewEditIndex(the EditIndex was evaluating to -1).

    Thanks again for the prompt responses.

  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    05-26-2005, 11:46 AM
    • Loading...
    • jrich523
    • Joined on 05-17-2005, 8:15 PM
    • Boston MA
    • Posts 78
    • Points 247
    The problem here is i want to send that data directly to my datasource for a deleteCommand

    typically i can take a bound command and just use the @name in my SQL query.

    it doesnt appear as though i can do that with the datakeynames.

    why didnt you leave it the way it was in 1.1? i dont think you should dumby down things because people dont read how things work. it should be fairly obvious.

    Justin
  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    05-26-2005, 12:31 PM
    • Loading...
    • pkellner
    • Joined on 11-12-2004, 10:42 AM
    • San Jose, California
    • Posts 3,539
    • Points 23,712
    • Moderator
      TrustedFriends-MVPs

    Not sure Iunderstand how to do this.  Here is what I try but does not work.  str0 returns the id_file, but str1 errors saying: Unable to cast object of type 'System.Int32' to type 'System.String'.  the column filedescr is a string in the database

    <asp:GridView ID="GridView1"  DataKeyNames="id_file,filedescr" DataSourceID="SqlDataSource1" AllowPaging="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    {

    GridView gv = (GridView)sender;

    string str0 = gv.DataKeyNames[0];

    int idFile = (int) gv.DataKeys[0].Value;

    string str1 = (string) gv.DataKeys[1].Value;

    }

    Peter Kellner
    http://73rdstreet.com and blogging at
    http://PeterKellner.net
    MVP, ASP.NET
  • Re: gridview column w/visible=false no longer contains cell data in beta 2

    05-26-2005, 12:34 PM
    • Loading...
    • Rafa
    • Joined on 04-22-2005, 3:52 PM
    • Ladera Ranch, CA
    • Posts 248
    • Points 1,232
    Another work around for this is to convert one of your columns to template and add a hidden label field! You can get it from frindcontrol
    http://incendy.spaces.live.com
Page 1 of 4 (60 items) 1 2 3 4 Next >