c# - 在 ASP.NET 中从数据库中获取数据而不刷新页面

标签 c# asp.net sql-server web-site-project

我正在 ASP.net 和 SQL Server 中做一个项目。我在用户登录屏幕上调用存储过程来验证用户。但是当我调用存储过程时,需要刷新整个页面才能获取数据。

如何在不刷新页面的情况下实现同样的效果?

这是我当前的代码

sql = "EXEC dbo.sProc_Admin_Auth @UserNm = '" + User + "',@Pwd = '"+Pwd+"'";

cmd = new SqlCommand(sql, cn.connect());

dr = cmd.ExecuteReader();

if(dr.Read())
{
    Session["UserId"] = dr["UserId"].ToString();
    Session["LoginId"] = User;
    Session["UserNm"] = dr["FullNm"].ToString();// "Jayasurya Satheesh";
    Session["Email"] = dr["Email"].ToString();
    Session["JoinDt"] = dr["CreateDt"].ToString();

    Response.Redirect("Index.aspx");
    LblError.Visible = false;
}
else
{
    LblError.Visible = true;
    LblError.Text = "Login Failed!";
}

最佳答案

使用 Ajax 扩展,这是一个简单的例子:

.aspx 文件

    <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:TextBox runat="server" id="username" name="username" placeholder="Enter Username"></asp:TextBox>
            <asp:TextBox name="passwd" ID="passwd" runat="server" placeholder="Enter Password"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Login" onclick="Button1_Click" />
            <br />
            <asp:Label ID="LblError" runat="server"></asp:Label>
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

aspx.cs 文件 - 将其添加到登录按钮的 Click 事件中

 protected void Button1_Click(object sender, EventArgs e)
        {
            string sql = "";
            SqlConnection cn = null;
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            string User = username.Text;
            string Pwd = passwd.Text;
            //cn = "<< your connection string>>";
            try
            {
                cn.Open();
                // Your code

                sql = "EXEC dbo.sProc_Admin_Auth @UserNm = '" + User + "',@Pwd = '" + Pwd + "'";

                cmd = new SqlCommand(sql, cn);

                dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                    Session["UserId"] = dr["UserId"].ToString();
                    Session["LoginId"] = User;
                    Session["UserNm"] = dr["FullNm"].ToString();// "Jayasurya Satheesh";
                    Session["Email"] = dr["Email"].ToString();
                    Session["JoinDt"] = dr["CreateDt"].ToString();
                    Response.Redirect("Index.aspx");
                    LblError.Visible = false;
                }
                else
                {
                    LblError.Visible = true;
                    LblError.Text = "Login Failed!";
                }
            }
            catch (Exception exce)
            {
                LblError.Text = exce.Message;
            }
            finally
            {
                cn.Close();
            }
        }

You can find UpdatePanel and ScriptManager under Toolbox -> Ajax Extension


Use try-catch block to handle runtime exceptions.

关于c# - 在 ASP.NET 中从数据库中获取数据而不刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47966467/

相关文章:

c# - Azure 机器学习 Web 服务不使用传递的 .ilearn 模型

c# - ConcurrencyMode.Multiple 和 WebOperationContext.Current 静态属性

javascript - 如何告诉 javascript 函数要操作什么元素?

sql-server - 将 nvarchar 转换为 int 时转换失败

c# - 从 C# Windows 窗体应用程序在 SQLite 数据库中记录错误

c# - 如何优化 ASP.NET MVC Controller 以最小化数据库查询次数

asp.net - 如何在 ASP.NET 中查询 MySQL?

c# - 连接两个表以绑定(bind)在 gridview 上

mysql - 如果我从 Windows 7 的注册表中更改 ODBC DSN,我在 MS Access 中的链接表是否需要刷新?

sql-server - VS2012自动化数据库部署问题-.dacpac不存在?