簡體   English   中英

JQuery DIalog和ASP.NET Repeater

[英]JQuery DIalog and ASP.NET Repeater

我有一個ASP.NET轉發器,它顯示了一個帶有刪除LinkBut​​ton的項目列表。

我想設置Delete LinkBut​​tons以顯示JQuery對話框以進行確認。 如果單擊“確定”按鈕,我想進行回發。

顯而易見的問題是轉發器中的每個LinkBut​​ton都有自己的ID,我不想復制對話框的所有javascript。

建議?

解決方案並非如此簡單。 按下jQuery UI Dialog的Ok按鈕后,您必須能夠調用原始回調函數。

首先,您需要一個通用的js函數來顯示對話框:

function showConfirmRequest(callBackFunction, title, content) 
{
    $("#divConfirm").html(content).dialog({
        autoOpen: true,
        modal: true, 
        title: title,
        draggable: true,
        resizable: false,
        close: function(event, ui) { $(this).dialog("destroy"); },
        buttons: { 
            'Ok': function() { callBackFunction(); },
            'Cancel': function() {
                $(this).dialog("destroy");
            }
        },
        overlay: { 
            opacity: 0.45, 
            background: "black" 
        } 
    });
}

我認為像div一樣存在

<div id="divConfirm"></div>

在c#代碼隱藏之后,你必須注冊以前的客戶端函數,將控件的原始asp.net callbackFunction作為參數傳遞(我概括):

protected void AddConfirmRequest(WebControl control, string title, string message) 
{
    string postBackReference = Page.ClientScript.GetPostBackEventReference(control, String.Empty);
    string function = String.Format("javascript:showConfirmRequest(function() {{ {0} }}, '{1}', '{2}'); return false;", 
                                     postBackReference,
                                     title,
                                     message);
    control.Attributes.Add("onclick", function);
}

通過GetPostBackEventReference方法,您可以檢索asp.net分配給控件的回發函數。

現在,在Repeater ItemDataBound上,檢索執行刪除的控件並將其傳遞給此函數:

<asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_OnItemDataBound">
    ...
    <ItemTemplate>
        ...
        <asp:Button ID="btnDelete" runat="server" Text="Delete" />
        ...
    </ItemTemplate>
</asp:Repeater>

和代碼:

protected void repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        WebControl btnDelete = ((WebControl)e.Item.FindControl("btnDelete"));
        AddConfirmRequest(btnDelete, "Confirm delete", "Are you sure? Really???");
    }
}

我希望這有幫助。

<asp:GridView ... CssClass="mygridview"></asp:GridView>

$('table.mygridview td a').whatever()

這將允許您同時使用所有鏈接按鈕。

你可以這樣做:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        ...
        <asp:LinkButton OnClick="DoSomething" OnClientClick="return ConfirmDelete();" ID="btnConfirm" runat="server" CssClass="button" Text="Delete"></asp:LinkButton><br /><br />
    </ItemTemplate>
</asp:Repeater>

<script>
    function ConfirmDelete() {
        return confirm("Delete this record?");
    }
</script>

或者我認為你也可以這樣做:

<script>
    $(function() {
        $(".button").click(function() {
            return confirm("Delete this record?");
        });
    });
</script>

在ConfirmDelete方法中,您可以定義jQuery Confirm對話框

HY,
首先你應該使用Jquery Dialog或其他clienside對話框,它更酷。

您應該在頁面上有一個html元素來調用Jquery對話框彈出窗口。

<div class="Popup"></div>

<script>
    var confirm = false;
    function ConfirmDelete(doPostback) {
        $(".Popup").dialog(){ /* threat the dialog result , set confirm variable */ };
        if(confirm) {
           __doPostback(); }
        else return false;
    }
</script>


在我放置注釋句子的部分,您可以放置​​代碼來處理對話結果。 您可以從上面的鏈接中找到信息。

該函數返回false,因此它阻止服務器端代碼的執行(異步回發)。
Button應如下所示:

<asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
    <asp:LinkButton OnClientClick="ConirmDelete(<#%GetPostbackReference()%>)" CommandArgument = "<%# DataBinder.Eval(Container.DataItem, "Id") %>" OnClick="btnConfirm_Click" ID="btnConfirm" runat="server"></asp:LinkButton>
    </ItemTemplate>
</asp:Repeater>



CommandArgument屬性上,我將綁定到轉發器的項目的ID設置為。
通過這種方式,在btnConfirm_Click事件中,您可以訪問此參數

void btnConfirm_Click(object sender, CommandEventArgs e)
{
   e.CommandArgument -> you will find the id an you can execute the delete
}

你應該對后面的代碼:

protected string GetPostbackReference()
{    
return Page.ClientScript.GetPostBackEventReference(btnConfirm, null);
}


在元素的綁定上調用此函數並返回當前控件的回發方法,它看起來像__doPostback(source,param)

這是一個javascript方法,您可以執行easilly,並且您可以完全控制回發。 在客戶端,您可以決定是否調用此回發事件。


PS:如果有什么不清楚的話,這里有一個問題,我會更新答案。

<asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
...
                <asp:LinkButton OnClick="DoSomething" OnClientClick="return ConfirmDelete();" ID="btnConfirm" runat="server" CssClass="button" Text="Delete"></asp:LinkButton><br /><br />
            </ItemTemplate>
        </asp:Repeater>
<script>
        function ConfirmDelete() {
            return confirm("Delete this record?");
        }
</script>

問題肯定是由tanathos回答的,但我有另一種選擇,如果你這么傾向,可以避免代碼隱藏中的腳本。 我只是使用display:none隱藏了asp刪除按鈕,並添加了一個刪除按鈕,如果確認刪除,則會調用確認對話框並單擊隱藏的asp刪除按鈕。

轉發器中的HTML:

<ItemTemplate>
...
<td>
    <a href="#" class="dummy-delete-button">Delete</a>
    <asp:Button ID="DeletePolicyButton" runat="server" OnCommand="OnDeleteCommand" CommandArgument="Argument" Text="Delete" CssClass="delete-button" />
</td>
...
</ItemTemplate>

CSS:

.delete-button 
{
    display: none;
}

javascript:

// make the dummy button look like a button
$("a.dummy-delete-button").button({
    icons: {
        primary: "ui-icon-trash"
    }
});

// create the dialog
var deleteDialog = $('<div>Are you sure you want to remove this policy?</div>')
    .dialog({
        autoOpen: false,
        modal: true,
        title: 'Delete Policy'
    });

// handle click event to dummy button
$("a.dummy-delete-button").click(function (e) {
    // don't follow the href of the dummy button
    e.preventDefault();
    // get a reference to the real ASP delete button
    var button = $(this).closest('td').find('.dummy-delete-button');
    deleteDialog.dialog({
        buttons: {
            // handle delete. Note: have to defer actual button click until after close
            // because we can't click the button while the modal dialog is open.
            "Delete": function () { deleteDialog.bind("dialogclose", function () { button.click() }); $(this).dialog("close"); },
            // handle close
            "Cancel": function () { $(this).dialog("close"); }
        }
    });

    deleteDialog.dialog("open");
});

暫無
暫無

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

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