【问题标题】:Running a Databind method from jquery keypress method从 jquery keypress 方法运行 Databind 方法
【发布时间】:2018-01-19 10:24:45
【问题描述】:

我有一个简单的页面,其中转发器由 c# 数据绑定方法填充。该页面有一个搜索文本框,我想在您键入时进行搜索。这通过 databind 方法中的 rowfilter 起作用,通过 searchtextbox 中的内容进行过滤。

我在每个按键上触发 jquery,但希望它在每个按键上运行我的 c# 数据绑定方法。我该怎么做?

数据绑定方法

protected void DataBindVideosRepeaterList()
    {
        DataView dv = (DataView)IRPAdminHelpTopicVideoDB.GetAllVideos();
        dv.RowFilter = this.GetVideosFilter();

        this.RptVideos.DataSource = dv;
        this.RptVideos.DataBind();
    }

行过滤方法

protected string GetVideosFilter()
    {
        string filterCriteria = "";

        if (!string.IsNullOrEmpty(this.TxtVideoSearch.Text))
        {
            filterCriteria = " Tags LIKE '%" + this.TxtVideoSearch.Text + "%'";
        }

        return filterCriteria;
    }

jQuery

<script type="text/javascript">
$("#TxtVideoSearch").keypress(function () {

/** I WANT THE DataBindVideosRepeaterList() METHOD TO RUN HERE **/

});
</script>

【问题讨论】:

    标签: javascript c# jquery asp.net data-binding


    【解决方案1】:

    最简单的方法是在页面上添加一个隐藏的虚拟按钮,该按钮会触发DataBindVideosRepeaterList()

    <span style="display: none">
        <asp:Button ID="DummyButton" runat="server" Text="Button" OnClick="Button1_Click" />
    </span>
    

    现在当按键被触发时,模拟一个按钮点击

    <script type="text/javascript">
        $("#<%= TxtVideoSearch.ClientID %>").keypress(function () {
    
            $("#<%= DummyButton.ClientID %>").click();
    
        });
    </script>
    

    然后会发生一个 PostBack 并运行该方法

    protected void Button1_Click(object sender, EventArgs e)
    {
        DataBindVideosRepeaterList();
    }
    

    但是,我建议在 TextBox TxtVideoSearchblurfocusout 事件或单独的按钮单击上触发过滤器。因为现在每次按键都会有一个 PostBack,并且网站对用户将无法正常运行。

    【讨论】:

    • 好主意。如果我将它全部放在更新面板中并添加按钮单击作为触发器怎么办。这行得通吗?
    • 是的,这样会更好。那就不会有页面刷新了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多