Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Crashing caused by Dropdownlist in Header template

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
B. Chernick - 20 Dec 2007 20:11 GMT
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. Chernick - 20 Dec 2007 20:31 GMT
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.)

> I've been looking at references about this all afternoon but I can't really
> see anything that seems relevant to my particular situation.  
[quoted text clipped - 28 lines]
> Line 34:                 <HeaderStyle HorizontalAlign="Left" />
>  
Mohamad Elarabi [MCPD] - 21 Dec 2007 04:14 GMT
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.
----
Mohamad Elarabi
MCP, MCTS, MCPD.

> 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?)
[quoted text clipped - 43 lines]
> > Line 34:                 <HeaderStyle HorizontalAlign="Left" />
> >  
B. Chernick - 21 Dec 2007 14:16 GMT
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>

> I need more information to try and answer this. Please make sure to answer
> each question.
[quoted text clipped - 67 lines]
> > > Line 34:                 <HeaderStyle HorizontalAlign="Left" />
> > >  
Mohamad Elarabi [MCPD] - 21 Dec 2007 19:37 GMT
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.

Signature

Mohamad Elarabi
MCP, MCTS, MCPD.

> 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
[quoted text clipped - 234 lines]
> > > > Line 34:                 <HeaderStyle HorizontalAlign="Left" />
> > > >  

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.