Solved

RowUpdating Column Visible?

Posted on 2011-03-01
12
694 Views
Last Modified: 2012-05-11
Q. Is it possible to make a column visible duing the OnRowUpdating event in code-behind?

1. I have a <asp:CommandField causing edit-mode to begin and would like make visible a hidden TextBox.
2. The TextBox text input during edit-mode is then used in SQL query.
0
Comment
Question by:pointeman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 35016585
Yes you can.

One approach would be to just put the TextBox inside an EditTemplate within a row.

Alternatively, in the RowUpdating event, first make the column visible and THEN rebind to your datasource.  ASP.NET won't render any column contents that are visible during the DataBind event, so make sure its visible before binding.
0
 

Author Comment

by:pointeman
ID: 35018861
Okay, I can HIDE the first column like so:

    protected void gridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
            gridView1.Columns[0].Visible = false;    
    }

However, I cannot SHOW the columns like so:

    protected void gridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
            gridView1.Columns[0].Visible = true;
    }
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 35018957
Try re-binding it to the database after setting visiblity.

    protected void gridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
            gridView1.Columns[0].Visible = true;
            gridView1.DataSource = whatever normally goes here...;
            gridView1.DataBind();
    }
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 25

Expert Comment

by:Rouchie
ID: 35018959
Sorry that should have said re-binding to the dataSOURCE
0
 

Author Comment

by:pointeman
ID: 35019424
Still not having any success.

I found these two links that use a stylesheet to hide then show a GridView column. It seems it's a matter of Rendering or not Rendering the column. Could be your code does the same thing but differently.  Have a look, it's interesting...

The first link states:

"If you do NOT want to RENDER the column at all, then you can set Visible property for that column to False."

"Now if you WANT to RENDER the column data but want to hide it from the user, then using Visible property will NOT work because setting that property will remove the column from the grid altogether."

http://www.netomatix.com/development/GridViewHideColumn.aspx

http://www.codeproject.com/Tips/80964/How-to-get-Hidden-Column-Value-in-GridView.aspx

0
 
LVL 25

Expert Comment

by:Rouchie
ID: 35019510
I would forget using stylesheets altogether because you are going to cause yourself all manner of problems.  Style sheets will only hide the rendered code, not the actual objects, so anyone can 'override' your rules and corrupt your page.

If you want to show a text box, put it in the appropriate ItemTemplate or EditItemTemplate area for that column.  Then it will get shown automatically when the GridView swtiches into view/edit mode.

I think what you are trying to do is hide a value that can then be used after the gridview row is edited (posted back) right?  If so, you are MUCH better off using the GridView.DataKeyNames functionality which can then be used with the selected row index to get the hidden data.
0
 

Author Comment

by:pointeman
ID: 35019780
Here's the GridView layout I should have stated to begin with.

[asp:BoundField]  [asp:TemplateField (TextBox/Label)]  [asp:CommandField]                                                            

If I could simply set the asp:TemplateField width to 0 THEN change the width to 100 after clicking the CommandField (edit-mode) Button.

This is a purely cosmetic purpose and some might find trivial, but I think it's a cool function if I can get it to work...
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 35019868
What exactly are you trying to show/hide?  Why is there a requirement to have it in the page even though its not visible?
0
 

Accepted Solution

by:
pointeman earned 0 total points
ID: 35032847
Okay, figured it out!

* Hide columns during normal activity
protected void gvLicPool_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    gridView1.Columns[8].HeaderStyle.Width = 1;
    gridView1.Columns[8].ItemStyle.Width = 1;
}

* Show columns during Edit-Mode
protected void gridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    gridView1.Columns[8].HeaderStyle.Width = 100;
    gridView1.Columns[8].ItemStyle.Width = 100;
}    
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 35034434
Width = 1 isn't actually hiding a column, its just setting its width to 1 pixel.  You never did explain what the end result is that you're aiming for...!
0
 

Author Comment

by:pointeman
ID: 35036354
>>Q. Is it possible to make a column visible duing the OnRowUpdating event in code-behind?

You may have taken the word visible by exact definition. I just don't want the column seen until RowUpdating occurs. Sorry if I confused anyone :)
0
 

Author Closing Comment

by:pointeman
ID: 35145545
This solution meets my needs by not displaying (hiding) the column and using Row events instead of reloading the page.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question