有条件的确认提示在asp.net code背后有条件、提示、asp、net

2023-09-10 21:23:50 作者:很是差勁

我环顾四周实现这一点的方式。这里是一个伪code再$ P $的我有什么psentation:

I have looked around for a way of implementing this. Here is a pseudocode representation of what I have:

bool hasData = ItemHasData(itemid);
Confirm = "false"; // hidden variable

if (hasData)
{
    //Code to call confirm(message) returns "true" or "false"
    if (Confirm == "true")
    {
         //Delete item
    }
    else if (Confirm == "false")
    {
         return;
    }
}

在code打电话确认使用的asp:立即控制,并设置它等于确认。我可以弹出,但只有在函数退出之后。它不做任何处理后的条件。

The code to call confirm uses a asp:Literal control and sets it equal to the confirm. I can get the popup but only after the function exits. And it does nothing with the conditions after that.

普遍的共识似乎是,调用JavaScript在那个特定行是不可能的(是有道理的,由于服务器端/客户端的差距),但我怎么能做到这一点?我试着用ConfirmButtonExtender从ASP.NET AJAX工具包,但我不能从当对象被设置为=服务器的背后code中的confirmbuttonextender对象交互。

The general consensus seems to be that calling the javascript at that specific line is impossible (makes sense due to the server side/client side gap), but how can I achieve this? I tried using the ConfirmButtonExtender from the ASP.NET AJAX Toolkit but I couldn't interact with the confirmbuttonextender object from the code behind when the object is set to runat="server".

编辑:

对不起,我确实怀念那些花絮。由于伊卡洛斯。

Sorry, I did miss those tidbits. Thanks Icarus.

控件本身是在GridView(伪版本实际上是从gvData_RowCommand功能)的rowcommand。第一检查一下,看是否是的CommandName DeleteItem并且如果是进入这个

The control itself is the GridView (the pseudo version is actually from the gvData_RowCommand function)'s rowcommand. The first check looks to see if the CommandName is DeleteItem and if so goes into this.

的gvData的列设置基于关闭头(和数据集)的列表传递,因为它是针对工作是为具有不同必需的信息的多个项目的表。该gvData的数据是存在的,我只需要得到一个是/否(或在现实中,它会最终被确定/取消)对话框,以验证他们想要删除的项目时,有数据。

The gvData's columns are set based off a list of headers (and the dataset) passed as the table it is working against is for multiple items with different required information. The gvData's data is there, I just need to get a Yes/No (or in reality it'll end up being Ok/Cancel) dialog to verify they want to delete the item when there is data.

推荐答案

一个方法我结束了在某些情况下使用是有显示的确认/取消按钮的面板。这就避免了需要处理JavaScript事件,并使用ASP.NET完全。

One method I end up using in some situations is to have a Panel that displays the Confirm / Cancel buttons. This avoids the need to handle JavaScript events and uses ASP.NET entirely.

<asp:Panel ID="pDeleteConfirm" runat="server"
    CssClass="AlertDialog"
    Visible="False">
    <p>Do you wish to delete the selected record?<br />
    <asp:Button ID="btDeleteYes" runat="server" OnClick="btDelete_Click" Text="Delete" />
    <asp:Button ID="btDeleteNo" runat="server" OnClick="btDelete_Click" Text="Cancel" />
    </p>
</asp:Panel>

<asp:GridView ID="gvData" runat="server"
    AutoGenerateColumns="False" 
    CssClass="GridView"
    DataKeyNames="ID"
    DataSourceID="sqlData"
    EmptyDataText="There is no data entered in the system."
    OnRowDeleting="gvData_RowDeleting">
    ......
</asp:GridView>

我用的是 OnRowDeleting 事件显示面板

protected void gvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    // Show confirmation dialog
    pDeleteConfirm.Visible = true;

    // Select the row to delete
    gvData.SelectedIndex = e.RowIndex;

    // Cancel the delete so the user can use the confirm box
    e.Cancel = true;
}

处理按钮点击事件

protected void btDelete_Click(object sender, EventArgs e)
{
    Button bt = (Button)sender;
    switch (bt.ID)
    {
        case "btDeleteYes": // they confirmed a delete
            sqlData.Delete();
            break;

        case "btDeleteNo": // they clicked cancel
            // Do nothing
            break;

        default:
            throw new Exception("Unknow button click in btDelete_Click");
    }
    // clear selection and hide the confirm box
    gvData.SelectedIndex = -1;
    pDeleteConfirm.Visible = false;
}

这是不是JavaScript的,但你可以添加一些的UpdatePanel s到做就可以了AJAX的工作。

This isn't JavaScript but you can add in some UpdatePanels to do AJAX work on it.

只有一个方法来做到这一点通过ASP.NET而不是JavaScript的处理。

Just one method to do it through ASP.NET rather than JavaScript handling.

 
精彩推荐
图片推荐