无法使用 SQL 数据激活消息框 - ASP.NET
我正在测试我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您基本上只是发送“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?
样本取自 MSDN & 针对您的示例进行修改
您还可以使用 RegisterStartupScript< /a> 而不是 RegisterClientScriptBlock 。
编辑:或者经典的 ASP 方式也应该可以工作。
我在没有任何编辑的情况下写这篇文章。
Sample taken from MSDN & modified for your example
You could also use RegisterStartupScript instead of RegisterClientScriptBlock.
EDIT: OR the classic ASP way should also work.
I am writing this without any editor.
它确实执行了。 我在其他代码中使用它,并且该消息框函数在其他项目上运行良好。
这就是我真正想做的事情:
请注意,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:
Note that TextBox6 is an ASP text box on the website. Clicking TestSubmit does not show the text.