简体   繁体   中英

How to perform OnClientClick before OnClick?

I have button with OnClick event and OnClientClick , inside of listView . The onClientClick have JS function confirm('sure??'). The confirmation message should appear before postback upon specific condition. How to perform that onClientClick depending on the condition inside the listview itemDataBound() ?

Code:

protected void lvCo_ItemDataBound(object sender, ListViewItemEventArgs e){
int Req = Convert.ToInt32(((Label)e.Item.FindControl("lblCoId")).Text);

if (Req==5)
   // Show Confirmation Message 
else
   // Don't Show the Confirmation Message
}

aspx:

<asp:ListView ID="lvCo" runat="server" DataKeyNames="CoId"                    OnItemCommand="lvCoFind_ItemCommand" OnItemDataBound="lvCo_ItemDataBound">
   // .. Layouttemplate .. && Itemtemplate

    <asp:Button Text="Save" ID="btSaveOp" CommandName="Save"  runat="server" OnClientClick="if (!confirm('Are you sure?')) return false;" />

Not sure if this is possible, but how about moving the onclientclick out of the aspx page and just setting it in the code behind.

protected void lvCo_ItemDataBound(object sender, ListViewItemEventArgs e){
int Req = Convert.ToInt32(((Label)e.Item.FindControl("lblCoId")).Text);

if (Req==5) // Show Confirmation Message 
   ((Button)e.Item.FindControl("btSaveOp")).OnClientClick = "if (!confirm('Are you sure?')) return false;";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM