在访问数据库中找不到dataitem时,为消息框调用JavaScript [英] calling a javascript for a messagebox when dataitem is not found in an acess database

查看:94
本文介绍了在访问数据库中找不到dataitem时,为消息框调用JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

asp.net 2.0中的该程序是西班牙语/英语词典.
它由两个文本框,一个按钮和一个数据列表组成.数据列表绑定到由两列组成的acces数据库.一种西班牙语和一种英语.当用户在textbox1中输入西班牙语单词并单击按钮时,该单词的含义将出现在textbox2中.现在,有时用户输入不在数据库中的单词.在这种情况下,我希望弹出一个javascript消息框,提示该单词未列出或拼写错误".
我尝试了几种JavaScript,以在后面的代码中弹出提示,但这没有用.我知道应该包括一个客户端,但我无法做到这一点.
下面的子代码是我用来提取单词的子代码,只要我输入访问数据库中已经存在的单词,它就可以完美地工作.数据库中的行中的单词由连字符分隔,因此我使用的拆分功能也可以正常工作.

This program in asp.net 2.0 is a Spanish/English Dictionary.
It consists of two textboxes,one button and a datalist. The datalist is bound to an acces database consisting of two columns; One Spanish and one English. When a user enters a Spanish word in textbox1 and clicks the button, the meaning of the word appears in textbox2. Now sometimes a user enters a word that is not in the database. I want, in this case, to have a javascript messagebox to popup saying ''Either the word is not listed or misspelled''.
I tried several javascripts to popup in code behind but that did not work. I know that a clientside should be included but I am not able to do that.
The sub below is the one I am using to fetch the words and it is working perfect as long as I enter a word that is already in access database. The words in the rows in database are separated by hyphens so I am using a Split fuction which is also working perfectly.

Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or _
     e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim ds As SqlDataSource
            ds = CType(e.Item.FindControl("Spanish"), _
            SqlDataSource)
            Dim Words() As String
            Dim arySpanish() As String
            Dim strSpanish As String
            Dim i As Integer
            Dim strScript As String = DataBinder.Eval(e.Item.DataItem, _
                "English").ToString()


下面的7行是我尝试获取js消息框的地方,但是它不起作用


The below 7 lines is where I am trying to get the js messagebox but it is not working

TextBox1.Text = strScript
            If TextBox1.Text <> strScript Then
                ClientScript.RegisterClientScriptBlock(Me.GetType(), "ClientScript", "alert (''wrong'')", True)
                Dim streng As String = "<script language=''JavaScript''> alert(''wrong'') </script>"
            End If
            strSpanish = DataBinder.Eval(e.Item.DataItem, _
    "spanish").ToString()
            Words = strSpanish.Split("-")
            arySpanish = strSpanish.Split("-")
            strSpanish = ""
            For i = 0 To UBound(arySpanish)
                strSpanish = strSpanish & arySpanish(i) & Environment.NewLine
                TextBox2.Text = strSpanish
            Next
            End If
    End Sub

推荐答案

除非您将其写到客户端,否则使用js创建字符串是毫无用处的.您的脚本是否已写入最后一页?页面上有js错误吗?为什么要在执行回发时弹出警报,为什么不在页面中仅显示错误字段?
Creating a string with your js in it is worthless unless you write it out to the client. Is your script being written in to the final page ? Are there any js errors on the page ? Why pop up an alert when you''e done a postback, why not just have an error field in the page ?


感谢您的回答
我在页面中有一个错误字段:
如果textbox1.text =''dataitem''然后
msgbox(右")
其他
msgbox(错误")
如果
结束 当单词在数据库中时,显示msgbox"right"

但是当单词不在数据库中时,不会显示msgbox错误"
thank you for your answer
I have an error field in the page:
if textbox1.text=''dataitem'' then
msgbox("Right")
else
msgbox("wrong")
end if
when the word is in the database msgbox "right" is displayed

but when the word is not in the database msgbox "wrong" is not displayed


这篇关于在访问数据库中找不到dataitem时,为消息框调用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆