1

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

4

3 回答 3

1

您可以使用asp.net ajax 计时器控件定期刷新页面/页面的一部分。以下示例显示您可以每 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();
}
于 2013-07-26T03:14:34.267 回答
1

实现这一点的最佳方法是使用SignalR

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

单击此处查看示例。

于 2013-07-26T04:03:07.657 回答
0

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

页面:

<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>

默认.aspx.cs:

protected void Timer1_Tick(对象发送者,EventArgs e){

    GridView1.DataBind();

}
于 2013-08-02T15:27:42.027 回答