refreshing a GridView after submitting data with a FormView

Setup:
I have a page that uses a GridView to display information out of a table in a database, and a FormView to insert new information into the same table.  There is a DropDownList (showing a list of states) that is the control for the GridView.  In this case, you pick a state from the DropDownList and see information pertinent to that state in the GridView.

Question:
My problem is that whenever I add another record to the table, I can't get the GridView to show the new information until I use the DropDownList to show a different state, then use the DropDownList again to go back to the original state.  Is there a parameter, control or property that I can use to automatically update the GridView for the new information?

Notes:
When you use refresh immediately after submitting data, the GridView does not update.  Even worse, the insert runs again, putting another (identical) entry into the table.  Refresh won't work.

I am looking for something similar to the AutoPostBack property found in the DropDownList.
chipsterva69Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ptleitchCommented:
you need to add

gridview1.DataBind()

to all of the formviews inserting updating, deleteing events that you want gridview to update on
0
chipsterva69Author Commented:
i have looked around and can find some instances of GridView.Databind being used in the back code while the data connection and dataset is established programmatically.  However, my page is setup with all of the connection and data code in the page itself.  Further, because of security rules placed on my for other reasons, all of my SQL queries have to be stored procedures.  I am not sure how the databind is supposed to be implemented for my code. (sorry - I'm a newbie.) I only have one formview event, insert, that I need to make the gridview respond to...  my page code is below if it helps:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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>HDMA - State Bill Information</title>
    <link href="http://www.hdma.net/includes/hdma.css" rel="stylesheet" type="text/css">
   
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p class = "title1">
        State Pedigree Legislation Administrative Page</p>
        <ul>
       
        page text

        </ul>
        <br />
        <p class="title2">Update/Delete bill information<br />
        <span style="font-size: 8pt">
        Select State:</span>
<asp:DropDownList ID="stateDropDownList1" runat="server" AutoPostBack="True" Font-Size="Small">
           
<asp:ListItem value="AL">Alabama</asp:ListItem>
      <asp:ListItem value="AK">Alaska</asp:ListItem>
       .... drop down list values...
      <asp:ListItem value="WI">Wisconsin</asp:ListItem>
      <asp:ListItem value="WY">Wyoming</asp:ListItem>
        </asp:DropDownList><br />
        <br />
       
<%
    Dim username
    username = Request.ServerVariables("LOGON_USER")
%>

<asp:GridView ID="stateGridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
            DataKeyNames="id" DataSourceID="stateinfoSqlDataSource1" ForeColor="#333333"
            GridLines="None" AllowPaging="True" AllowSorting="True" Font-Size="Small" OnRowUpdating="stateGridView1_ItemUpdate">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                    SortExpression="id" />
                <asp:BoundField DataField="bill_number" HeaderText="bill number" SortExpression="bill_number" />
                <asp:BoundField DataField="sponsor" HeaderText="sponsor" SortExpression="sponsor" />
                <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />
                <asp:BoundField DataField="summary" HeaderText="summary" SortExpression="summary" />
                <asp:BoundField DataField="link" HeaderText="link" SortExpression="link" />
                <asp:BoundField DataField="state_id" HeaderText="state" SortExpression="state_id" />
                <asp:BoundField DataField="entered" HeaderText="entered" SortExpression="entered" ReadOnly="True" />
                <asp:BoundField DataField="entered_user" HeaderText="data entered by" SortExpression="entered_user" ReadOnly="True" />
                <asp:BoundField DataField="updated" HeaderText="updated" SortExpression="updated" ReadOnly="True" />
                <asp:BoundField DataField="updated_user" HeaderText="data updated by" SortExpression="updated_user" ReadOnly="True" />
            </Columns>
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="False" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
        <asp:SqlDataSource ID="stateinfoSqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:statedetailSqlDataSource2 %>"
            DeleteCommand="sp_StateBill_Delete" DeleteCommandType="StoredProcedure"
            SelectCommand="sp_StateBill_Select_Grid" SelectCommandType="StoredProcedure"
            UpdateCommand="sp_StateBill_UpdateInfo" UpdateCommandType="StoredProcedure">
            <DeleteParameters>
                <asp:Parameter Name="id" Type="Int32" />
                <asp:Parameter Name="username" Type="String" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="id" Type="Int32" />
                <asp:Parameter Name="bill_number" Type="String" />
                <asp:Parameter Name="sponsor" Type="String" />
                <asp:Parameter Name="status" Type="String" />
                <asp:Parameter Name="summary" Type="String" />
                <asp:Parameter Name="link" Type="String" />
                <asp:Parameter Name="state_id" Type="String" />
                <asp:Parameter Name="updated" Type="DateTime" />
                <asp:Parameter Name="entered" Type="DateTime" />
                <asp:Parameter Name="username" Type="String" />
            </UpdateParameters>
            <SelectParameters>
                <asp:Parameter Name="id" Type="Int32" />
                <asp:Parameter Name="bill_number" Type="String" />
                <asp:Parameter Name="sponsor" Type="String" />
                <asp:Parameter Name="status" Type="String" />
                <asp:Parameter Name="summary" Type="String" />
                <asp:Parameter Name="link" Type="String" />
                <asp:ControlParameter ControlID="stateDropDownList1" DefaultValue="AL" Name="state_id"
                    PropertyName="SelectedValue" Type="String" />
                <asp:Parameter Name="entered" Type="String" />
                <asp:Parameter Name="entered_user" Type="String" />
                <asp:Parameter Name="updated" Type="String" />
                <asp:Parameter Name="updated_user" Type="String" />
                <asp:Parameter Name="username" Type="String" />
               
            </SelectParameters>
        </asp:SqlDataSource>
        <br />
        <p class="title2">Insert new bill information<br />
        <asp:FormView ID="FormView1" runat="server" DataKeyNames="bill_number"
            DataSourceID="SqlDataSource1" OnItemInserting="FormView1_ItemInserting" DefaultMode="Insert">            
            <InsertItemTemplate>
                Bill number:
                <asp:TextBox ID="bill_numberTextBox" runat="server" Text='<%# Bind("bill_number") %>'>
                </asp:TextBox><br />
                Sponsor:
                <asp:TextBox ID="sponsorTextBox" runat="server" Text='<%# Bind("sponsor") %>'>
                </asp:TextBox><br />
                Status:
                <asp:TextBox ID="statusTextBox" runat="server" Text='<%# Bind("status") %>'>
                </asp:TextBox><br />
                Summary:
                <asp:TextBox ID="summaryTextBox" runat="server" Text='<%# Bind("summary") %>'>
                </asp:TextBox><br />
                Link to bill (include http://):
                <asp:TextBox ID="linkTextBox" runat="server" Text='<%# Bind("link") %>'>
                </asp:TextBox><br />
                State:
                <asp:DropDownList ID="state_idDropDown" runat="server"
                        DataSourceID="state_idDataSource"
                        DataTextField="state" DataValueField="state_id"
                        SelectedValue='<%# Bind("state_id") %>' >
                    </asp:DropDownList><br />
                    <asp:SqlDataSource ID="state_idDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:hdma-stateinfoConnectionString1 %>"
                SelectCommand="SELECT [state_id], [state] FROM [states]"></asp:SqlDataSource>
                <!--Date entered:
                <asp:TextBox ID="enteredTextBox" runat="server" Text='<%# Eval("entered") %>'>
                </asp:TextBox><br />  -->
               <!--  Date updated:
                <asp:TextBox ID="updatedTextBox" runat="server" Text='<%# Bind("updated") %>'>
                </asp:TextBox><br /> -->
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                    Text="Insert">
                </asp:LinkButton>&nbsp;&nbsp;&nbsp;
                <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel">
                </asp:LinkButton>
            </InsertItemTemplate>
            <ItemTemplate>
             <!--   bill_number:
                <asp:Label ID="bill_numberLabel" runat="server" Text='<%# Eval("bill_number") %>'>
                </asp:Label><br />
                sponsor:
                <asp:Label ID="sponsorLabel" runat="server" Text='<%# Bind("sponsor") %>'></asp:Label><br />
                status:
                <asp:Label ID="statusLabel" runat="server" Text='<%# Bind("status") %>'></asp:Label><br />
                summary:
                <asp:Label ID="summaryLabel" runat="server" Text='<%# Bind("summary") %>'></asp:Label><br />
                link:
                <asp:Label ID="linkLabel" runat="server" Text='<%# Bind("link") %>'></asp:Label><br />
                state_id:
                <asp:Label ID="state_idLabel" runat="server" Text='<%# Bind("state_id") %>'></asp:Label><br />
                entered:
                <asp:Label ID="enteredLabel" runat="server" Text='<%# Bind("entered") %>'></asp:Label><br />
                updated:
                <asp:Label ID="updatedLabel" runat="server" Text='<%# Bind("updated") %>'></asp:Label><br />
                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit">
                </asp:LinkButton>
                <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                    Text="Delete">
                </asp:LinkButton>  -->
               
                <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                    Text="Click here to add a new bill">
                </asp:LinkButton>
            </ItemTemplate>
        </asp:FormView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:statedetailSqlDataSource2 %>"
            SelectCommand="sp_StateBill_Select" SelectCommandType="StoredProcedure"
            InsertCommand="sp_StateBill_Insert" InsertCommandType="StoredProcedure">
            <InsertParameters>
                <asp:Parameter Name="id" Type="String" />
                <asp:Parameter Name="bill_number" Type="String" />
                <asp:Parameter Name="sponsor" Type="String" />
                <asp:Parameter Name="status" Type="String" />
                <asp:Parameter Name="summary" Type="String" />
                <asp:Parameter Name="link" Type="String" />
                <asp:Parameter Name="state_id" Type="String" />
                <asp:Parameter Name="entered" Type="DateTime" />
                <asp:Parameter Name="updated" Type="DateTime" />
                <asp:Parameter Name="entered_user" Type="String" />
                <asp:Parameter Name="updated_user" Type="String" />
                <asp:Parameter Name="username" Type="String" />
            </InsertParameters>
            <SelectParameters>
                <asp:Parameter Name="id" Type="Int32" />
                <asp:Parameter Name="bill_number" Type="String" />
                <asp:Parameter Name="sponsor" Type="String" />
                <asp:Parameter Name="status" Type="String" />
                <asp:Parameter Name="summary" Type="String" />
                <asp:Parameter Name="link" Type="String" />
                <asp:Parameter Name="state_id" Type="String" />
                <asp:Parameter Name="entered" Type="String" />
                <asp:Parameter Name="updated" Type="String" />
                <asp:Parameter Name="entered_user" Type="String" />
                <asp:Parameter Name="updated_user" Type="String" />
                <asp:Parameter Name="username" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
     </p>
       
     </div>
    </form>
</body>
</html>


------------------------------- >>>>  back code


Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
        SqlDataSource1.InsertParameters("username").DefaultValue = Request.ServerVariables("logon_user")
    End Sub

    Protected Sub stateGridView1_ItemUpdate(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        stateinfoSqlDataSource1.UpdateParameters("username").DefaultValue = Request.ServerVariables("logon_user")
    End Sub
End Class

thanks for any help you can offer!
0
ptleitchCommented:
Have you tried?

Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
        SqlDataSource1.InsertParameters("username").DefaultValue = Request.ServerVariables("logon_user")
        stateGridView1.DataBind()
End Sub

I will look at your problem more closely a little later.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chipsterva69Author Commented:
yep - same issue - the formview inserts the info, but the gridview does not automatically update.
0
chipsterva69Author Commented:
ended up solving this by putting the data insert function onto a seperate page - i never could get the formfield insert to bind back to the gridview and force a postback upon inserting...  now users see the gridview control with data and can edit and delete from there; they click on a link to open a page with a formview control that only inserts (and is the default view), and when they are done loading in the data, click on a link that sends them back to the gridview page.  not quite as slick but it is functional.

thanks for the help, ptleitch - you got me pointed in the right direction.  i am sure i could have worked it out with enough time, but the solution in place works just as well.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.