Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command Still Executes When Cancel Is Selected On Dialog Box

Status
Not open for further replies.

JabbaTheNut

Programmer
Jul 29, 2002
176
US
I have a DropDownList and an ImageButton in ItemTemplates of a DataGrid. My ImageButton is intended to execute an action based on the selected value in the DropDownList. For some (not all) of the values in the DropDownList, a dialog box must pop up to inform the user of what they are about to do and to confirm that they still want to do it (similar to the "delete" confirmation discussed in other posts, except that the message contained in the dialog box will vary depending on the DropDownList selection).

To accomplish this I am using a javascript function that accepts the DropDownList's ClientID and checks its selected value. If the value is one of those that needs a confirmation dialog, the function returns the dialog with the proper message. In the OnItemDataBound event of the DataGrid, I add an attribute for the "onclick" event of the ImageButton which calls this javascript function and properly supplies the function with the DropDownList's ClientID. This all works fine and the dialog displays the proper messages.

The problem is that the ImageButton's action is still executed whether the user selects "OK" or "CANCEL" on the dialog box. I want the action to stop if the user selects "CANCEL".

Here is the code that I am using...

**********CODE START**********

###The pertinent controls in the .aspx page...###

<asp:datagrid id=&quot;myDG&quot; runat=&quot;server&quot; OnItemCommand=&quot;dgDoCommand&quot; OnItemDataBound=&quot;dgItemDataBound&quot; AutoGenerateColumns=&quot;False&quot;>

<ItemTemplate>
<table><tr><td>
<asp:DropDownList ID=&quot;actionlist&quot; Runat=&quot;server&quot; DataSource='<%# ActionsDDLItems %>' DataTextField=&quot;actiontext&quot; DataValueField=&quot;actionvalue&quot;>
</td></tr></table>
</ItemTemplate>

<ItemTemplate>
<table><tr><td>
<asp:ImageButton ID=&quot;actionbtn&quot; CommandName=&quot;DoAction&quot; ImageUrl=&quot;images/gobtn.gif&quot; Runat=&quot;server&quot; CausesValidation=&quot;False&quot;></asp:ImageButton>
</td></tr></table>
</ItemTemplate>

###The pertinent code-behind functions...###

protected void dgItemDataBound(object s, DataGridItemEventArgs e)
{
ListItemType itemtype = e.Item.ItemType;
if (itemtype == ListItemType.Item ||
itemtype == ListItemType.AlternatingItem)
{
DropDownList ddl = new DropDownList();
ddl = (DropDownList) e.Item.FindControl(&quot;actionlist&quot;);
ImageButton imgbtn = new ImageButton();
imgbtn = (ImageButton) e.Item.FindControl(&quot;actionbtn&quot;);
imgbtn.Attributes.Add(&quot;onclick&quot;, &quot;javascript:chkAction('&quot; +
ddl.ClientID.ToString() + &quot;');&quot;);
}
}

protected void dgDoCommand(object s, DataGridCommandEventArgs e)
{
if (e.CommandName == &quot;DoAction&quot;)
{
&quot;Do Some Action&quot;;
}
}

###The javascript function (stripped down to base elements)...###

function chkAction(ddlid)
{
var ddlObj = new Object();
ddlObj = eval(&quot;document.getElementById('&quot; + ddlid + &quot;')&quot;);
return confirm(&quot;Proper Message for: &quot; + ddlObj.value);
}

**********CODE END**********

When the ImageButton is clicked, a dialog pops up and properly displays the message. However, if &quot;CANCEL&quot; is selected on the dialog, the action is still executed. How do I stop the action from executing?

I apologize for the long post and thank you for any help you may offer

Game Over, Man!
 
The line

imgbtn.Attributes.Add(&quot;onclick&quot;, &quot;javascript:chkAction('&quot; + ddl.ClientID.ToString() + &quot;');&quot;);

should be replaced with

imgbtn.Attributes.Add(&quot;onclick&quot;, &quot;javascript:return chkAction('&quot; + ddl.ClientID.ToString() + &quot;');&quot;);

Like this, when the javascript function returns false, the form will not do a postback.

regards,
Blaxo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top