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

C# 无法使用SQL数据激活消息框-ASP.NET,c#,asp.net,sql,ado.net,populate,C#,Asp.net,Sql,Ado.net,Populate,我正在测试我对ADO.NET和SQL的知识,目前正在尝试做一些基础工作。我想从表中读取数据,当用户单击按钮时,会弹出一个消息框,其中包含ApplicationName列中的值 当我点击按钮时,它现在什么都不做。。。有什么想法吗 protected void TestSubmit_ServerClick(object sender, EventArgs e) { // Initialize the database connector. SqlConnect

我正在测试我对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); }
您基本上只是发送窗口。提醒“您的消息”;作为浏览器的HTML,它不会作为JavaScript执行。你真的想让它成为一个弹出窗口吗?如果这样考虑使用
RegisterStartupScript,而不是通过标签输出JS。如果没有,那么为什么不将标签的内容设置为您的返回消息?

示例取自MSDN&modified供您参考

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);
}
您也可以使用而不是

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

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

它确实执行。我在其他代码中使用了这个函数,messagebox函数在其他项目中运行良好

以下是我真正想做的:

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文本框。单击TestSubmit不会显示文本。

right-您的实现的问题是您正在传输一段Javascript,但周围没有标记。显然,这不会执行脚本。在WinForms中,在回发服务器端执行此操作的正确方法是使用RegisterStartupScript或RegisterClientScriptDOH注册脚本!!!它没有复制到。。。。它在那里,但StackOverflow不会让它复制过来。。。