如何在 vb.net 代码中编写 java 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14229071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:50:10  来源:igfitidea点击:

How to write java script in vb.net code?

javascriptjqueryvb.net

提问by pavan

Can anyone help me out as I need to write a Javascript in vb.net code? what i meant is i am new to vb.net coding, I have a dynamically created table from a web-service, and for that table i need to add sorting function which is in the javascript code, i need to add this script to the dynamically created table I tried with this:

任何人都可以帮助我,因为我需要在 vb.net 代码中编写 Javascript?我的意思是我是 vb.net 编码的新手,我有一个从 Web 服务动态创建的表,对于该表,我需要添加 javascript 代码中的排序功能,我需要将此脚本添加到动态创建的表我试过这个:

     oSB.Append("table.RegisterStartupScript('SCRIPTNAME', '<script language='javascript'>function tname(){alert('kk')};</script>')")

but it seems to be not working atall.

但它似乎根本不起作用。

Also i tried with this

我也试过这个

     'oSB.Append("<script>$('[id^=tname] th').live('click',function(event){ alert('hello')}</script>")

my ajax function:

我的 ajax 函数:

     $.ajax({
        type: "POST",
        url: "Service1.asmx/GetRecipie",
        contentType: "application/json; charset=utf-8",

        data: "{'sDB':'" + sDB + "'}",
        dataType: "json",
        success: OnGetMemberSuccess,

        failure: function (errMsg) {
            $('#errorMessage').text(errMsg);  //errorMessage is id of the  div
        }

    });
    function OnGetMemberSuccess(data, status) {

        xistr = data.d.split(',');
        $("#MemberDetails").html(data.d);
        $('input[type=button]').attr('disabled', false);
    }

} 

Table is created by this code(webservice):

表是由这个代码(webservice)创建的:

     oSB.Append("<table id= '" + table_id + "' class='sortable' ><thead><tr><th class=border id='tname' >" + "Name" + "<img src='next.gif'/></th><th class=border>" + "Duration" + "</th><th class=border>" + "State" + "</th><th class=border>" + "Party" + "</th><th class=border>" + "Year" + "</th></tr></thead>")
     sNameValue = dr("sName").ToString
        sDurValue = dr("sDuration").ToString
        sStateValue = dr("sState").ToString
        sPartyValue = dr("sParty").ToString
        sYearValue = dr("sYear").ToString
        oSB.Append("<tbody id=tbodyid'>")
        ' oSB.Append("<tr id='trid'>")
        oSB.Append("<tr>")
        oSB.Append("<td id='tdid' class=border1>")
        oSB.Append(sNameValue)
        oSB.Append("</td>")

        oSB.Append("<td class=border1>")
        oSB.Append(sDurValue)
        oSB.Append("</td>")

        oSB.Append("<td id='td_state' class=border1>")
        oSB.Append(sStateValue)
        oSB.Append("</td>")

        oSB.Append("<td class=border1>")
        oSB.Append(sPartyValue)
        oSB.Append("</td>")

        oSB.Append("<td class=border1>")
        oSB.Append(sYearValue)
        oSB.Append("</td>")

        oSB.Append("</tr>")
        oSB.Append("</tbody>")
    End While
    dr.Close()
    con.Close()
    oSB.Append("</table>")
    'MsgBox(table.ToString)
    Debug.Print(oSB.ToString)

can somebody tell me where i am wrong?

有人能告诉我我错在哪里吗?

Cheers again

再次欢呼

回答by jeev

you can not write javascript directly in vb.net code. However you can register and fire javascript from vb.net code.

你不能直接在 vb.net 代码中编写 javascript。但是,您可以从 vb.net 代码注册和触发 javascript。

Try this

尝试这个

Page.RegisterClientScriptBlock("key","<script>alert('Hello World');</script>");

Try this vb code block:

试试这个 vb 代码块:

Dim strScript As String = "<script>"
strScript += "alert('Hello, Pavan');"
strScript += "</script>"
Page.RegisterClientScriptBlock("strScript", strScript)

回答by Jani Hyyti?inen

I can see that you're using

我可以看到你正在使用

$('[id^=tname] th').live('click'...

From the jQuery docs however:

然而,来自 jQuery 文档:

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live(). http://api.jquery.com/live/

从 jQuery 1.7 开始,不推荐使用 .live() 方法。使用 .on() 附加事件处理程序。旧版本 jQuery 的用户应该优先使用 .delegate() 而不是 .live()。 http://api.jquery.com/live/

In addition to that, TH is a really bad event target. As you'd need to click the TH, not the text within it. Otherwise your event won't trigger.

除此之外,TH 是一个非常糟糕的事件目标。因为您需要单击 TH,而不是其中的文本。否则您的事件将不会触发。

See the markup in here: http://jsfiddle.net/4eXkT/2/

请参阅此处的标记:http: //jsfiddle.net/4eXkT/2/

That should work.

那应该有效。

In reality, you don't need to embed any javascript into your vb.net code. Simply include a javascript file into your page and use the jQuery's .on()event handler. If you must manage the javascript from your web service, have another endpoint for returning the javascript to be used in accordance to your web service generated code.

实际上,您不需要将任何 javascript 嵌入到您的 vb.net 代码中。只需在您的页面中包含一个 javascript 文件并使用 jQuery 的.on()事件处理程序。如果您必须从您的 Web 服务管理 javascript,请根据您的 Web 服务生成的代码使用另一个端点来返回要使用的 javascript。

If you MUST use any other version than the latest jQuery, please refer to the documentation provided above.

如果您必须使用最新 jQuery 以外的任何其他版本,请参阅上面提供的文档。

回答by Adil

You can not write javascript code in vb.net code. You can add/embed javascript code in vb.net code to execute on client. Usually you need to write javascript code in aspx page. You can use ScriptManager.RegisterClientScriptBlockto register the script from vb code.

您不能在 vb.net 代码中编写 javascript 代码。您可以在 vb.net 代码中添加/嵌入 javascript 代码以在客户端上执行。通常你需要在aspx页面中编写javascript代码。您可以使用 ScriptManager.RegisterClientScriptBlock从 vb 代码注册脚本。