标题听起来很简单,但具体细节如下。
我得到了一个旧网站来扩展一点。它是一个 VB.NET 实现,也是原始编码人员的第一个站点 - 因此很多人都在努力让它工作。
网站摘要:
母版页设计,带有oAuth登录页面。主页是记录的分页,并且根据用户是否登录,显示单独的按钮进行编辑。到目前为止 - 没什么大不了的,它有效,一切都很好。
现在,当我们深入编辑页面时,产品所有者希望能够向记录添加注释。看起来很简单,除了实际的插入之外,所有其他 CRUD 都已完成。
我在页面底部添加了一个文本区域和一个按钮。插入事件正在运行,并且虚拟记录正在被放入数据库中。
但是当我尝试获取文本区域的值并将其放入查询中时,失败就出现了。
<html xmlns="http://www.w3.org/1999/xhtml">
<script language="VB" runat="server" >
Protected Sub NewNoteButton_Click(sender As Object, e As System.EventArgs)
' Dim myStringResult As String = NewNoteTextArea.InnerHtml
MsgBox("Here1")
Dim constr As String = ConfigurationManager.ConnectionStrings("XXXXXConnectionString").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("INSERT INTO EasementNote ([RE_Number], [noteuser], [note]) VALUES (@RE_Number, @NoteUser, @Note)")
cmd.Parameters.AddWithValue("@RE_Number", "test")
cmd.Parameters.AddWithValue("@NoteUser", "test")
cmd.Parameters.AddWithValue("@Note", "more testing")
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
MsgBox("here2")
End Sub
</script>
<head runat="server">
<title></title>
</head>
这是我的 HTML 代码
<asp:TableFooterRow Width="100%">
<asp:TableCell HorizontalAlign="center" ColumnSpan="2">
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" />
</asp:TableCell>
<asp:TableCell HorizontalAlign="center" ColumnSpan="1">
<asp:TextBox id="NewNoteTextArea" TextMode="multiline" Columns="40" Rows="3" runat="server" />
<asp:Button runat="server" ID="NewNoteButton" Text="Insert Note" OnClick="NewNoteButton_Click"/>
</asp:TableCell>
</asp:TableFooterRow>
当我尝试获取文本区域的值时,我收到了
我真的很困惑为什么会收到此错误消息。顺便说一句 - 页面后面没有代码...不知道为什么,但没有,但是当我添加一个代码并将按钮单击事件移动到它时,按钮单击无法解析。
感谢您的浏览。
最佳答案
由于您的控件位于另一个服务器控件内,因此您需要使用 FindControl 来访问代码中的控件,例如
Dim txtNote As TextBox = DirectCast(Me.FormView1.FindControl("NewNoteTextArea"), TextBox)
关于asp.net - 将值插入sql server : control not declared,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33219249/