Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 对象引用未设置为对象的实例。-Visual Basic(Web)_Asp.net_Vb.net - Fatal编程技术网

Asp.net 对象引用未设置为对象的实例。-Visual Basic(Web)

Asp.net 对象引用未设置为对象的实例。-Visual Basic(Web),asp.net,vb.net,Asp.net,Vb.net,所以,我的代码有点小问题 我当时正在做一个学校的小项目(图书图书馆的改造),遇到了一个我无法理解的问题 因此,用户可以搜索书籍列表,在填充列表后(通过DataList控件),会出现一个小按钮“Book now”(保留),用户可以单击该按钮并保留其书籍 这是驻留在“Search.aspx”中的DataList控件的代码 我试图做到以下几点:;单击“rezervacijeButton”按钮后,将运行一个函数“rezervacijaClick”,该函数将填充数据库中的表 Protected Sub r

所以,我的代码有点小问题

我当时正在做一个学校的小项目(图书图书馆的改造),遇到了一个我无法理解的问题

因此,用户可以搜索书籍列表,在填充列表后(通过DataList控件),会出现一个小按钮“Book now”(保留),用户可以单击该按钮并保留其书籍

这是驻留在“Search.aspx”中的DataList控件的代码

我试图做到以下几点:;单击“rezervacijeButton”按钮后,将运行一个函数“rezervacijaClick”,该函数将填充数据库中的表

Protected Sub rezervacijaClick(sender As Object, e As System.EventArgs)
            Dim Conn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("LocalSqlServer").ToString)
            Dim cmd As New System.Data.SqlClient.SqlCommand
            Dim sql As New StringBuilder
            Dim rezstatus As Label = DataList1.FindControl("rezStatusLabel")

            sql.Append("INSERT INTO rezervacije(UserName, knjigaId) VALUES (@UserName, @knjigaId)")

            Dim buttonsender As Button = sender
            cmd.Parameters.AddWithValue("UserName", User.Identity.Name)
            cmd.Parameters.AddWithValue("knjigaId", buttonsender.CommandArgument)

            Conn.Open()
            cmd.CommandText = sql.ToString
            cmd.Connection = Conn
            cmd.ExecuteNonQuery()
            Conn.Close()

            buttonsender.Visible = False
            rezstatus.Text = "aaa"

            'Try
            '    rezstatus.Text = "testing..."
            'Catch ex As Exception
            '    exlabel.Text = "POGREŠKA"
            '    exlabel.ForeColor = Drawing.Color.Red
            'End 
        End Sub
在函数“rezervacijaClick”中,我想做的下一件事是在单击“Reserve”按钮后,将标签(ID为“rezStatusLabel”,位于DataList1控件内)的文本值设置为“SOME text”

但点击按钮后,我得到以下错误:

 Object reference not set to an instance of an object. 
Line 21: 
Line 22:         buttonsender.Visible = False
Line 23:         rezstatus.Text = "aaa"
Line 24: 
Line 25:         'Try

您的
rezstatus
对象是
Nothing
null

发生这种情况是因为您没有在正确的位置查找标签

绑定到
数据列表
的每个数据记录将创建一个新的控件层次结构,这些容器包含您在
项目模板
中定义的其他控件

DataList1
的直接后代将是
DataListItem
对象的集合,然后将控件放在这些对象中

由于我们不确定(除非您知道您只将一条记录绑定到
数据列表
)所需标签将位于哪个
数据列表项
,因此我们只需沿着控制树向后走,然后从那里找到标签

由于您正在响应
rezervacijaClick
方法中的按钮单击事件,因此参数
sender
的类型将为
button
,并且将来自
rezervacijeButton
,因此我们可以使用该信息查找您的标签:

Dim clickedButton As Button = CType(sender, Button) 'convert the sender parameter to the correct type: Button
Dim buttonParent As Control = clickedButton.Parent 'get a reference to the button's parent control
Dim rezstatus As Label = CType(buttonParent.FindControl(""), Label) 'find the label by ID and convert it to a Label as the return type of FindControl is Control

我建议您使用事件,而不是使用按钮单击事件。这会让你的生活轻松很多。每当在DataList控件的行中单击按钮时,此事件将激发

这样,您就可以通过
DataListCommandEventArgs
参数获得传入的索引。然后,您只需要更新DataList标记以添加事件处理程序:

<asp:DataList ID="DataList1" runat="server" DataKeyField="knjigaId" 
    DataSourceID="SearchSqlDataSource" CssClass="searchControl"
    ItemCommand="DataList1_ItemCommand" >

很好地解释了OPs对该控件结构的误解。谢谢,我通过艰苦的学习了解到了这一点,有时会让人困惑。很好,我甚至不知道我可以追溯到父控件:/@valaya我养成了一个习惯,这对解决问题非常有帮助,是打开我在MSDN上使用的特定类的文档页面,并向下滚动属性、方法和事件列表,以了解我可用的内容。@Sean Yea,我正试图这样做,但我必须承认我在搜索时有点迷路,特别是因为我一周前开始使用ASP。
Dim clickedButton As Button = CType(sender, Button) 'convert the sender parameter to the correct type: Button
Dim buttonParent As Control = clickedButton.Parent 'get a reference to the button's parent control
Dim rezstatus As Label = CType(buttonParent.FindControl(""), Label) 'find the label by ID and convert it to a Label as the return type of FindControl is Control
<asp:DataList ID="DataList1" runat="server" DataKeyField="knjigaId" 
    DataSourceID="SearchSqlDataSource" CssClass="searchControl"
    ItemCommand="DataList1_ItemCommand" >
Protected Sub DataList1_ItemCommand(sender As Object, e As System.EventArgs)
    Dim Conn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("LocalSqlServer").ToString)
    Dim cmd As New System.Data.SqlClient.SqlCommand
    Dim sql As New StringBuilder

    Dim rezstatus As Label = e.Item.FindControl("rezStatusLabel")

    sql.Append("INSERT INTO rezervacije(UserName, knjigaId) VALUES (@UserName, @knjigaId)")

    Dim buttonsender As Button = e.Item.FindControl("rezervacijeButton")
    cmd.Parameters.AddWithValue("UserName", User.Identity.Name)
    cmd.Parameters.AddWithValue("knjigaId", buttonsender.CommandArgument)

    Conn.Open()
    cmd.CommandText = sql.ToString
    cmd.Connection = Conn
    cmd.ExecuteNonQuery()
    Conn.Close()

    buttonsender.Visible = False
    rezstatus.Text = "aaa"
End Sub