[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

refreshing a GridView after submitting data with a FormView

Posted on 2006-04-13
5
Medium Priority
?
514 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:chipsterva69
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:ptleitch
ID: 16448019
you need to add

gridview1.DataBind()

to all of the formviews inserting updating, deleteing events that you want gridview to update on
0
 

Author Comment

by:chipsterva69
ID: 16448683
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
 
LVL 3

Accepted Solution

by:
ptleitch earned 750 total points
ID: 16448768
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
 

Author Comment

by:chipsterva69
ID: 16448788
yep - same issue - the formview inserts the info, but the gridview does not automatically update.
0
 

Author Comment

by:chipsterva69
ID: 16525632
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question