首先,我对此还是个新手,所以请原谅我.我创建了一个网页,其中有一个表单,人们可以填写并在链接到SQLTABLE的网格视图中显示条目.该页面适合一个用户,但如果多个用户正在使用该网站,我希望所有用户都能自动看到对SQLTABLE的任何更改.在阅读了多个站点后,听起来我想在网格视图上使用计时器,但找不到任何关于如何做到这一点的东西.

如果有人能给我指个正确的方向?

ASPX

%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Workstation_Role.aspx.cs" Inherits="OnCallWeb.Dispatch_Policies.Workstation_Role" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            font-size: 14pt;
        }
        .auto-style3 {
            font-size: 14pt;
            width: 56px;
            text-align: right;
        }
        .auto-style5 {
            font-size: 14pt;
            width: 311px;
        }
        .auto-style6 {
            font-size: 12pt;
        }
        .auto-style7 {
            font-size: 14pt;
            text-align: right;
        }
        .auto-style8 {
            text-align: center;
        }
        .auto-style9 {
            font-size: 14pt;
            margin-top: 9px;
        }
        .auto-style10 {
            font-size: 14pt;
            width: 214px;
            text-align: left;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table class="auto-style1">
            <tr>
                <td class="auto-style3">Name:</td>
                <td class="auto-style5">
                    <asp:DropDownList ID="DropDownList1" runat="server" CssClass="auto-style2" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Name" Width="300px">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:OnCallWebConnectionString %>" SelectCommand="SELECT [Name] FROM [Personnel] ORDER BY [Name]"></asp:SqlDataSource>
                </td>
                <td class="auto-style10">Postion #: <asp:DropDownList ID="DropDownList3" runat="server" CssClass="auto-style2" Width="130px">
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>Training 7</asp:ListItem>
                        <asp:ListItem>Training 8</asp:ListItem>
                        <asp:ListItem>Training 9</asp:ListItem>
                        <asp:ListItem>Office</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td class="auto-style2">
                    Roles:
             Roles:
                    <asp:TextBox ID="TextBox1" runat="server" CssClass="auto-style2" Width="772px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style3">Status:</td>
                <td class="auto-style2" colspan="3">
                    <asp:DropDownList ID="DropDownList4" runat="server" CssClass="auto-style2" Width="223px">
                        <asp:ListItem>At Desk</asp:ListItem>
                        <asp:ListItem>Away From Desk</asp:ListItem>
                    </asp:DropDownList>
                &nbsp;Dispatcher Covering:
                    <asp:DropDownList ID="DropDownList2" runat="server" CssClass="auto-style2" DataSourceID="SqlDataSource3" DataTextField="Name" DataValueField="Name" Width="300px">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:OnCallWebConnectionString %>" SelectCommand="SELECT [Name] FROM [PersonnelCovering] ORDER BY [Name]"></asp:SqlDataSource>
                    <asp:Button ID="btnSave" runat="server" CssClass="auto-style6" OnClick="btnSave_Click" Text="Add To List" BackColor="#006600" ForeColor="White" />
&nbsp;
                    <asp:Button ID="btnUpdate" runat="server" CssClass="auto-style6" OnClick="btnUpdate_Click" Text="Update" BackColor="Yellow" />
&nbsp;
                    <asp:Button ID="btnDelete" runat="server" CssClass="auto-style6" OnClick="btnDelete_Click" Text="Delete" BackColor="Red" ForeColor="White" />
&nbsp;
                    <asp:Button ID="btnCancel" runat="server" CssClass="auto-style6" OnClick="btnCancel_Click" Text="Clear" BorderColor="Black" />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;
                    &nbsp;<asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="auto-style7" colspan="4">
                    <div class="auto-style8">
                    </div>
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:OnCallWebConnectionString %>" SelectCommand="SELECT [Name], [Position], [Roles], [Status], [DispatcherCovering], [ID] FROM [Dispatcher_Roles]"></asp:SqlDataSource>
                </td>
            </tr>
        </table>
        <div>
                    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" CssClass="auto-style9" Width="1757px" EmptyDataText="No Records Found!">
                        <Columns>
                            <asp:CommandField ShowSelectButton="True" />
                            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                            <asp:BoundField DataField="Position" HeaderText="Position" SortExpression="Position" />
                            <asp:BoundField DataField="Roles" HeaderText="Roles" SortExpression="Roles" />
                            <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
                            <asp:BoundField DataField="DispatcherCovering" HeaderText="DispatcherCovering" SortExpression="DispatcherCovering" />
                            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" Visible="False" />
                        </Columns>
                    </asp:GridView>
        </div>
    </form>
</body>
</html>

代码隐藏

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Timers;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace OnCallWeb.Dispatch_Policies
{
    public partial class Workstation_Role : System.Web.UI.Page
    {
        string cs = ConfigurationManager.ConnectionStrings["OnCallWebConnectionString"].ConnectionString;
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter adapter;
        DataTable dt;

        public void DataLoad()
        {
            if (Page.IsPostBack)
            {
                GridView1.DataBind();
            }
        }

        public void ClearAllData()
        {
            DropDownList1.SelectedValue = DropDownList1.Items[0].ToString();
            DropDownList2.SelectedValue = DropDownList2.Items[0].ToString();
            DropDownList3.SelectedValue = DropDownList3.Items[0].ToString();
            DropDownList4.SelectedValue = DropDownList4.Items[0].ToString();
            TextBox1.Text = "";
            lblMessage.Text = "";
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList1.SelectedValue = GridView1.SelectedRow.Cells[1].Text;
            DropDownList3.Text = GridView1.SelectedRow.Cells[2].Text;
            TextBox1.Text = GridView1.SelectedRow.Cells[3].Text;
            DropDownList4.Text = GridView1.SelectedRow.Cells[4].Text;
            DropDownList2.Text = GridView1.SelectedRow.Cells[5].Text;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text != "" && DropDownList1.SelectedValue != "" && DropDownList3.SelectedValue != "")
            {
                using (con = new SqlConnection(cs))
                {
                    con.Open();
                    cmd = new SqlCommand("Insert into Dispatcher_Roles (Name, Position, Roles, Status, DispatcherCovering) Values(@Name, @Position, @Roles, @Status, @DispatcherCovering)", con);
                    cmd.Parameters.AddWithValue("@Name", DropDownList1.SelectedValue);
                    cmd.Parameters.AddWithValue("@Position", DropDownList3.SelectedValue);
                    cmd.Parameters.AddWithValue("@Roles", TextBox1.Text);
                    cmd.Parameters.AddWithValue("@Status", DropDownList4.SelectedValue);
                    cmd.Parameters.AddWithValue("@DispatcherCovering", DropDownList2.SelectedValue);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    DataLoad();
                    ClearAllData();
                }
            }
            else
            {
                lblMessage.Text = "***Fill In All Information***";
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text != "" || DropDownList1.Text !="" || DropDownList2.Text !="")
                
            {
                using (con = new SqlConnection(cs))
                {
                    con.Open();
                    cmd = new SqlCommand("Update Dispatcher_Roles Set Name=@Name, Position=@Position, Roles=@Roles, Status=@Status, DispatcherCovering=@DispatcherCovering where Name=@Name", con);
                    cmd.Parameters.AddWithValue("@Name", DropDownList1.SelectedValue);
                    cmd.Parameters.AddWithValue("@Position", DropDownList3.SelectedValue);
                    cmd.Parameters.AddWithValue("@Roles", TextBox1.Text);
                    cmd.Parameters.AddWithValue("@Status", DropDownList4.SelectedValue);
                    cmd.Parameters.AddWithValue("@DispatcherCovering", DropDownList2.SelectedValue);

                    cmd.ExecuteNonQuery();
                    con.Close();
                    DataLoad();
                    ClearAllData();
                }
            }
            else
            {
                lblMessage.Text = "Fill In All All Fields";
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            using(con=new SqlConnection(cs))
            {
                con.Open();
                cmd = new SqlCommand("Delete From Dispatcher_Roles where Name=@Name", con);
                cmd.Parameters.AddWithValue("@Name", GridView1.SelectedRow.Cells[1].Text);
                cmd.ExecuteNonQuery();                
                con.Close();
                DataLoad();
                ClearAllData();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearAllData();
        }
    }
}

推荐答案

要为查看网格视图的多个用户实现实时更新,您可以考虑使用ASP.NET中的SignalR库.SignalR允许您通过使服务器端代码能够将内容即时推送到连接的客户端来构建实时Web应用程序.

以下是如何使用SignalR实现网格视图的实时更新:

第1步:安装SignalR

  • 在Visual Studio中,右击您的项目并 Select "管理NuGet包".
  • 搜索"Microsoft.AspNet.SignalR"并安装该程序包.

步骤2:创建SignalR集线器

  • 向项目中添加一个新类,并将其命名为类似于"GridUpdateHub.cs"的名称.

  • 从SignalR提供的Hub继承类.

  • 在集线器中实现一个方法,该方法将被调用以向客户端发送更新.例如,您可以创建一个名为"UpdateGrid"的方法,将更新后的网格数据发送到连接的客户端.

    使用Microsoft.AspNet.SignalR;

    公共类GridUpdateHub:Hub { PUBLIC QUID UPDATE网格(字符串数据) { //将更新后的数据发送给所有连接的客户端 Clients.All.updateGrid(Data); } }

步骤3:注册SignalR Hub

  • 打开项目中的"Global.asax.cs"文件.

  • Application_Start方法中添加以下代码以注册SignalR集线器:

      使用Microsoft.AspNet.SignalR;
      using System.Web.Routing;
    
      protected void Application_Start(object sender, EventArgs e)
      {
          // Register the SignalR hub
          RouteTable.Routes.MapHubs();
      }
    

    步骤4:更新ASPX页面

    • 在您的ASPX页面中添加对SignalR JavaScript库的引用.

    • 添加一段JavaScript代码以连接到SignalR集线器并处理接收到的更新.将此代码放在<head>部分或<body>部分的末尾.

    $(function () { // Connect to the SignalR hub var hub = $.connection.gridUpdateHub; // Define a client-side method to handle received updates hub.client.updateGrid = function (data) { // Update the gridview or perform any other necessary actions with the updated data // For example, you can use JavaScript/jQuery to update the gridview dynamically }; // Start the SignalR connection $.connection.hub.start(); });

步骤5:从服务器端代码触发更新

  • 在您的服务器端代码中(例如,在btnSave_Click或btnUpdate_Click方法中),在保存或更新SQL表中的数据之后,您可以使用SignalR集线器将更新后的数据发送到连接的客户端.

    使用Microsoft.AspNet.SignalR;

    受保护的无效btnSave_Click(对象发送者,事件参数e) { //将数据保存到SQL表

      // Get the updated data from the SQL table
      string updatedData = GetUpdatedGridData(); // Implement this method to retrieve the updated data
    
      // Send the updated data to connected clients
      var hubContext = GlobalHost.ConnectionManager.GetHubContext<GridUpdateHub>();
      hubContext.Clients.All.updateGrid(updatedData);
    

    }

就这样!在此实现中,每当用户保存或更新数据时,SignalR集线器都会将更新的数据发送到所有连接的客户端,而ASPX页面中的JavaScript代码将处理接收到的更新,并相应地实时更新所有用户的网格视图

编码快乐!!

Sql相关问答推荐

GROUP BY与多个嵌套查询T—SQL

SQL更新,在2个额外的表上使用内部连接

如何用3个(半)固定位置建模团队,并有效地搜索相同/不同的团队?

数据库索引:如何使用名称和类别对项目进行最佳索引?

没有循环的SQL更新多个XML node 值

Django将字符串筛选为整数?

SQL:如何将相应位置的两个数组中的元素组合在一起

按分类标准检索记录

Oracle中的时间戳

带日期函数的复合索引不允许只扫描索引吗?

如何为缺少的类别添加行

如何对 jsonb 中的字段执行求和,然后使用结果过滤查询的输出

查询中获取审批者不起作用

查询以查找今天和昨天的数据之间的差异以及伪列

使用其他表 SUM 的交换价格转换价格并获得 AVG

存储过程 - 动态 SQL 中不同列值的计数

如何从 2 个 SQLite 表构建嵌套对象?

根据条件列出不同的值

如何根据另一列对行值进行分组?

SQL/Postgres:按日期和其他属性对相关性能进行分组