asp.net代码后面的条件确认提示

时间:2011-10-06 15:42:49

标签: javascript asp.net ajax confirm

我四处寻找实现这一目标的方法。这是我所拥有的伪代码表示:

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;
    }
}

要调用的代码使用asp:Literal控件并将其设置为等于confirm。我可以获得弹出窗口,但只有在函数退出后才会出现。在那之后,它对条件没有任何作用。

普遍的共识似乎是在该特定行调用javascript是不可能的(由于服务器端/客户端间隙而有意义),但我怎样才能实现这一点?我尝试使用ASP.NET AJAX Toolkit中的ConfirmButtonExtender但是当对象设置为runat =“server”时,我无法与后面的代码中的confirmbuttonextender对象进行交互。

编辑:

抱歉,我确实错过了那些花絮。谢谢伊卡洛斯。

控件本身是GridView(伪版本实际上来自gvData_RowCommand函数)的rowcommand。第一项检查是查看CommandName是否为DeleteItem,如果是,则进入此状态。

gvData的列是根据标题列表(和数据集)设置的,因为它正在处理的表是针对具有不同所需信息的多个项目。 gvData的数据就在那里,我只需要得到一个是/否(或实际上它最终会被确定/取消)对话框,以确认他们想要在有数据时删除该项目。

2 个答案:

答案 0 :(得分:3)

我在某些情况下最终使用的一种方法是让Panel显示确认/取消按钮。这样就无需处理JavaScript事件并完全使用ASP.NET。

<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事件来显示Panel

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;
}

处理按钮Click事件

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来对其进行AJAX工作。

只需一种方法即可通过ASP.NET而不是JavaScript处理它。

答案 1 :(得分:1)

代码隐藏的工作是将HTML呈现给浏览器。它执行此操作,然后关闭套接字并且不再执行服务器端代码。

您必须在客户端使用Javascript函数实现此逻辑。

您是否尝试在用户执行某项操作之前警告用户是否已装入数据?或者也许在他们试图离开页面之前?当页面上发生该操作时,您必须使用脚本弹出警报。

相关问题