单击“提交”按钮后插入评级

时间:2014-02-03 21:26:19

标签: asp.net vb.net

我是VB的新手。因为我试图将我的评级表单插入到基于点击的数据库中。 但是我成功了,我收到了以下错误。

Error   1   'SubmitForm_Click' is not a member of 'ASP.default_aspx'.   C:\Users\raj\Documents\Visual Studio 2013\WebSites\WebSite13\Default.aspx   143

我尝试做的是当我点击提交按钮时,我的星级和文本框的值应该将值存储在Db中。

但是它正在使用out按钮,但我将值存储两次  如

      Rate  text
       4    
       3     hi

我只想保存一次。

这是default.aspx

<%@ Page Title="Home Page" Language="VB"  CodeFile="~/Default.aspx.vb" AutoEventWireup="true" Inherits="_Default"%>
<%@ 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">


<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;
}
.left-column, .right-column{
  float:left;
  padding:10px;
  border:solid 1px black;

}
.left-column{
  width:30%; 

  border:solid 1px black;
}
.right-column{
  width:60%;

  border:solid 1px black; 
}

</style>
</head>
<body>
<form id="form1" runat="server">
<ajax:ToolkitScriptManager ID="ScripManager1" runat="server"/>

<asp:UpdatePanel ID="pnlRating" runat="server">
<ContentTemplate>

        <div class="left-column">
  <div>
    <b>test1</b><br />
    <div style="padding-left:5em">
<ajax:Rating ID="ratingControl" AutoPostBack="true" OnChanged="RatingControlChanged" runat="server" StarCssClass="ratingEmpty" WaitingStarCssClass="ratingSaved" EmptyStarCssClass="ratingEmpty" FilledStarCssClass="ratingFilled" MaxRating="10">
</ajax:Rating>
<b> <asp:label ID="lbltxt" runat="server"/> </b><br />
        </div>

  </div>



</div>
<div class="right-column">

     <div><b>test 2</b><br />
    <div style="padding-left:5em">

<b> <asp:TextBox id="tb1"  autopostback="true" runat="server" /> </b><br />
        </div></div>

     <p>
        &nbsp;</p>
    <p>



    <asp:Button  id="SubmitForm" onclick="SubmitForm_Click" text="Submit & logout" runat="server" />

    </p>

</div>



</ContentTemplate>
</asp:UpdatePanel>



</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) Handles SubmitForm.Click
        con.Open()

        Dim cmd As New SqlCommand("insert into rating(Rate,course)values(@Rate,@course)", con)
        cmd.Parameters.AddWithValue("@Rate", ratingControl.CurrentRating)

        cmd.Parameters.AddWithValue("@course", tb1.Text)
        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 :(得分:1)

您没有SubmitForm_Click的事件处理程序。您可以更改它,以便您不会尝试调用处理程序...

<asp:Button  id="SubmitForm" text="Submit & logout" runat="server" />

或者创建事件处理程序......但我对VB不够熟悉,无法为您提供正确的语法。