tags:

views:

239

answers:

4

Have a gridview control and i want to display checkboxes for each row. The checkboxes should only appear if Session["DisplayBox"] == true.

<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" EnableSortingAndPagingCallbacks="True"
    AllowPaging="True" DataSourceID="JObjectDataSource" PageSize="5" OnRowCommand="gridView_RowCommand"
    DataKeyNames="ID" Width="100%">
    <Columns>
        <asp:TemplateField HeaderText="Review">
            <ItemTemplate>
                <asp:CheckBox  ID="chkRejectFile" AutoPostBack="true" runat="server" OnCheckedChanged="chkRejectFile_CheckedChanged" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

I removed some of the columns and kept the one that i am asking the question about. How would I place a conditional code in the aspx page and check for the session value?

Also, if I page, do i have to explicitly handle keeping track of which row was checked and which wasn't?

A: 

You want to set the Visible property based on the value of the session variable. Add something like the following (untested) code to your control:

 Visible='<%# Convert.ToBoolean(HttpContext.Current.Session["DisplayBox"]) %>'

Note that this doesn't check if the session variable is actually defined, i.e., it depends on it being set and set to either true or false.

If you do paging and care about retaining checked status, you'll need to keep track of the checked status for each item explicitly. The only thing that will get posted back on submit are the controls that are actually on the page at the time of the submit.

tvanfosson
tvanfosson: thanks. for paging at present i was keeping track and applying the appropriate check marks on the boxes.
+2  A: 

Add a callback handler for the OnDataBind event of the GridView. Then on each row, determine whether or not to show the checkbox.

The code will of course be in your .cs file.

GoodEnough
A: 

You could override the "OnRowDataBound" event in the code behind, and for each row, perform your logic and set the visisble property accordingly.

Jobo
+1  A: 

Add a new event property to the grid like this one:

OnRowDataBound="gridView_RowDataBound"

And then, in the code-behind, add the following corresponding event handler:

protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    // check for null first
    object displayBoxFlag = Session["DisplayBox"];
    if(displayBoxFlag!=null && (bool) displayBoxFlag)
    {
      e.Row.FindControl("chkRejectFile").Visible = true;
    }
    else
    {
      e.Row.FindControl("chkRejectFile").Visible = false;
    }
  }
}

Plenty of room to optimize, but it should work.

What happens is that ASP.NET will raise this event and call the method for each row of the grid right after they are bound. You now may go ahead and override or customize the way they appear.

Jorge Alves
Note: the answer given by tvanfosson will keep your view logic in the aspx file. He didn't test it, but if it works and you won't need extra processing, that's the best way to do it.
Jorge Alves
Jorge Alves: Thanks. I will try tvanfosson way and close this thread if all works.
Jorge Alves