簡體   English   中英

如何使用按鈕填充數據而不進行回發

[英]How to use a button to populate data without doing a postback

我的GridView中有以下ASP.net按鈕:

<asp:Button UseSubmitBehavior="false" runat="server" ID="btnShow" CssClass="btnSearch" Text="View All" CommandName="ViewAll" OnCommand="btnShow_Command" CommandArgument='<%#((GridViewRow)Container).RowIndex%>' />

后面的代碼是:

protected void btnShow_Command(object sender, CommandEventArgs e)
{
    int index = 0;

    if (e.CommandName == "ViewAll")
    {
        index = Convert.ToInt32(e.CommandArgument);

        DataTable cacheTable = HttpContext.Current.Cache["ResultsTable"] as DataTable;

        string column = cacheTable.Rows[index].Field<string>("Guideline");
        string test = BookingResults.Rows[index].Cells[7].Text;
        string html = HttpUtility.HtmlDecode(column);

        ResultsDiv.InnerHtml = html;
    }
}

使用jQuery在彈出窗口中顯示ResultsDiv:

//CONTROLLING EVENTS IN jQuery
$(document).ready(function () {
    //Click the button event!
    $(".btnSearch").click(function (e) {
        alert($(this).val() + " Clicked");
        e.preventDefault();
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });
});

當我導航到頁面時,生成的HTML如下所示(該列中有多行具有相同的按鈕):

<input type="button" name="ctl00$ctl33$g_36ed1b14_1f08_43fb_8099_eb3423a33ed9$BookingResults$ctl224$btnShow" value="View All" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ctl33$g_36ed1b14_1f08_43fb_8099_eb3423a33ed9$BookingResults$ctl224$btnShow&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))" id="ctl00_ctl33_g_36ed1b14_1f08_43fb_8099_eb3423a33ed9_BookingResults_ctl224_btnShow" class="btnSearch" />

現在發生的是,當我單擊“ View All按鈕時,它顯示警報,當我單擊“確定”時,它顯示瞬間的彈出窗口並刷新頁面。

如何修改后台代碼/ JQuery,以便可以單擊任何按鈕,它會每次顯示警報並顯示彈出窗口,而不進行回發?

我認為您甚至需要在警報之前要做的第一件事就是e.preventDefault();

//CONTROLLING EVENTS IN jQuery
$(document).ready(function () {
    //Click the button event!
    $(".btnSearch").click(function (e) {
        e.preventDefault();
        e.stopproPagation();
        alert($(this).val() + " Clicked");

        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });
});

正如我所認為的,警報使事件首先執行其默認工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM