无法使用 SQL 数据激活消息框 - ASP.NET

发布于 2024-08-02 00:36:56 字数 1299 浏览 8 评论 0原文

我正在测试我对 ADO.NET 和 SQL 的了解,目前只是尝试做基础知识。 我想从表中读取数据,当用户单击按钮时,会弹出一个消息框,其中包含 ApplicationName 列中的值。

当我单击按钮时,它目前不会执行任何操作...有什么想法吗?

protected void TestSubmit_ServerClick(object sender, EventArgs e)
    {
        // Initialize the database connector.
        SqlConnection connectSQL = new SqlConnection();

        // Send the connection string.
        connectSQL.ConnectionString = @"Data Source = localhost\SQLEXPRESS;" + 
            "Initial Catalog = Inputs; Integrated Security = SSPI";

        try
        {
            // Setup our command.
            SqlCommand theCommand = new SqlCommand("SELECT * FROM Inputs", connectSQL);

            // Write the stored value in the text boxes.
            connectSQL.Open();

            SqlDataReader theReader;

            theReader = theCommand.ExecuteReader();
            theReader.Read();
            MessageBox(theReader["ApplicationName"].ToString());

            theReader.Close();
            connectSQL.Close();
        }
        catch (Exception ee)
        {
            MessageBox("Oopsie: " + ee);
        }       

 private void MessageBox(string msg)
    {
        Label lbl = new Label();
        lbl.Text = "" + Environment.NewLine + "window.alert('" + msg + "')";
        Page.Controls.Add(lbl);
    }

I am testing my knowledge of ADO.NET and SQL and am currently just trying to do the basics. I want to read from a table and when the user clicks a button, a message box pops up with the value in the ApplicationName column.

It currently doesn't do anything when I click the button... any ideas?

protected void TestSubmit_ServerClick(object sender, EventArgs e)
    {
        // Initialize the database connector.
        SqlConnection connectSQL = new SqlConnection();

        // Send the connection string.
        connectSQL.ConnectionString = @"Data Source = localhost\SQLEXPRESS;" + 
            "Initial Catalog = Inputs; Integrated Security = SSPI";

        try
        {
            // Setup our command.
            SqlCommand theCommand = new SqlCommand("SELECT * FROM Inputs", connectSQL);

            // Write the stored value in the text boxes.
            connectSQL.Open();

            SqlDataReader theReader;

            theReader = theCommand.ExecuteReader();
            theReader.Read();
            MessageBox(theReader["ApplicationName"].ToString());

            theReader.Close();
            connectSQL.Close();
        }
        catch (Exception ee)
        {
            MessageBox("Oopsie: " + ee);
        }       

 private void MessageBox(string msg)
    {
        Label lbl = new Label();
        lbl.Text = "" + Environment.NewLine + "window.alert('" + msg + "')";
        Page.Controls.Add(lbl);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断肠人 2024-08-09 00:36:56

您基本上只是发送“window.alert('your message');” 作为浏览器的 HTML,这不会作为 JavaScript 执行。 您真的希望它成为“弹出窗口”吗? 如果是这样考虑使用
RegisterStartupScript() 而不是通过标签输出 JS (http://msdn. microsoft.com/en-us/library/asz8zsxy.aspx)。 如果不是,那么为什么不将标签的内容设置为返回消息呢?

You are basically just sending "window.alert('your message');" as HTML to the browser, this wont execute as JavaScript. Do you really want it to be a "popup"? If so consider using
RegisterStartupScript() instead of outputting the JS via a label (http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx). If not then why not just set the contents of the label to your return message?

十二 2024-08-09 00:36:56

样本取自 MSDN & 针对您的示例进行修改

private void MessageBox(string msg)
{
    StringBuilder cstext2 = new StringBuilder();
    cstext2.Append("<script type=\"text/javascript\">");
    cstext2.Append("window.alert('" + msg + "')} </");
    cstext2.Append("script>");
    ClientScript.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
}

您还可以使用 RegisterStartupScript< /a> 而不是 RegisterClientScriptBlock

编辑:或者经典的 ASP 方式也应该可以工作。
我在没有任何编辑的情况下写这篇文章。

Response.Write(@"<script language='javascript'>window.alert('" + msg + "');</script>");

Sample taken from MSDN & modified for your example

private void MessageBox(string msg)
{
    StringBuilder cstext2 = new StringBuilder();
    cstext2.Append("<script type=\"text/javascript\">");
    cstext2.Append("window.alert('" + msg + "')} </");
    cstext2.Append("script>");
    ClientScript.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
}

You could also use RegisterStartupScript instead of RegisterClientScriptBlock.

EDIT: OR the classic ASP way should also work.
I am writing this without any editor.

Response.Write(@"<script language='javascript'>window.alert('" + msg + "');</script>");
不喜欢何必死缠烂打 2024-08-09 00:36:56

它确实执行了。 我在其他代码中使用它,并且该消息框函数在其他项目上运行良好。

这就是我真正想做的事情:

try
        {
            // Setup our command.
            SqlCommand theCommand = new SqlCommand("SELECT * FROM Inputs", connectSQL);

            // Write the stored value in the text boxes.
            connectSQL.Open();

            SqlDataReader theReader;

            theReader = theCommand.ExecuteReader();
            theReader.Read();
            TextBox6.Text = (theReader["ApplicationName"].ToString());

            theReader.Close();
            connectSQL.Close();
        }
        catch (Exception ee)
        {
            MessageBox("Oopsie: " + ee);
        }       

请注意,TextBox6 是网站上的 ASP 文本框。 单击“测试提交”不会显示文本。

It does execute actually. I use this in other code and that messagebox function works fine on other projects.

Here's what I really want to do:

try
        {
            // Setup our command.
            SqlCommand theCommand = new SqlCommand("SELECT * FROM Inputs", connectSQL);

            // Write the stored value in the text boxes.
            connectSQL.Open();

            SqlDataReader theReader;

            theReader = theCommand.ExecuteReader();
            theReader.Read();
            TextBox6.Text = (theReader["ApplicationName"].ToString());

            theReader.Close();
            connectSQL.Close();
        }
        catch (Exception ee)
        {
            MessageBox("Oopsie: " + ee);
        }       

Note that TextBox6 is an ASP text box on the website. Clicking TestSubmit does not show the text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文