Question

I'm having an awful time with my GridView. I've been going over my code with a fine-tooth comb and can't find the problem.

I have an ASP.net GridView with seven columns; the data comes from SQL. Two columns hold javascript that the user can click on to remove or modify an entry in the database. The other five fields contain names, phone numbers, and e-mail addresses of people in a database.

Four of my fields are ASP.net TemplateFields.

When the page loads, everything is displayed correctly. However, if a user clicks a JavaScript link, clicks to sort the GridView by any field, or does any other action that causes a postback, the resulting page may not display the contents of my TemplateFields; they are all blank.

Just so you can see what I'm working with, I have a "name" column which is defined like this:

<asp:TemplateField HeaderText="Name" SortExpression="name" ItemStyle-Width="150">
<ItemTemplate>
<%#(string)Eval("surname") + ", " + (string)Eval("fname")  %>
</ItemTemplate>
</asp:TemplateField>

If I step thru the code in the debugger, I see that in the case that the template fields display correctly, the debugger stops on the line that displays the name. In the case that the fields are all blank, it jumps over the code to display the contents of those fields; and for no discrenible reason what-so-ever.

The only thing that seems to cause the problem is that there is a single enum value that is set from the query-string (which tells the page what data to load). When the enum value is set to 1, I never see this error, but it seems that I always see it when the enum value is set to 2.

However, the enum is never ever referenced anywhere in my gridview code.

In any event, the GridView.DataSource = x is always called and GridView.DataBind() is always called. I've tried stepping thru the code and checking that the data from the database is correct. I've tried debugging on the code block where DataBind is called from and also the GridViewRow_DataBound Event handler.

I've seen other posts elesewhere about trouble with TemplateField, but none of them seem to address what's going on here.

Any ideas?

Was it helpful?

Solution

I figured it out part of the answer. I found some code that read like:

if (!userIsAllowedToSeePrivatePhoneNumbers)
    removePrivatePhoneNumber(gridView);
***
    protected void removePrivatePhoneNumber(GridView gridView)
    {
        DataControlField privatePhoneNumberColumn = null;
        foreach (DataControlField column in gridView.Columns)
        {
            if (column.HeaderText.ToUpper() == "PRIVATE PHONE")
                privatePhoneNumberColumn = column;
        }            

        gridView.Columns.Remove(privatePhoneNumberColumn);
    }

I discovered that if I set the visibility of the column to false instead of removing the column, all of my data displays correctly. I do not understand why this is, but it is a satisfactory solution to make the program work for now.

    protected void removePrivatePhoneNumber(GridView gridView)
    {
        DataControlField privatePhoneNumberColumn = null;
        foreach (DataControlField column in gridView.Columns)
        {
            if (column.HeaderText.ToUpper() == "PRIVATE PHONE")
                privatePhoneNumberColumn = column;
        }            

        privatePhoneNumber.Visible = false;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top