'RatingControlChanged'不是'ASP.default_aspx'的成员。

时间:2014-02-02 22:37:19

标签: asp.net vb.net

net使用VB。我按照这个tutorial来做我的评级控制器

但是我收到以下错误

Error   1   'RatingControlChanged' is not a member of 'ASP.default_aspx'.   C:\Users\raj\Documents\Visual Studio 2013\WebSites\WebSite13\Default.aspx   46  
Error   2   'ratingControl' is not declared. It may be inaccessible due to its protection level.    C:\Users\raj\Documents\Visual Studio 2013\WebSites\WebSite13\Default.aspx.vb    16  48  WebSite13
Error   3   'ratingControl' is not declared. It may be inaccessible due to its protection level.    C:\Users\raj\Documents\Visual Studio 2013\WebSites\WebSite13\Default.aspx.vb    33  13  WebSite13
Error   4   'lbltxt' is not declared. It may be inaccessible due to its protection level.   C:\Users\raj\Documents\Visual Studio 2013\WebSites\WebSite13\Default.aspx.vb    34  13  WebSite13

我不知道哪一个导致了错误My DB name is Test and Table name is ratings

这是我的default.aspx

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    Protected Sub Page_Load(sender As Object, e As EventArgs)

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ajax Rating Sample</title>
<style type="text/css">
.ratingEmpty
{
background-image: url(ratingStarEmpty.gif);
width:18px;
height:18px;
}
.ratingFilled
{
background-image: url(ratingStarFilled.gif);
width:18px;
height:18px;
}
.ratingSaved
{
 background-image: url(ratingStarSaved.gif);
width:18px;
height:18px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<ajax:ToolkitScriptManager ID="ScripManager1" runat="server"/>
<div>
<asp:UpdatePanel ID="pnlRating" runat="server">
<ContentTemplate>
<table style="width:35%">
<tr>
<td style="width:20%">
<b>Average Rating:</b>
</td>
<td>
<ajax:Rating ID="ratingControl" AutoPostBack="true" OnChanged="RatingControlChanged" runat="server" StarCssClass="ratingEmpty" WaitingStarCssClass="ratingSaved" EmptyStarCssClass="ratingEmpty" FilledStarCssClass="ratingFilled">
</ajax:Rating>
<b> <asp:label ID="lbltxt" runat="server"/> </b>
</td>
</tr>
<tr>
<td colspan="2">
Testing
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

这是default.aspx.vb代码

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page
    Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("test").ConnectionString)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindRatingControl()
        End If
    End Sub
    Protected Sub RatingControlChanged(ByVal sender As Object, ByVal e As AjaxControlToolkit.RatingEventArgs)
        con.Open()
        Dim cmd As New SqlCommand("insert into rating(rate)values(@Rating)", con)
        cmd.Parameters.AddWithValue("@Rating", ratingControl.CurrentRating)
        cmd.ExecuteNonQuery()
        con.Close()
        BindRatingControl()
    End Sub
    Protected Sub BindRatingControl()
        Dim total As Integer = 0
        Dim dt As New DataTable()
        con.Open()
        Dim cmd As New SqlCommand("Select Rate from rating", con)
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(dt)
        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
                total += Convert.ToInt32(dt.Rows(i)(0).ToString())
            Next
            Dim average As Integer = total \ (dt.Rows.Count)
            ratingControl.CurrentRating = average
            lbltxt.Text = dt.Rows.Count & "user(s) have rated this article"
        End If
    End Sub
End Class

所以可以帮助我如何解决这个问题。

1 个答案:

答案 0 :(得分:3)

您的default.aspx页面(前端)似乎缺少所需的页面声明 - 这就是告诉它使用哪个代码隐藏的原因。

它应该位于default.aspx文件的最顶部。

示例默认页面声明:

<%@ Page Title="Home Page" Language="VB.net" MasterPageFile="~/Site.Master" 
AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebTest._Default" %>

重要的部分是:

CodeBehind="Default.aspx.cs" 
Inherits="WebTest._Default"

这告诉ASP.net引擎将哪个文件和类名与页面关联。

您确定没有意外删除该行并保存文件吗?

这应该很容易恢复,只需从其他文件复制并修复CodeBehindInherits(以及Title)属性即可获得正确的值。