• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1109
  • Last Modified:

Gridview doesnt update Formview correctly

heres the thing -

I have a gridview that has 'Enable Paging' checked. It also has 'Enable Selection' checked.

When the users select a record from the gridview. I update the selected index of a formview and then switch to that tab.

(See the code snippet attached)

However if I page on the gridview and then select a record. The formview updates as if I have made a selection from the first page.

Does that makes sense or do you need more info?
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        ' When a record is selected on the grid then update the formview to reflect the record change. Then switch to that tab
                FormView1.PageIndex = GridView1.SelectedIndex
                TabContainer1.ActiveTabIndex = 1
End Sub

Open in new window

0
Real_coffee
Asked:
Real_coffee
  • 5
  • 4
1 Solution
 
David RobitailleAnalyst ProgrammerCommented:
  1. set the datakey of your gridview and formview to your primary key
  2. SelectedValue instead of SelectedIndex and PageIndex
0
 
Real_coffeeAuthor Commented:
1) sorry both datakeynames not set to client_id is that what you mean?
2) have replaced selectedindex with selectedvalue

symptom has changed but still out of whack: formview now seems to display a random record? If it helps - I can see that if i select something from the first page then the formview selects the record +1 ie the next record. but if I select from another page then it just appears to be random.
0
 
David RobitailleAnalyst ProgrammerCommented:
it<s because you are trying to set directly the page index, you need to set the selected data key (client_id) of the formview. For that you need to find the index of the datakey in the formview.
But the easiest way of doing the selection is by setting a parameter in the datasource instead of working with indexes.
http://www.asp.net/learn/data-access/tutorial-10-vb.aspx 
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
prairiedogCommented:
When you have GridView's Paging enabled, then GridView's SelectedIndex always be from 0 to (PageSize - 1) on each paging.
For example, if your GridView has 25 records totally, and you set PageSize = 5, then your GridView's PageIndex will like:
Page 1       Page 2      Page 3      Page 4     Page 5
0-4              0-4             0-4             0-4            0-4
0
 
Real_coffeeAuthor Commented:
Riiiight...

prairiedog I get why it doesn't work now. Thanks.
davrob60 yes i need to avoid the indexes and parameters are the way to go. Thanks too

When I put a test page together with both controls on the page it works fine.

But and its a big but..

I still get problems as soon as I try to use a tabbed container - (the idea is gridview on tab page1, fromview on tab page 2) and this is why I originally started messing with the selected index etc.

The error I get is -


[InvalidOperationException: Could not find control 'GridView1' in ControlParameter 'Client_id'.]
   System.Web.UI.WebControls.ControlParameter.Evaluate(HttpContext context, Control control) +1753150
   System.Web.UI.WebControls.Parameter.UpdateValue(HttpContext context, Control control) +47
   System.Web.UI.WebControls.ParameterCollection.UpdateValues(HttpContext context, Control control) +114
   System.Web.UI.WebControls.SqlDataSource.LoadCompleteEventHandler(Object sender, EventArgs e) +43
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.Page.OnLoadComplete(EventArgs e) +8698566
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +735

Maybe Im not referencing the gridview1 control properly?

Ive attached my sorry example webpage. When you take the controls off the tabbed container it works ok.




<%@ Page Language="VB"%>
 
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <cc1:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="1" 
        Height="510px" Width="852px">
        <cc1:TabPanel runat="server" HeaderText="Tab 1" ID="TabPanel1">
            <ContentTemplate>
                
&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                    AutoGenerateColumns="False" DataKeyNames="Client_id" 
                    DataSourceID="SqlDataSource1">
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="Client_id" HeaderText="Client_id" 
                    InsertVisible="False" ReadOnly="True" SortExpression="Client_id" />
                <asp:BoundField DataField="ratlistaddr" HeaderText="ratlistaddr" 
                    SortExpression="ratlistaddr" />
                <asp:BoundField DataField="head_office" HeaderText="head_office" 
                    SortExpression="head_office" />
                <asp:BoundField DataField="ho_addr1" HeaderText="ho_addr1" 
                    SortExpression="ho_addr1" />
                <asp:BoundField DataField="ho_addr2" HeaderText="ho_addr2" 
                    SortExpression="ho_addr2" />
                <asp:BoundField DataField="company_name" HeaderText="company_name" 
                    SortExpression="company_name" />
                    </Columns>
                    
                </asp:GridView>
                
&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; 
            </ContentTemplate>
            
        </cc1:TabPanel>
        <cc1:TabPanel ID="TabPanel2" runat="server" HeaderText="Tab 2">
            <ContentTemplate>
                
                <br />
                
                <br />
                
                <br />
                
                <br />
                
                <asp:FormView ID="FormView1" runat="server" DataKeyNames="Client_id" 
                    DataSourceID="SqlDataSource2">
    <EditItemTemplate>
        Client_id:
        <asp:Label ID="Client_idLabel1" runat="server" 
            Text='<%# Eval("Client_id") %>' />
        <br />
        ratlistaddr:
        <asp:TextBox ID="ratlistaddrTextBox" runat="server" 
            Text='<%# Bind("ratlistaddr") %>' />
        <br />
        head_office:
        <asp:TextBox ID="head_officeTextBox" runat="server" 
            Text='<%# Bind("head_office") %>' />
        <br />
        ho_addr2:
        <asp:TextBox ID="ho_addr2TextBox" runat="server" 
            Text='<%# Bind("ho_addr2") %>' />
        <br />
        ho_addr1:
        <asp:TextBox ID="ho_addr1TextBox" runat="server" 
            Text='<%# Bind("ho_addr1") %>' />
        <br />
        ho_addr3:
        <asp:TextBox ID="ho_addr3TextBox" runat="server" 
            Text='<%# Bind("ho_addr3") %>' />
        <br />
        ho_addr4:
        <asp:TextBox ID="ho_addr4TextBox" runat="server" 
            Text='<%# Bind("ho_addr4") %>' />
        <br />
        ho_postcode:
        <asp:TextBox ID="ho_postcodeTextBox" runat="server" 
            Text='<%# Bind("ho_postcode") %>' />
        <br />
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
            CommandName="Update" Text="Update" />
        &#160;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                    </EditItemTemplate>
                    
    <InsertItemTemplate>
        ratlistaddr:
        <asp:TextBox ID="ratlistaddrTextBox0" runat="server" 
            Text='<%# Bind("ratlistaddr") %>' />
        <br />
        head_office:
        <asp:TextBox ID="head_officeTextBox0" runat="server" 
            Text='<%# Bind("head_office") %>' />
        <br />
        ho_addr2:
        <asp:TextBox ID="ho_addr2TextBox0" runat="server" 
            Text='<%# Bind("ho_addr2") %>' />
        <br />
        ho_addr1:
        <asp:TextBox ID="ho_addr1TextBox0" runat="server" 
            Text='<%# Bind("ho_addr1") %>' />
        <br />
        ho_addr3:
        <asp:TextBox ID="ho_addr3TextBox0" runat="server" 
            Text='<%# Bind("ho_addr3") %>' />
        <br />
        ho_addr4:
        <asp:TextBox ID="ho_addr4TextBox0" runat="server" 
            Text='<%# Bind("ho_addr4") %>' />
        <br />
        ho_postcode:
        <asp:TextBox ID="ho_postcodeTextBox0" runat="server" 
            Text='<%# Bind("ho_postcode") %>' />
        <br />
        <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
            CommandName="Insert" Text="Insert" />
        &#160;<asp:LinkButton ID="InsertCancelButton" runat="server" 
            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                    </InsertItemTemplate>
                    
    <ItemTemplate>
        Client_id:
        <asp:Label ID="Client_idLabel" runat="server" Text='<%# Eval("Client_id") %>' />
        <br />
        ratlistaddr:
        <asp:Label ID="ratlistaddrLabel" runat="server" 
            Text='<%# Bind("ratlistaddr") %>' />
        <br />
        head_office:
        <asp:Label ID="head_officeLabel" runat="server" 
            Text='<%# Bind("head_office") %>' />
        <br />
        ho_addr2:
        <asp:Label ID="ho_addr2Label" runat="server" Text='<%# Bind("ho_addr2") %>' />
        <br />
        ho_addr1:
        <asp:Label ID="ho_addr1Label" runat="server" Text='<%# Bind("ho_addr1") %>' />
        <br />
        ho_addr3:
        <asp:Label ID="ho_addr3Label" runat="server" Text='<%# Bind("ho_addr3") %>' />
        <br />
        ho_addr4:
        <asp:Label ID="ho_addr4Label" runat="server" Text='<%# Bind("ho_addr4") %>' />
        <br />
        ho_postcode:
        <asp:Label ID="ho_postcodeLabel" runat="server" 
            Text='<%# Bind("ho_postcode") %>' />
        <br />
                    </ItemTemplate>
                    
                </asp:FormView>
                
                <br />
                
                <br />
                
                <br />
                
            </ContentTemplate>
            
        </cc1:TabPanel>
    </cc1:TabContainer>
          <br />
        <br />
        <br />
          <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
        <br />
        <br />
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
            SelectCommand="SELECT [Client_id], [ratlistaddr], [head_office], [ho_addr2], [ho_addr1], [ho_addr3], [ho_addr4], [ho_postcode] FROM [Clients] WHERE ([Client_id] = @Client_id)">
            <SelectParameters>
                <asp:ControlParameter ControlID="GridView1" DefaultValue="1" Name="Client_id" 
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
        <br />
        <br />
    
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
        SelectCommand="SELECT [Client_id], [ratlistaddr], [head_office], [ho_addr1], [ho_addr2], [company_name] FROM [Clients]">
    </asp:SqlDataSource>
    <br />
    </form>
</body>
</html>

Open in new window

0
 
David RobitailleAnalyst ProgrammerCommented:
well, i dont think you can use  acontol parameter in that case.
One solution is to use a standard parameter and set i`s value in the SelectedIndexChanged of the GridView
0
 
Real_coffeeAuthor Commented:
Sorry mate - can you give me an example of this?

You see I dont understand why this would be any different to my first approach - wont the returned index parameter just be the same way as prairie dog described?
0
 
Real_coffeeAuthor Commented:
ok Im nearly there.

Please disregard the last post. I now fully understand why the selectedvalue approach is different.

can you talk me through adding the parameter to the formview? Im guessing this must be a session parameter. I have added this to the datasource of the formview. It is called myrecordposition.

Am I right to assume that I just have to declare myrecordposition and then populate it with the result of gridview1.selectedvalue ?

Please see my attached code and tell me what I have got worng.
<%@ Page Language="VB"%>
 
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    Protected Sub GridView1_SelectedIndexChanged2(ByVal sender As Object, ByVal e As System.EventArgs)
        ' I think this is my session parameter
        Dim myrecordposition As Integer
        myrecordposition = GridView1.SelectedValue
       
        'lets switch to the second tab to see if it has changed the formview
        TabContainer1.ActiveTabIndex = 1
    End Sub
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
           <br />
    <cc1:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" 
        Height="510px" Width="852px">
        <cc1:TabPanel runat="server" HeaderText="Tab 1" ID="TabPanel1">
            <ContentTemplate>
                
                &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
                <br />
                <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                    AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Client_id" 
                    DataSourceID="SqlDataSource1" 
                    onselectedindexchanged="GridView1_SelectedIndexChanged2">
                    <Columns>
                        <asp:CommandField ShowSelectButton="True" />
                        <asp:BoundField DataField="Client_id" HeaderText="Client_id" 
                            InsertVisible="False" ReadOnly="True" SortExpression="Client_id" />
                        <asp:BoundField DataField="ratlistaddr" HeaderText="ratlistaddr" 
                            SortExpression="ratlistaddr" />
                        <asp:BoundField DataField="head_office" HeaderText="head_office" 
                            SortExpression="head_office" />
                        <asp:BoundField DataField="ho_addr1" HeaderText="ho_addr1" 
                            SortExpression="ho_addr1" />
                        <asp:BoundField DataField="ho_addr2" HeaderText="ho_addr2" 
                            SortExpression="ho_addr2" />
                        <asp:BoundField DataField="company_name" HeaderText="company_name" 
                            SortExpression="company_name" />
                    </Columns>
                </asp:GridView>
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
            </ContentTemplate>
            
        </cc1:TabPanel>
        <cc1:TabPanel ID="TabPanel2" runat="server" HeaderText="Tab 2">
            <HeaderTemplate>
                Tab 2
            </HeaderTemplate>
            <ContentTemplate>
                
                <br />
                
                <br />
                
                <asp:FormView ID="FormView1" runat="server" DataKeyNames="Client_id" 
                    DataSourceID="SqlDataSource2">
    <EditItemTemplate>
        Client_id:
        <asp:Label ID="Client_idLabel1" runat="server" 
            Text='<%# Eval("Client_id") %>' />
        <br />
        ratlistaddr:
        <asp:TextBox ID="ratlistaddrTextBox" runat="server" 
            Text='<%# Bind("ratlistaddr") %>' />
        <br />
        head_office:
        <asp:TextBox ID="head_officeTextBox" runat="server" 
            Text='<%# Bind("head_office") %>' />
        <br />
        ho_addr2:
        <asp:TextBox ID="ho_addr2TextBox" runat="server" 
            Text='<%# Bind("ho_addr2") %>' />
        <br />
        ho_addr1:
        <asp:TextBox ID="ho_addr1TextBox" runat="server" 
            Text='<%# Bind("ho_addr1") %>' />
        <br />
        ho_addr3:
        <asp:TextBox ID="ho_addr3TextBox" runat="server" 
            Text='<%# Bind("ho_addr3") %>' />
        <br />
        ho_addr4:
        <asp:TextBox ID="ho_addr4TextBox" runat="server" 
            Text='<%# Bind("ho_addr4") %>' />
        <br />
        ho_postcode:
        <asp:TextBox ID="ho_postcodeTextBox" runat="server" 
            Text='<%# Bind("ho_postcode") %>' />
        <br />
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
            CommandName="Update" Text="Update" />
        &#160;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                    </EditItemTemplate>
                    
    <InsertItemTemplate>
        ratlistaddr:
        <asp:TextBox ID="ratlistaddrTextBox" runat="server" 
            Text='<%# Bind("ratlistaddr") %>' />
        <br />
        head_office:
        <asp:TextBox ID="head_officeTextBox" runat="server" 
            Text='<%# Bind("head_office") %>' />
        <br />
        ho_addr2:
        <asp:TextBox ID="ho_addr2TextBox" runat="server" 
            Text='<%# Bind("ho_addr2") %>' />
        <br />
        ho_addr1:
        <asp:TextBox ID="ho_addr1TextBox" runat="server" 
            Text='<%# Bind("ho_addr1") %>' />
        <br />
        ho_addr3:
        <asp:TextBox ID="ho_addr3TextBox" runat="server" 
            Text='<%# Bind("ho_addr3") %>' />
        <br />
        ho_addr4:
        <asp:TextBox ID="ho_addr4TextBox" runat="server" 
            Text='<%# Bind("ho_addr4") %>' />
        <br />
        ho_postcode:
        <asp:TextBox ID="ho_postcodeTextBox" runat="server" 
            Text='<%# Bind("ho_postcode") %>' />
        <br />
        <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
            CommandName="Insert" Text="Insert" />
        &#160;<asp:LinkButton ID="InsertCancelButton" runat="server" 
            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                    </InsertItemTemplate>
                    
    <ItemTemplate>
        Client_id:
        <asp:Label ID="Client_idLabel" runat="server" Text='<%# Eval("Client_id") %>' />
        <br />
        ratlistaddr:
        <asp:Label ID="ratlistaddrLabel" runat="server" 
            Text='<%# Bind("ratlistaddr") %>' />
        <br />
        head_office:
        <asp:Label ID="head_officeLabel" runat="server" 
            Text='<%# Bind("head_office") %>' />
        <br />
        ho_addr2:
        <asp:Label ID="ho_addr2Label" runat="server" Text='<%# Bind("ho_addr2") %>' />
        <br />
        ho_addr1:
        <asp:Label ID="ho_addr1Label" runat="server" Text='<%# Bind("ho_addr1") %>' />
        <br />
        ho_addr3:
        <asp:Label ID="ho_addr3Label" runat="server" Text='<%# Bind("ho_addr3") %>' />
        <br />
        ho_addr4:
        <asp:Label ID="ho_addr4Label" runat="server" Text='<%# Bind("ho_addr4") %>' />
        <br />
        ho_postcode:
        <asp:Label ID="ho_postcodeLabel" runat="server" 
            Text='<%# Bind("ho_postcode") %>' />
        <br />
                    </ItemTemplate>
                    
                </asp:FormView>
                
                <br />
                
                <br />
                
                <br />
                
            </ContentTemplate>
            
        </cc1:TabPanel>
    </cc1:TabContainer>
        <br />
        <br />
        <br />
        <br />
    
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
        SelectCommand="SELECT [Client_id], [ratlistaddr], [head_office], [ho_addr1], [ho_addr2], [company_name] FROM [Clients]">
    </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
            
        
        SelectCommand="SELECT [Client_id], [ratlistaddr], [head_office], [ho_addr2], [ho_addr1], [ho_addr3], [ho_addr4], [ho_postcode] FROM [Clients] WHERE ([Client_id] = @Client_id)">
            <SelectParameters>
                <asp:SessionParameter DefaultValue="1" Name="Client_id" 
                    SessionField="myrecordposition" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
          <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <br />
    </form>
</body>
</html>

Open in new window

0
 
David RobitailleAnalyst ProgrammerCommented:
You dont have to declare myrecordposition and then populate it with the result of gridview1.selectedvalue...
 
here is how to set a parameter to a sqldatasource.
http://www.asp.net/learn/data-access/tutorial-48-vb.aspx 
the idea is to get the client_id from the selected ited (it<s the role of sleected value)
then in the GridView1_SelectedIndexChanged, you set the SqlDataSource2 parameter and call databind so it loaded according to the parameter.
******
An other solution, if you follow prairiedog`s comment, involve simple math function to translate the gridview SelectedIndex and current page into the  FormView1.PageIndex.
it<s something like
(GridView1CurrentPageNunber-1) * PageSize)+SelectedIndex
0
 
Real_coffeeAuthor Commented:
Success!

In the end I used your suggestion of the maths function:

FormView1.PageIndex = (GridView1.PageIndex * GridView1.PageSize) + GridView1.SelectedIndex

Despite following the tutorials, I never did get the parameter to work. The tutorials did not use ajax tabs and this proved to be a problem for me. I simply couldnt figure out how to get the formview on tab 2 to use the parameter based upon a gridview select in tab 1. If anyone ever comes across an example of this I would be interested to see it in action.

Thanks davrob60.

Time for a coffee...
       
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now