Asp.net 将FormView值指定给VB变量

Asp.net 将FormView值指定给VB变量,asp.net,vb.net,email,variables,Asp.net,Vb.net,Email,Variables,我正在尝试将FormView值指定给VB变量 当first_nameTextBox是一个文本框,用户直接输入数据时,我使用的代码运行良好,但当first_nameTextBox是一个通过FormView从数据库填充的标签时,代码运行失败 我收到的错误是BC30451:“FormView2\u first\u nameTextBox”未声明。由于其保护级别,可能无法访问 非常感谢您的帮助 代码如下 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr

我正在尝试将FormView值指定给VB变量

当first_nameTextBox是一个文本框,用户直接输入数据时,我使用的代码运行良好,但当first_nameTextBox是一个通过FormView从数据库填充的标签时,代码运行失败

我收到的错误是BC30451:“FormView2\u first\u nameTextBox”未声明。由于其保护级别,可能无法访问

非常感谢您的帮助

代码如下

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Text" %>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" dir="ltr">
<head runat="server">
<!-- Scripting area here -->
<script runat="server" type="text/vbscript"> 
Protected Sub btnSubmit_click (ByVal sender As Object, ByVal e As EventArgs)
    If IsPostBack Then
            Dim sc As SmtpClient = New SmtpClient("relay.hostinguk.net")
            Dim sb As StringBuilder = New StringBuilder()
            Dim msg As MailMessage = Nothing
            sb.Append("Name : " + FormView2_first_nameTextBox.Text + vbCrLf)
            Try
                msg = New MailMessage("from@company.com", _
                    "to@company.com", "Contact details for Co", _
                    sb.ToString())
                sc.Send(msg)
            Catch ex As Exception
                ' something bad happened
                Response.Write("Something bad happened! - please try again")
            Finally
            Multiview1.SetActiveView(ViewConfirmation)
                If Not msg Is Nothing Then msg.Dispose()        
            End Try
    End If
    End Sub
Protected Sub Page_Load (ByVal sender As Object, ByVal e As EventArgs)
    If Not IsPostBack Then
    Try
    Multiview1.SetActiveView(ViewForm)
    Catch ex As Exception
                ' something bad happened
                Response.Write("Something bad happened! - please try again")
    End Try
    End If
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:MultiView runat="server" id="MultiView1">
    <asp:View runat="server" id="ViewForm"> 
            <asp:AccessDataSource ID="AccessDataSource2bd" runat="server" DataFile="../app_data/bw_data.mdb" SelectCommand="SELECT * FROM student WHERE student_ID = 92">
            </asp:AccessDataSource>
<asp:FormView runat="server" id="FormView2" DataSourceID="AccessDataSource2bd" DataKeyNames="student_ID" DefaultMode="ReadOnly">
        <ItemTemplate >
        <asp:Label id="first_nameTextBox" runat="server" Text='<%# Eval("first_name") %>' />,
        </ItemTemplate >
</asp:FormView>         
<asp:Table runat="server" id="Table2">
    <asp:TableRow runat="server">
        <asp:TableCell runat="server" HorizontalAlign="Left" height="20px">
        <asp:Button id="UpdateButton" runat="server" CommandName="Update" onclick="btnSubmit_click" Text="Send Email"/>
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>
</asp:View>
<asp:View runat="server" id="ViewConfirmation">
        <p>An email has been sent, and copied to you, confirming all current contact details.</p>
</asp:View>
<p></p>
</asp:MultiView>
</form>
</body>
</html>
谢谢 Ant

试试这个:

dim tb as TextBox = FormView2.Row.FindControl("first_nameTextBox")
sb.Append("Name : " + tb.Text + vbCrLf)

因为它在ItemTemplate中,first_nameTextBox可能有许多实例,所以要访问它,您需要使用FindControl。@Arindamayak谢谢-我试图添加FindControl,但没有使用我的代码?表单显示时填充了first_nameTextBox,但在提交时获得System.NullReferenceException:对象引用未设置为对象的实例。这是我添加的,以包括findcontrol:dim tb as TextBox=findcontrol FormView2_first_Name TextBox dim sc as SmtpClient=New SmtpClientrelay.hostinguk.net dim sb as StringBuilder=New StringBuilder dim msg as MailMessage=Nothing sb.Append Name:+tb.Text+vbCrLfI刚才说,您不能使用FormView2\u first\u nameTextBox,而是必须使用first\u nameTextBox,我认为您必须循环使用MultiView1控件和FindControl。注意:将有问题的尝试过的代码作为更新。我在第页上尝试了“code”私有子GetControlValue“Find”控件。Dim myControl1 As Control=FindControlMe.first\u nameTextBox如果不是myControl1,则myControl1为Nothing'获取控件的父级。Dim myControl2 As Control=myControl1.Parent Response.WriteParent文本框为:&myControl2.ID Else Response.WriteControl未找到。。。。。End If End Sub,但它总是以“not found”结尾-因此我没有得到正确的语法-有什么建议吗?