Problem using two gridviews when using selected value of 1st to control datasource of 2nd

 I have a page with 2 gridviews. When you select a row in the first gridview, the DataKey of that row gets passed to the second gridview, which then displays a list of related records. I've read at http://www.asp.net/learn/data-access/tutorial-08-cs.aspx that there can be issues around page lifecycle timing that can affect what I'm doing so I've tried to follow the instructions there. However, I'm still having problems.

Here's the flow of what I want to happen:

a) User clicks row in gridview1.
b) Postback occurs.
c) gridview1 is selected, gridview2 shows records relating to gridview1.
d) User clicks a button in a templatefield in gridview2.
e) postback occurs.
f) button's code is called.

My problem is, after step e, gridview2 has disappeared, and gridview1 has no item selected. I believe this is because I have turned viewstate off on both gridviews. My reason for this is: if I try and turn viewstate on for gridview2, it doesn't show any records after step b. If I try and turn viewstate on for gridview1, the same thing happens - no records shown in gridview2 after step b.

here is my aspx:

  

                    <asp:GridView ID="gvwAnalyses" runat="server" DataSourceID="odsAnalyses" 
                        AutoGenerateColumns="False" AllowPaging="True" 
                        PageSize="8" DataKeyNames="ID" EnableViewState="false" CssClass="reportstable" >
                        <Columns>
                            <asp:BoundField DataField="FriendlyName" HeaderText="Title" />
                            <asp:BoundField DataField="SubmitDateTime" HeaderText="Date/Time Submitted" />
                            
                            
                            <asp:CommandField HeaderText="Show Drafts" SelectText="Show" 
                                ShowSelectButton="True" />
                            
                        </Columns>
                        <HeaderStyle CssClass="yellow" />
                        <SelectedRowStyle CssClass="SelectedRowStyle" />
                    </asp:GridView>
                    <asp:Label runat="server" ID="lblGvwSlctdValue" />
                    
                    <asp:ObjectDataSource ID="odsAnalyses" runat="server" 
                        DeleteMethod="Delete" InsertMethod="Insert" 
                        OldValuesParameterFormatString="original_{0}" 
                        SelectMethod="GetAnalysesByUserID" 
                        TypeName="AnswersTableAdapters.AnalysisTableAdapter" UpdateMethod="Update" 
                        onselecting="odsAnalyses_Selecting">
                        <DeleteParameters>
                            <asp:Parameter Name="Original_ID" Type="Int32" />
                        </DeleteParameters>
                        <UpdateParameters>
                            <asp:Parameter DbType="Guid" Name="UserID" />
                            <asp:Parameter Name="SubmitDateTime" Type="DateTime" />
                            <asp:Parameter Name="Original_ID" Type="Int32" />
                        </UpdateParameters>
                        <SelectParameters>
                            <asp:Parameter Name="UserID" Type="Object" />
                        </SelectParameters>
                        <InsertParameters>
                            <asp:Parameter DbType="Guid" Name="UserID" />
                            <asp:Parameter Name="SubmitDateTime" Type="DateTime" />
                        </InsertParameters>
                    </asp:ObjectDataSource>
                    
                    <asp:Label ID="lblExportResult" runat="server"></asp:Label>
                    
                    
                        
                        <asp:GridView runat="server" ID="gvwDrafts" DataSourceID="odsDrafts" 
                            AutoGenerateColumns="False" CssClass="reportstable" DataKeyNames="ID" 
                            EnableViewState="False" OnRowCommand="gvwDrafts_RowCommand" >
                            <Columns>
                                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" 
                                    SortExpression="ID" />
                                <asp:BoundField DataField="AnalysisID" HeaderText="AnalysisID" 
                                    SortExpression="AnalysisID" />
                                <asp:BoundField DataField="SubmitDateTime" HeaderText="SubmitDateTime" 
                                    SortExpression="SubmitDateTime" />
                                <asp:BoundField DataField="RevisionNumber" HeaderText="RevisionNumber" 
                                    SortExpression="RevisionNumber" />
                                
                                <asp:TemplateField HeaderText="Export this draft as PDF">
                                    <ItemTemplate>
                                        <asp:Button ID="Export" runat="server" Text="Export" CommandName="Export" 
                                            CommandArgument='<%# Eval("ID", "{0}") %>' OnCommand="Export_Click" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:TemplateField HeaderText="Edit this draft">
                                    <ItemTemplate>
                                        <asp:Button ID="Edit" runat="server" Text="Edit" CommandName="Edit"
                                            CommandArgument='<%# Eval("ID", "{0}") %>' OnCommand="Edit_Click" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                
                                <asp:ButtonField HeaderText="Test Button" CommandName="Export" ButtonType="Button" />
                                
                            </Columns>
                        </asp:GridView>
                        
                        <asp:ObjectDataSource runat="server" ID="odsDrafts" 
                            OldValuesParameterFormatString="original_{0}" 
                            SelectMethod="GetDraftsByAnalysisID" 
                            TypeName="AnswersTableAdapters.AnalysisDraftTableAdapter" 
                            DeleteMethod="Delete" InsertMethod="Insert" UpdateMethod="Update" >
                            <DeleteParameters>
                                <asp:Parameter Name="Original_ID" Type="Int32" />
                            </DeleteParameters>
                            <UpdateParameters>
                                <asp:Parameter Name="AnalysisID" Type="Int32" />
                                <asp:Parameter Name="SubmitDateTime" Type="DateTime" />
                                <asp:Parameter Name="RevisionNumber" Type="Int32" />
                                <asp:Parameter Name="Original_ID" Type="Int32" />
                            </UpdateParameters>
                            <SelectParameters>
                                <asp:ControlParameter ControlID="gvwAnalyses" ConvertEmptyStringToNull="True" 
                                    Name="AnalysisID" Type="Int32" />
                            </SelectParameters>
                            <InsertParameters>
                                <asp:Parameter Name="ID" Type="Int32" />
                                <asp:Parameter Name="AnalysisID" Type="Int32" />
                                <asp:Parameter Name="SubmitDateTime" Type="DateTime" />
                                <asp:Parameter Name="RevisionNumber" Type="Int32" />
                            </InsertParameters>
                        </asp:ObjectDataSource>
 

Also for your information, the way my first datasource (odsAnalyses) is getting it's select parameter is through this method in the code behind: 

    protected void odsAnalyses_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.InputParameters["UserID"] = Membership.GetUser().ProviderUserKey;
    }
 

Disco Patrick writes web applications using:

Visual Studio 2008
ASP.NET 3.5
C#
SQL Server 2008
0
Disco
3/18/2009 9:49:53 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

3 Replies
1033 Views

Similar Articles

[PageSpeed] 14

 Read this

http://csharpdotnetfreak.blogspot.com/2008/11/pouplating-multiple-detailsview-based.html

 

Hope this helps


Don't say thanks rather mark my reply as "Answer" if it helps you ,Doing so u'll get points too
amiT jaiN

C#.NET Articles
0
amit
3/18/2009 12:07:25 PM

 Hi Amit,

Thanks for the link, but I'm afraid it doesn't solve my problem.

My specific problem is that I have 2 gridviews, where gridview2's data depends on the selected row in gridview1.

I suspect the problem lies around the viewstate of both gridviews, which I currently have to set to false in order for both gridviews to show up. This has the knock-on effect that gridview1's selected value is not persisted between postbacks.

Can anyone help?


Disco Patrick writes web applications using:

Visual Studio 2008
ASP.NET 3.5
C#
SQL Server 2008
0
Disco
3/19/2009 11:45:18 AM

Hi Disco Patrick,

I have tested your code. ViewState cannot be turned off, so please turn on the both ViewState. I move the code in Selecting event to Init event of GridView "gvwAnalyses", it works well.

protected void gvwAnalyses_Init(object sender, EventArgs e)
    {
        odsAnalyses.SelectParameters["UserID"].DefaultValue = Membership.GetUser().ProviderUserKey.ToString();
    }

Thanks,


Qin Dian Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Qin
3/23/2009 7:05:50 AM
Reply:

Similar Artilces:

Problem in Using DropDownList Control to select a selected row of SqlDataSource that is under GridView Control
Hi all,In my SQL Server Management Studio Express, I have a database "ChemDatabase" that has a table "LabData"  formatted in the following way: SampleID int PRIMARY KEY, SampleName nvarchar(25), ChemicalName, nvarchar(25), Concentration decimal(6.2) with 5 rows of records in this .dbo table. In my Visual Web Developer Express, I created a website "RPD-TCE" for extracting a row of record from the LabData table by using a DropDownList, a GridView and a SqlDataSource.  I want to click DropDownList to list one number of S...

Trying to use the FAQ " Sorting and paging in the GridView control when not using data source controls "Sorting and paging in the GridView control when not using..."
and I am not getting the results I had hoped for.  I keep getting a "Compiler Error Message: CS0123: No overload for 'AdhocJobGridView_PageIndexChanged' matches delegate 'System.EventHandler'".   Here is my event handling... protected void AdhocJobGridView_PageIndexChanged(object sender, GridViewPageEventArgs e){ try { AdhocJobGridView.DataSource = wsBaseInfoArray; AdhocJobGridView.PageIndex = e.NewPageIndex; AdhocJobGridView.DataBind(); }catch (Exception ex) {throw ex; }   Here is my aspx... <asp:GridView ID="AdhocJobGridView...

Issue with getting values from child controls in a gridview, to use for the update using a SQLDataSource control
Hi all, I have a gridview bound with a SQLDataSource. I am using the Update feature of the SQLDataSource to update a SQL Server database with values entered into the gridview. However I am not getting it to work. I believe this is due to the controls that contain the user entries are not the gridview itself, but rather child controls within the gridview. I have been using the names of the actual controls but nothing happens. Upon submit, the screen returns blank, and the database is not updated. Here is some code:   <asp:GridView ID="GridEditSettlement" runat="server" AutoGenerate...

What control do I use (or do I not use a control at all...)
Okay so here's the story so far. I am making a blog to be incorporated into a website. Well, I'm doing the blog from scratch. I'm using MySQL to source all the data via MySQL .NET/Connector. Now while developing the script, I used DataReader and Response.Write to show the blog topics. Now that it has come to put it under a design (I'm using a Master Page for that), I've hit the problem. Response.Write shows all the topics before any of the design elements could be incorporated. So that (more or less) is out of question. So next I looked into the data presentation controls. I looked up GridVi...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Problem Using Slider Control in Gridview Control
Hello to all,   it is urgent.   i want to use slider control in a Gridview Template Column, if someone can help me how i can get this functionality?...

How to change the SQL used for the GridView, when using a control parameter in the datasource
I'm using a GridVeiw with and Access database. I want to allow users to select a date and then update the GridView to show only data entered after that date. So my question is, how do I modify the SQL used by the datagrid? I'm using a control parameter as part of my AccessDataSource and I think that is what I'm having trouble figuring out how to include. Here is my datasource, with the control parameter in the the GridView. The GridView works fine with these initial settings settings. <asp:AccessDataSource ID="AccessDataSourceForwView" runat="server" ConflictDetection="Compa...

using controlS to control a gridview
This question is probably posted but i did not bother to look at 10,000+ posts so i am sorry. I've seen the tutorial where he builds a dataset and uses a gridview and controls the gridview with the drop down list box. What i am trying to do is to control the gridview with 2 drop down list boxes. First select the Country you life in and then the State. I've looked at the grid view properties but couldn't find where i can add another parameter. Is it possible to control a grid view (or any form of displaying the information) with more than 1 controller? Thank You.  ...

(master/detail) control using gridview and formview controls problem...
hi guys, i'm new to .net programming and have been toying with the (master/detail) control using gridview and formview controls...however, i can't seem to drilldown to the correct record when the primary key is a composite key. please help....   <asp:GridView ID="GridView1" runat="server" DataKeyNames="ServiceID,VersionNum" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"               &nbs...

Datagrid(.net 1.1) to GridView(.net 2.0) and using the RowUpdating event how to extract the values from the controls.
Ok  This is the settings: I use a Viewgrid with only itemtemplate colums for example. <asp:TemplateField HeaderText="Test"><ItemTemplate><asp:Label id="LBL_Test" text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' runat="server" meta:resourcekey="LBL_TestResource1"></asp:Label></font></ItemTemplate> <EditItemTemplate><asp:TextBox ID="HDN_Test" Text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' Runat="server" meta:resourcekey="HDN_TestResource1" /></EditItemTemplate></asp:TemplateField>  The ...

code for Custom paging without using sqldatasource control and sql datasource control for gridview...........
 code for Custom paging without using sqldatasource control and sql datasource control for gridview...........i need code for how to show all pagenumbers in gridview with custom paging in gridview..............It is very urgent................................. T.Ashok Kumar  suppose that we have a table like thisCountryId  identity intCountryTitle    nvarchar(50)you can use this sql command for paging the dataUSE [CyberOffice]GO/****** Object:  StoredProcedure [dbo].[CountryGetListPaged]    Script Date: 09/17/2008 14:01:06 ******/SET A...

Problem: Parent Control cannot pick up child control values in pages that use new 2.0 features
I have been experiencing the loss of data either with my own custom controls or using third party controls such as FreeTextBox...regardless if I reference the controls by a controlID, manage viewstate etc.However, the issues with not being to retrieve data from child controls typically only occur when a chld control is called from another child control and the parent control uses Treeview, MultiView, or WizardSteps....For example: I have an page that has a treeview menu control on the left (using a master page) that selects the page to load.  The page in question is a security selection...

Custom Datasource Controls Vs. Object Datasource Controls
Hi folks,I am a web developer developing pages in the project. I need to integrate the front end pages with the backend API. Active directory is being used as the database system for our project. I only use the API and the methods offered by it to interact with the data.For all the operations on the data, I have methods that I can call from the API (viz. Create(), Delete(), Modify() etc.). For connecting the data to my frontend data controls (viz. GridView, DropDownList etc.) I can use Object Datasource Control. I also read some articles about Custom Datasource controls. Now, I am having a d...

Using Data Controls Without Using ObjectDataSource Control
Hi, I've been looking on the Internet for a tutorial that would show me how to use the data conrols (FormView, GridView etc) without using an ObjectDataSource (or any other data source control).  The reason is that I want to use a formview and gridview from within another formview control to create a single form  that would allow me to insert, update and delete an order and its order details at the same time. I thought that this could be achieved by having a DataSet containing two DataTable objects in the code-behind with the Order DataTable containing one row to repres...

Web resources about - Problem using two gridviews when using selected value of 1st to control datasource of 2nd - asp.net.presentation-controls

Datasource - Wikipedia, the free encyclopedia
A DataSource object has properties that can be modified when necessary. For example, if the data source is moved to a different server, the property ...

Inverness Graham Acquires DataSource
Inverness Graham , a lower middle market private equity firm headquartered in suburban Philadelphia, has acquired DataSource , a print supply ...

SmartGlance for iPad for iPad on the iTunes App Store
Read reviews, get customer ratings, see screenshots, and learn more about SmartGlance for iPad on the App Store. Download SmartGlance for iPad ...

Chaitanya Pandit (@chaitanyapandit) on Twitter
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Data source - Wikipedia, the free encyclopedia
... Data source A data source is any of the following types of sources for (mostly) digitized data: a database in the Java software platform, datasource ...

2ndQuadrant - PostgreSQL expertise from specialists with a source code level understanding of RDBMS ...
PostgreSQL expertise from specialists with a source code level understanding of RDBMS PostgreSQL Planets Gabriele’s PlanetPostgreSQL Gianni’s ...

Tagged entries for CLOUD COMPUTING
Alan Williamson's output as a Java Champion, Blog-City Architect, BlueDragon Creator, Author, Speaker and Internet Guru

Private equity deals
... of biometric identity management systems, applications and services. www.crossmatch.com Inverness Graham Investment has acquired DataSource ...

C# C Sharp and Tutorials on C# Friends.com
Learn the c# langauge to build web applications using our online tutorials with live demos. Participate in our forums and learn from others. ...

JavaScript UI Library, Ajax Components & HTML5 Framework - DHTMLX
DHTMLX offers a rich JavaScript library, UI components & HTML5 mobile framework. Build impressive web apps for both desktop and mobile devices. ...

Resources last updated: 12/7/2015 10:24:26 AM