开发者

Conditional confirm prompt in asp.net code behind

开发者 https://www.devze.com 2023-04-11 00:13 出处:网络
I have looked around for a way of implementing this. Here is a pseudocode representation of what I have:

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

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.

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".

edit:

Sorry, I did miss those tidbits. T开发者_C百科hanks Icarus.

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.

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.


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>

I use the OnRowDeleting event to show the 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;
}

Handle the button Click events

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

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

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


The job of your code-behind is to render HTML out to the browser. It does this, and then the socket is closed and your server side code is no longer executing.

You'll have to implement this logic using a Javascript function on the client side.

Are you attempting to warn the user if there's data loaded before they take a certain action? Or perhaps before they try to leave the page? You'll have to popup the alert using script when that action happens on the page.

0

精彩评论

暂无评论...
验证码 换一张
取 消