开发者

Executing ASP.NET 1.1 Code within Javascript

开发者 https://www.devze.com 2023-04-05 11:28 出处:网络
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).

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:LinkBut开发者_JS百科ton>

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


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?'");
        }
    }
}
0

精彩评论

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

关注公众号