javascript - 从 javascript 调用隐藏代码

标签 javascript c# asp.net client-side onkeypress

我想解决一个问题,

试试这个

<script type="text/javascript">
    function txtOnKeyPress(txt1) {
        if (txt1 != 'undefined') {
            var txt2 = document.getElemejavantById('<%=TxtArama.ClientID %>');
            txt2.value = txt1.value;
            <% Session["Sercert"] = TxtArama.Text;%> 
            //alert(txt2.value);
            var DTT =<%= GetSearcher("") %>;
            alert(DTT);

        }
    }
    <%--function CallCodeBehindMethod() {
        var txt2 = document.getElementById('<%=TxtArama.ClientID %>');
        alert(window.PageMethods.GetName(txt2));
        //this.GetName(txt2);
    }--%>
</script>

JS是客户端文本框按键事件

事件背后的代码是

protected string GetSearcher(String KeyValue)
    {
        KeyValue = Session["Sercert"].ToString();
        String aa = "";
        if (KeyValue.Length > 0)
        {
            DataTable DT = new DataTable();
            DT = DbClass.GetDataTable("SELECT * FROM Products WHERE ProductName LIKE '%" + KeyValue + "%' LIMIT 10", "MySql");

            for (int i = 0; i < DT.Rows.Count; i++)
            {
                aa += "<a href=\"" +ReWriterPath(DT.Rows[i]["ProductId"].ToString(), DT.Rows[i]["ProductName"].ToString()) +"\">" +DT.Rows[i]["ProductName"] +"</a><br />";
            }


            RptSearcher.DataSource = DT;
            RptSearcher.DataBind();
            UPLSearcher.Update();
        }
        return aa;

    }

我尝试在中继器中填充数据

<asp:UpdatePanel runat="server" ID="UPLSearcher" UpdateMode="Conditional">
                                        <ContentTemplate>
                                            <asp:Literal runat="server" ID="LtrSearcher"></asp:Literal>
                                            <asp:Repeater runat="server" ID="RptSearcher">
                                                <ItemTemplate>

                                                    <a href="<%# ReWriterPath(Eval("ProductId").ToString(), Eval("ProductName").ToString()) %>"><%# Eval("ProductName") %></a><br />
                                                </ItemTemplate>
                                            </asp:Repeater>
                                        </ContentTemplate>
                                    </asp:UpdatePanel>

我想在代码隐藏方法的搜索按键事件中更新我的中继器。 并重新更新我的更新面板并在结果中列出。 但js代码和方法只运行页面加载。并且 key pre 不在侧代码后面的代码上运行。 但是这个//alert(txt2.value);每次按键时运行。

请帮忙。

最佳答案

您必须在代码隐藏中创建[WebMethod]。您可以从 javascript 调用该方法。

 <script type="text/javascript">
    function txtOnKeyPress(txt1) {

        $.ajax({
            type: "POST",
            url: "/yourpage.aspx/GetSearcher",
            data: '', // put your data here that you want to pass in server method
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        //write your code what you want to display on success
    }
</script>

在方法后面创建代码

 [WebMethod]
    [ScriptMethod()]
    public static string GetSearcher(String KeyValue)
    {  //Your code goes here  }

关于javascript - 从 javascript 调用隐藏代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27901046/

相关文章:

c# - ASP.NET C# 如何从 header 中获取用户语言?

c# - 如何使用 DataContractJsonSerializer 反序列化字典?

asp.net - 陷阱 : Upgrading from single servers to web farms

javascript - 在javascript中声明一个变量并在html中使用

c# - 使用 NHibernate 管理单例实体的 session

c# - 从 ASP.NET Web 应用程序运行 .exe 的权限问题

html - ASP.NET 主页导航当前页面 Css

javascript - 如何使用 jQuery 获取输入值并提交

javascript - 工作箱服务人员: Cannot use import statement outside a module

javascript - 如何将 Typed.js 包含在 Aurelia 项目中