danyim danyim - 5 months ago 23
Javascript Question

Executing ASP.NET 1.1 Code within Javascript

When the user clicks a certain LinkButton, I need to open a confirmation dialog with OK/Cancel. If the user hits OK, then I need to run a function from ASP.NET (something to update the database).

Something like the following...

Javascript

function openConfirm() {
return window.confirm("Are you sure you want to do this?");
}


ASP

<asp:LinkButton runat="server" CommandName="viewPart" onClick="if(openConfirm() === true) <% SomeASPFunctionCall() %>;">
Delete
</asp:LinkButton>


The catch is that my application is running ASP.NET 1.1, so any references to adding an
OnClientClick
to the control is irrelevant (because
OnClientClick
has been added for ASP 2.0). I have tried postbacks via __doPostBack and the __eventObject and __eventArguments, but those simply do not work or I can't figure it out.

How should I manage this combination of client-side/server-side interaction?

Thank you

Answer

your linkButton has no ID, are you sure?

put an ID to it, let's imagine it's called myLinkButton (don't use this id please :D )

in the Page_PreRender put this code:

myLinkButton.Attributes.Add("onClick", "return window.confirm('Are you sure you want to do this?'");

it's pseudo code but should build and work also in .NET 1.1 I think, I am not using it since ages...

Edit: inside your grid, in the ItemDataBound or RowDataBound event depending on Framework and Grid version... put something like this:

private void myDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var myControl =  (LinkButton)e.Item.FindControl("myLinkButton");

        if(myControl != null)
        {
            myControl.Attributes.Add("onClick", "return window.confirm('Are you sure you want to do this?'");
        }
    }
}