GridView / FormView binding question

Discussion in 'ASP .Net' started by Bob, Aug 1, 2006.

  1. Bob

    Bob Guest

    Hi All:
    I have a GridView with several pages and a FormView linked via a
    ControlParameter. The FormView populates correctly when the user makes
    a selection from the first page of the grid. However, when the user
    goes to the next page of the grid, the ControlParameter is passing in a
    null (or the default value) to the FormView. I want to populate the
    formview with the data from the first item of the grid. I've set
    GridView1.SelectedIndex in GridView1_PageIndexChanging, but no luck.
    Any ideas?
     
    Bob, Aug 1, 2006
    #1
    1. Advertising

  2. I don't know why this is happening with you but try the following ASP.NET 2.0
    script on your aspx page, note that I didn't use any code behind and I'm
    using SqlDataSource and the same aspect will work for ObjectDataSource.

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    AutoGenerateColumns="False"
    DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
    <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="ProductID" HeaderText="ProductID"
    InsertVisible="False"
    ReadOnly="True" SortExpression="ProductID" />
    <asp:BoundField DataField="ProductName"
    HeaderText="ProductName" SortExpression="ProductName" />
    </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT [ProductID], [ProductName] FROM
    [Products]"></asp:SqlDataSource>
    <asp:FormView ID="FormView1" runat="server" DataKeyNames="ProductID"
    DataSourceID="SqlDataSource2">
    <EditItemTemplate>
    ProductID:
    <asp:Label ID="ProductIDLabel1" runat="server" Text='<%#
    Eval("ProductID") %>'></asp:Label><br />
    ProductName:
    <asp:TextBox ID="ProductNameTextBox" runat="server"
    Text='<%# Bind("ProductName") %>'>
    </asp:TextBox><br />
    UnitPrice:
    <asp:TextBox ID="UnitPriceTextBox" runat="server" Text='<%#
    Bind("UnitPrice") %>'>
    </asp:TextBox><br />
    UnitsInStock:
    <asp:TextBox ID="UnitsInStockTextBox" runat="server"
    Text='<%# Bind("UnitsInStock") %>'>
    </asp:TextBox><br />
    <asp:LinkButton ID="UpdateButton" runat="server"
    CausesValidation="True" CommandName="Update"
    Text="Update">
    </asp:LinkButton>
    <asp:LinkButton ID="UpdateCancelButton" runat="server"
    CausesValidation="False" CommandName="Cancel"
    Text="Cancel">
    </asp:LinkButton>
    </EditItemTemplate>
    <InsertItemTemplate>
    ProductName:
    <asp:TextBox ID="ProductNameTextBox" runat="server"
    Text='<%# Bind("ProductName") %>'>
    </asp:TextBox><br />
    UnitPrice:
    <asp:TextBox ID="UnitPriceTextBox" runat="server" Text='<%#
    Bind("UnitPrice") %>'>
    </asp:TextBox><br />
    UnitsInStock:
    <asp:TextBox ID="UnitsInStockTextBox" runat="server"
    Text='<%# Bind("UnitsInStock") %>'>
    </asp:TextBox><br />
    <asp:LinkButton ID="InsertButton" runat="server"
    CausesValidation="True" CommandName="Insert"
    Text="Insert">
    </asp:LinkButton>
    <asp:LinkButton ID="InsertCancelButton" runat="server"
    CausesValidation="False" CommandName="Cancel"
    Text="Cancel">
    </asp:LinkButton>
    </InsertItemTemplate>
    <ItemTemplate>
    ProductID:
    <asp:Label ID="ProductIDLabel" runat="server" Text='<%#
    Eval("ProductID") %>'></asp:Label><br />
    ProductName:
    <asp:Label ID="ProductNameLabel" runat="server" Text='<%#
    Bind("ProductName") %>'>
    </asp:Label><br />
    UnitPrice:
    <asp:Label ID="UnitPriceLabel" runat="server" Text='<%#
    Bind("UnitPrice") %>'></asp:Label><br />
    UnitsInStock:
    <asp:Label ID="UnitsInStockLabel" runat="server" Text='<%#
    Bind("UnitsInStock") %>'>
    </asp:Label><br />
    </ItemTemplate>
    </asp:FormView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice],
    [UnitsInStock] FROM [Products] WHERE ([ProductID] = @ProductID)">
    <SelectParameters>
    <asp:ControlParameter ControlID="GridView1" Name="ProductID"
    PropertyName="SelectedValue"
    Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>

    Hope this will help you
     
    =?Utf-8?B?TXVoYW1tYWQgTW9zYQ==?=, Aug 2, 2006
    #2
    1. Advertising

  3. Note the behaviour when you select a record and then move to next page.
    suppose that you select record number 2, when you move to next page or any
    other page, the record number 2 will on the seeked page will be apear as
    selected.
     
    =?Utf-8?B?TXVoYW1tYWQgTW9zYQ==?=, Aug 2, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGlt?=

    FormView - RadioButtonList binding

    =?Utf-8?B?VGlt?=, Apr 28, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    5,327
    =?Utf-8?B?VGlt?=
    Apr 28, 2006
  2. Amit
    Replies:
    6
    Views:
    13,806
    Assimalyst
    Oct 24, 2006
  3. Chris

    Formview within Formview

    Chris, May 19, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    531
    Chris
    May 19, 2007
  4. JcFx
    Replies:
    0
    Views:
    278
  5. David Thielen

    FormView/Repeater/ Eval(FormView data)

    David Thielen, Jun 16, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    645
    David Thielen
    Jun 16, 2006
Loading...

Share This Page