刷新服务器中存在的页面中的gridview数据

时间:2013-07-26 03:07:26

标签: c# asp.net

我创建了一个包含gridview的asp.net网页。当从工具箱中拖放(而不是从代码中)时,我通过gridview控件提供的步骤将gridview绑定到访问数据库。该页面实际上在服务器中。当我在客户端(在另一个网页中)进行更新(例如:添加或删除数据库中的记录)时,尽管数据库中的值已更改,但gridview不会反映更改。只有当我在服务器中刷新时,才会在客户端看到更改。请帮助。

3 个答案:

答案 0 :(得分:1)

您可以使用asp.net ajax timer控件定期刷新页面/部分页面。以下示例显示您可以每5秒更新一次服务器时间。同样,您将更新GridView

HTML

<asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:Timer runat="server" id="UpdateTimer" interval="5000" ontick="UpdateTimer_Tick" />
    <asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
        </Triggers>
        <ContentTemplate>
            <asp:Label runat="server" id="DateStampLabel" />
        </ContentTemplate>
    </asp:UpdatePanel>

背后的代码

protected void UpdateTimer_Tick(object sender, EventArgs e)
{
    DateStampLabel.Text = DateTime.Now.ToString();
}

答案 1 :(得分:1)

实现此目标的最佳方法是使用 SignalR

以下教程将为您提供有关如何实现服务器广播的建议。

点击here获取示例。

答案 2 :(得分:0)

谢谢大家。以下代码对我有用:

aspx页面:

    

<asp:Panel ID="Panel2" runat="server" Height="146px" Style="z-index: 100; left: 382px;
        position: absolute; top: 247px" Visible="true" Width="235px">
         <asp:Label ID="deptlabel" runat="server" 
    style="z-index: 1; left: 0px; top: 33px; position: absolute" Text="Label" 
    Visible="False"></asp:Label>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
 <asp:UpdatePanel ID="UpdatePanel1" runat="server">

         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" Style="z-index: 100; left: -146px;
            position: absolute; top: 216px; height: 374px; width: 1024px;" AllowSorting="True" 
             CellPadding="3" AllowPaging="True" BackColor="#DEBA84" BorderColor="#DEBA84" 
             BorderStyle="None" BorderWidth="1px" CellSpacing="2">
            <Columns>
            <asp:TemplateField HeaderText="Delete">
                <ItemTemplate>
                    <asp:CheckBox ID="chkDelete" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
                <asp:BoundField DataField="bid" HeaderText="bid" 
                    SortExpression="bid" />
                <asp:BoundField DataField="did" HeaderText="did" SortExpression="did" />
                <asp:BoundField DataField="bname" HeaderText="bnme" 
                    SortExpression="bname" />
                <asp:BoundField DataField="author" HeaderText="athr" 
                    SortExpression="author" />
                <asp:BoundField DataField="price" HeaderText="cost" 
                    SortExpression="price" />
                <asp:BoundField DataField="edition" HeaderText="editn" 
                    SortExpression="edition" />
                <asp:BoundField DataField="category" HeaderText="ctgry" 
                    SortExpression="category" />
                <asp:BoundField DataField="publisher" HeaderText="publsr" 
                    SortExpression="publisher" />
                <asp:BoundField DataField="status" HeaderText="stat" 
                    SortExpression="status" />
                <asp:BoundField DataField="acpr" HeaderText="acpr" SortExpression="acpr" />
                <asp:BoundField DataField="volume" HeaderText="vol" 
                    SortExpression="volume" />
                <asp:BoundField DataField="ref" HeaderText="ref" SortExpression="ref" />
                <asp:BoundField DataField="pages" HeaderText="pgs" SortExpression="pages" />
            </Columns>
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        </asp:GridView>

         </ContentTemplate>
</asp:UpdatePanel>
    &nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:con %>" 
              SelectCommand="SELECT DISTINCT * FROM [books] WHERE ([bid] LIKE '%' + @bid + '%')">
         <SelectParameters>
             <asp:ControlParameter ControlID="deptlabel" Name="bid" PropertyName="Text" 
                 Type="String" />
         </SelectParameters>  </asp:SqlDataSource>

        &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
</asp:Panel>
<asp:Label ID="Label9" runat="server" Font-Size="XX-Large" ForeColor="#C04000" Style="z-index: 102;
    left: 492px; position: absolute; top: 256px; width: 678px;" 
    Text="BOOKS REMOVE"></asp:Label>

default.aspx.cs:

protected void Timer1_Tick(object sender,EventArgs e)     {

    GridView1.DataBind();

}