简体   繁体   中英

Translate a word using SQL Server database in VB.NET and ajax

Can anyone guide me how to replace a word from SQL Server using VB.NET and ajax with clicking a button?

I tried this backend code:

Using da As New SqlDataAdapter
    con.Open()
    cmd.CommandText = "SELECT value_en as value FROM tbl_language WHERE element_id = 'a1';"
    da.SelectCommand = cmd
    Dim dt As New DataTable
    da.Fill(dt)
    Dim WordValue As String = dt.Rows(0).Item(0)
End Using

and in front end to display the word:

<%# Eval("WordValue")%>

My ajax:

$.ajax({
        type: "POST",
        cache: false,
        url: "page.aspx/functionname",
        data: {},
        success: alert("success"),
        error: alert("error")
    });

Buttons to translate:

 <form runat="server">
  <asp:RadioButtonList ID="change_language" runat="server"  AutoPostBack="True" 
        onselectedindexchanged="change_language_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="english">English</asp:ListItem> 
<asp:ListItem Value="malay">Malay</asp:ListItem>
        </asp:RadioButtonList>
 </form>

On button click you can call the function "GetLabelText". On success callback of jquery ajax post method, you can set the label value using the result of a web method.

<script type = "text/javascript">
    function GetLabelText() {
        $.ajax({
            type: "POST",
            url: "page.aspx/functionname",
            data: {},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function(response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        $("#label").val(response.d);
    }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM