Crashing caused by Dropdownlist in Header template

B

B. Chernick

I've been looking at references about this all afternoon but I can't really
see anything that seems relevant to my particular situation.

I have a gridview. This is used in read-only mode only, strictly for
display and deletes. Paging is on. I turned a column into a template and
put a dropdown list in the header template. The values of this dropdown are
read from a list of fixed values stored in a table other than the one bound
to the grid. The setting of the dropdown controls the FilterExpression of
the grids ObjectDataSource. If the FilterExpression is blank there is no
problem at all. Otherwise the filtering action of the ddl works fine until I
try to page the gridview. If I try to page the gridview with a filter
setting the page crashes with the message:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in
the context of a databound control.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Databinding methods
such as Eval(), XPath(), and Bind() can only be used in the context of a
databound control.

Source Error:

Line 30: </HeaderTemplate>
Line 31: <ItemTemplate>
Line 32: <asp:Label ID="Label1" runat="server" Text='<%#
Eval("UnitName") %>'></asp:Label>
Line 33: </ItemTemplate>
Line 34: <HeaderStyle HorizontalAlign="Left" />
 
B

B. Chernick

Once again, I may have appended too soon. Once again, I have to ask: This
works. Why? (Or at least is this the best solution?)

I put the following code in the GridView's PageIndexChanged and RowDeleted
event handlers:

ObjectDataSource1.FilterExpression = "RK_Unit=" + Session("UnitFilter")
If ObjectDataSource1.FilterExpression <> "" Then
GridView1.DataBind()
End If

(Not sure why I have to restore the ODS's FilterExpression. Wouldn't the
ODS's FilterExpression be retained in the view state? And I don't think it's
being accidentally cleared anywhere.)
 
M

Mohamad Elarabi [MCPD]

I need more information to try and answer this. Please make sure to answer
each question.

1- I'm confused by your subject and your preface. What made you believe that
this is caused by the DropDown? The error seems to be not related to the drop
down.

2- Is ObjectDataSource1 the DS for the drop down or the grid view? Seems to
be the DS for the GridView.

3- Is it possible to paste over your complete
<asp:GridView>...</asp:GridView> tag?

Thanks,

P.S. The if statement that checks if FilterExpression <> "" will always
return true since you're hardcoding "RK_Unit=" into the FilterExpression in
the previous line. To avoid an error in your bind you might need to check If
Session("UnitFilter") <> "" instead and if it is true then set the
FilterExpression and Bind.
 
B

B. Chernick

1. That's just it. This is the startup page, a very vanilla grid with
almost no code until I started trying to install a dropdown. Just bind the
gridview to a data source and display. It's always in ReadOnly mode. On the
surface I can't see any way the ddl could cause this problem. (Could the
problem be caused by having the ddl's ODS included within the header
template?)

2. Yes, ObjectDataSource1 is the ods for the grid.

Actually I did modify the code slightly. The event handlers now all do it
this way:

If Session("UnitFilter") <> "" Then
ObjectDataSource1.FilterExpression = "RK_Unit=" +
Session("UnitFilter")
GridView1.DataBind()
End If

Here's the complete html of the page:

<%@ Page Language="vb" MasterPageFile="~/Site1.Master"
AutoEventWireup="false" Codebehind="Default.aspx.vb"
Inherits="GM_Project_Application_2._Default" Theme="default" %>

<asp:Content ID="Content1" runat="server"
ContentPlaceHolderID="ContentPlaceHolder1">
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="True" Text="View
All" /><br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="PK" DataSourceID="ObjectDataSource1"
ForeColor="#333333"
GridLines="None" Width="100%" PageSize="15" AllowSorting="True">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="PK" HeaderText="PK"
InsertVisible="False" ReadOnly="True"
SortExpression="PK" Visible="False" />
<asp:TemplateField HeaderText="Unit" SortExpression="RK_Unit">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("UnitName") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderTemplate>
<asp:Label ID="Label2" runat="server"
Text="Unit"></asp:Label><br />
<asp:DropDownList ID="ddlUnit" runat="server"
DataSourceID="ObjectDataSourceUnit"
DataTextField="Item" DataValueField="PK"
AutoPostBack="True" OnPreRender="SetValue"
OnDataBound="ddlUnit_DataBound"
OnSelectedIndexChanged="ddlUnit_SelectedIndexChanged">
</asp:DropDownList><asp:ObjectDataSource
ID="ObjectDataSourceUnit" runat="server"
OldValuesParameterFormatString="{0}"
SelectMethod="GetData"
TypeName="GM_Project_Application_2.GMPATableAdapters.ztblPicklistTableAdapter">
<SelectParameters>
<asp:parameter DefaultValue="ABACUS"
Name="Group" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblUnitName" runat="server" Text='<%#
Eval("UnitName") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:BoundField DataField="PGName" HeaderText="Product Group"
SortExpression="RK_PG">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="ID" HeaderText="Local Project ID"
SortExpression="ID">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="AlternateProjectId" HeaderText="Alt.
Proj ID" SortExpression="AlternateProjectId">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Project Name"
SortExpression="Name">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="ClassName"
HeaderText="Classification" SortExpression="RK_Class">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="G0" DataFormatString="{0:MM/dd/yyyy}"
HeaderText="G0 Date"
SortExpression="G0" HtmlEncode="False">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:HyperLinkField DataNavigateUrlFields="PK"
DataNavigateUrlFormatString="ListItems.aspx?ProjectId={0}"
HeaderText="Project Details" Text="View Details">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle VerticalAlign="Top" />
</asp:HyperLinkField>
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DeleteMethod="Delete"
InsertMethod="Insert" OldValuesParameterFormatString="{0}"
SelectMethod="GetData"

TypeName="GM_Project_Application_2.GMPATableAdapters.tblProjectTableAdapter"
UpdateMethod="Update">
<DeleteParameters>
<asp:parameter Name="PK" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="RK_Unit" Type="Int32" />
<asp:parameter Name="RK_PG" Type="Int32" />
<asp:parameter Name="ID" Type="String" />
<asp:parameter Name="Name" Type="String" />
<asp:parameter Name="Desc" Type="String" />
<asp:parameter Name="RK_Class" Type="Int32" />
<asp:parameter Name="G0" Type="DateTime" />
<asp:parameter Name="Notes" Type="String" />
<asp:parameter Name="PK" Type="Int32" />
<asp:parameter Name="IsNull_ts" Type="String" />
<asp:parameter Name="ts" Type="Object" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="RK_Unit" Type="Int32" />
<asp:parameter Name="RK_PG" Type="Int32" />
<asp:parameter Name="ID" Type="String" />
<asp:parameter Name="Name" Type="String" />
<asp:parameter Name="Desc" Type="String" />
<asp:parameter Name="RK_Class" Type="Int32" />
<asp:parameter Name="G0" Type="DateTime" />
<asp:parameter Name="Notes" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>
<table width="100%">
<tr width="100%">
<td align="left">
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="ListItems.aspx?ProjectId=-1">Add Project</asp:HyperLink>
</td>
<td align="right">
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl="~/DisplayEmployees.aspx"
Visible="False">View Employees</asp:HyperLink>
</td>
</tr>
</table>
</asp:Content>
 
M

Mohamad Elarabi [MCPD]

Ok, This seems correct. You have to re-bind the GridView to its datasource
after the page index changes. The paging functionality doesn't work out of
the box by just setting AllowPaging=True. So given that, your code that you
adjusted, you have to take the GridView1.DataBind() out of the if statement.
You must bind in all paths. So I think it should be like this

If Session("UnitFilter") <> "" Then
ObjectDataSource1.FilterExpression = "RK_Unit=" +
Session("UnitFilter")
Else
ObjectDataSource1.FilterExpression = "" 'To clear any previous
values
End If
GridView1.DataBind()

The rest of your code are fine. I'd just recommend taking the
ObjectDataSourceUnit out of the headertemplate and all the way outside the
GridView tag, but this is just for readability. I usually put all my
datasource tags in one area either the top or the bottom of the page.

Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,771
Messages
2,569,587
Members
45,097
Latest member
RayE496148
Top