【问题标题】:How to display a message box with yes or no in asp net webforms using vb.net?如何使用 vb.net 在 asp net webforms 中显示是或否的消息框?
【发布时间】:2017-03-16 03:28:35
【问题描述】:

在向表中插入记录之前,系统会首先检查该人的名字和姓氏是否已经存在。如果此人存在,则会出现一个消息框,其中包含是或否按钮,询问用户是否要继续插入记录。我尝试了以下代码:

Imports ChurchMIS.Data
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Linq
Imports System.Text.RegularExpressions
Imports System.Web
Imports System.Web.Security
Imports System.Data.SqlClient
Namespace ChurchMIS.Rules

Partial Public Class IndividualBusinessRules
    Inherits ChurchMIS.Data.BusinessRules

    ''' <summary>
    ''' This method will execute in any view for an action
    ''' with a command name that matches "SQL".
    ''' </summary>
    <Rule("r107")>  _
    Public Sub r107Implementation( _
                ByVal individualId As Nullable(Of Integer),  _
                ByVal firstName As String,  _
                ByVal lastName As String,  _
                ByVal dateOfBirth As Nullable(Of DateTime),  _
                ByVal addressTypeId As Nullable(Of Integer),  _
                ByVal suburb As String,  _
                ByVal streetAddress As String,  _
                ByVal postCode As Nullable(Of Integer),  _
                ByVal contactInfoTypeId As Nullable(Of Integer),  _
                ByVal contactNo As String,  _
                ByVal fullName As String,  _
                ByVal individualTypeId As Nullable(Of Integer),  _
                ByVal state As String,  _
                ByVal dateOfBaptism As Nullable(Of DateTime),  _
                ByVal dateOfTransfer As Nullable(Of DateTime),  _
                ByVal email As String,  _
                ByVal faithStatus As Nullable(Of Integer),  _
                ByVal noOfVisits As Nullable(Of Integer),  _
                ByVal name As String,  _
                ByVal name_1 As String,  _
                ByVal name_2 As String)
        'This is the placeholder for method implementation.
        Dim con As SqlConnection = New SqlConnection("Data     Source=CNEPHILS;Initial Catalog=ChurchMIS;User ID=sa;Password=Cn$phil$")

        Dim theQuery As String = "SELECT * FROM Individual WHERE  FirstName=@FirstName AND LastName=@LastName"
        Dim cmd1 As SqlCommand = New SqlCommand(theQuery, con)
        cmd1.Parameters.AddWithValue("@FirstName", firstName)
        cmd1.Parameters.AddWithValue("@LastName", lastName)

        Using reader As SqlDataReader = cmd1.ExecuteReader()
            If reader.HasRows Then
                ' person already exists
                    Dim result As Integer=

                Dim result As Integer = MessageBox.Show("message",  "caption", MessageBoxButtons.YesNoCancel)
                If result = DialogResult.Cancel Then
                    MessageBox.Show("Cancel pressed")
                ElseIf result = DialogResult.No Then
                    MessageBox.Show("No pressed")
                ElseIf result = DialogResult.Yes Then
                    Dim cmd As SqlCommand = New SqlCommand("exec  spInsertIndividual @FirstName,@LastName,@DateOfBirth,@Suburb,@StreetAddress,@PostCode,@State,@AddressTypeId,@ContactInfoTypeId,@ContactNo,@IndividualTypeId,@Email,@FaithStatus,@DateOfBaptism,@DateOfTransfer", con)
                    cmd.ExecuteNonQuery()
                    MsgBox("Records Successfully Added!", MsgBoxStyle.Information)
                End If
            Else
                ' person does not exist, add them
                Dim cmd As SqlCommand = New SqlCommand("exec spInsertIndividual @FirstName,@LastName,@DateOfBirth,@Suburb,@StreetAddress,@PostCode,@State,@AddressTypeId,@ContactInfoTypeId,@ContactNo,@IndividualTypeId,@Email,@FaithStatus,@DateOfBaptism,@DateOfTransfer", con)
                cmd.ExecuteNonQuery()
                MsgBox("Records Successfully Added!", MsgBoxStyle.Information)
            End If
        End Using

        con.Close()

    End Sub
End Class
End Namespace

但是,我提出了一个错误“消息框未声明.....保护级别。”

希望有人可以提供帮助。谢谢!

【问题讨论】:

  • 在 asp.net 中你在项目中使用 ado.net 或 linq

标签: asp.net sql-server vb.net


【解决方案1】:

@罗杰。我认为您正在使用 Web 应用程序。试试这个,在你的 .aspx 页面的 head 标签内添加这个脚本。

<script type="text/javascript">
        function SomeMethod() {
            try {
                var result = true;
                var obj = {};
                obj.Firstname = $('#<%=txtfirstname.ClientID %>').val();
                obj.Lastname = $('#<%=txtlastname.ClientID %>').val();
                $.ajax({
                    type: "POST",
                    data: JSON.stringify(obj),
                    url: "yourformname.aspx/yourmethodname",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async:false,
                    success: function (response) {
                        if (response.d.toString() == "true") {
                                if (confirm("first name and last name of the person is already exists?")) {
                                    result = true;
                                    // insert the user name
                                }
                                else {
                                    result = false;

                                }
                        }
                        else {

                        }
                    },
                    failure: function (response) {
                        alert(response.d);
                    }
                });
            }
            catch (e) {
                alert(e);
            }
            return result;
        }
    </script>

在您的按钮单击事件中调用 javascript 函数。如果您使用 RequiredFieldValidator 进行验证,请使用 Page_ClientValidate() 否则在按钮 onclick 事件中删除 Page_ClientValidate() 函数。

<asp:Button ID="btnbutton" CssClass="Designclass" OnClientClick="if(Page_ClientValidate()) return SomeMethod();"
                                        runat="server" Text="Generate" />

在您的 .aspx.vb 页面中创建以下 Web 方法

 <System.Web.Services.WebMethod()>
Public Shared Function Checkuserexists(ByVal Firstname As String, ByVal Lastname As String) As String
    'write your code for checking firstname and last name
     If exists > 0 Then
        Return true
    Else
        Return False
    End If
End Function

通过使用 Checkuserexists 方法检查名字和姓氏是否存在于数据库中。如果名字和姓氏存在,则返回 true 并要求确认消息。如果单击是,则将值插入数据库。

【讨论】:

  • @rogerbulawan 很乐意为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 2021-03-15
  • 2021-08-25
  • 1970-01-01
相关资源
最近更新 更多