【问题标题】:Inserting value into sql server: control not declared将值插入 sql server:未声明控件
【发布时间】:2015-10-19 16:16:00
【问题描述】:

标题听起来很简单,但这里是细节。

我得到了一个旧网站来扩展一点。这是一个 VB.NET 实现,它是原始编码器的第一个站点 - 很多人都在努力让它工作。

网站摘要:
母版页面设计,带有 oAuth 登录页面。主页是记录的分页,根据用户是否登录,显示一个单独的按钮进行编辑。到目前为止 - 没什么大不了的,它有效,一切都很好。

现在,当我们深入研究编辑页面时,产品负责人希望能够向记录添加注释。看起来很简单,除了实际插入之外,其他所有的 CRUD 都完成了。

我在页面底部添加了一个文本区域和一个按钮。插入事件正在运行,并且正在将虚拟记录放入数据库中。

但是当我尝试获取 textarea 的值并将其放入查询时,失败就起作用了。

<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" />&nbsp;
      <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" />&nbsp;&nbsp;&nbsp;
      <asp:Button runat="server" ID="NewNoteButton" Text="Insert Note" OnClick="NewNoteButton_Click"/>
    </asp:TableCell>
  </asp:TableFooterRow>

当我尝试获取 textarea 的值时,我收到了一个

我真的很困惑为什么会收到此错误消息。顺便说一句 - 页面后面没有代码......不知道为什么,但没有,但是当我添加一个并将按钮单击事件移至它时,按钮单击无法解析。

感谢收看。

【问题讨论】:

  • 在服务器控件中不应该被 FindControl 引用,例如在这种情况下 forums.asp.net/post/4479665.aspx
  • FindControl 也因“未声明”错误而失败。
  • 查看您的代码和 cmets,您的 TextBox 似乎位于 Table(服务器控件)中,而 Table(服务器控件)又位于 FormView 中。如果是这样,那么您将必须使用 FindControl 遵循相同的层次结构,例如 FormView > Table > FooterRow > Cell > TextBox,如上面给出的 asp.net 论坛链接(检查多个 FindControl)
  • haraman - 请张贴作为答案,以便您获得积分。这是有效的... Dim TextBoxText As TextBox = DirectCast(Me.FormView1.FindControl("NewNoteTextArea"), TextBox)

标签: asp.net vb.net


【解决方案1】:

由于您的控件位于另一个服务器控件中,因此您需要使用 FindControl 在代码中访问它,例如

Dim txtNote As TextBox = DirectCast(Me.FormView1.FindControl("NewNoteTextArea"), TextBox) 

【讨论】:

    【解决方案2】:

    您应该使用文本框控件的 Text 属性值,而不是 InnerHtml

    Dime mySyting as String = NewNoteTextArea.Text
    

    【讨论】:

    • @pithhelmet 你的文本框在你的表单标签内吗?此代码是否也在同一页面的代码隐藏文件中?
    • 它隐藏在 asp:FormView 中,所有真正的“代码”都在同一页面上,是的,所有模板都位于 FORM 元素中,但 sub 位于 form 元素之外
    • @pithhelmet 在这种情况下,您必须阅读此 SO 帖子 stackoverflow.com/a/4476186/5104101
    猜你喜欢
    • 2011-08-17
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 2012-03-13
    相关资源
    最近更新 更多