JabbaTheNut
Programmer
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="myDG" runat="server" OnItemCommand="dgDoCommand" OnItemDataBound="dgItemDataBound" AutoGenerateColumns="False">
<ItemTemplate>
<table><tr><td>
<aspropDownList ID="actionlist" Runat="server" DataSource='<%# ActionsDDLItems %>' DataTextField="actiontext" DataValueField="actionvalue">
</td></tr></table>
</ItemTemplate>
<ItemTemplate>
<table><tr><td>
<asp:ImageButton ID="actionbtn" CommandName="DoAction" ImageUrl="images/gobtn.gif" Runat="server" CausesValidation="False"></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("actionlist"
ImageButton imgbtn = new ImageButton();
imgbtn = (ImageButton) e.Item.FindControl("actionbtn"
imgbtn.Attributes.Add("onclick", "javascript:chkAction('" +
ddl.ClientID.ToString() + "');"
}
}
protected void dgDoCommand(object s, DataGridCommandEventArgs e)
{
if (e.CommandName == "DoAction"
{
"Do Some Action";
}
}
###The javascript function (stripped down to base elements)...###
function chkAction(ddlid)
{
var ddlObj = new Object();
ddlObj = eval("document.getElementById('" + ddlid + "')"
return confirm("Proper Message for: " + ddlObj.value);
}
**********CODE END**********
When the ImageButton is clicked, a dialog pops up and properly displays the message. However, if "CANCEL" 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!
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="myDG" runat="server" OnItemCommand="dgDoCommand" OnItemDataBound="dgItemDataBound" AutoGenerateColumns="False">
<ItemTemplate>
<table><tr><td>
<aspropDownList ID="actionlist" Runat="server" DataSource='<%# ActionsDDLItems %>' DataTextField="actiontext" DataValueField="actionvalue">
</td></tr></table>
</ItemTemplate>
<ItemTemplate>
<table><tr><td>
<asp:ImageButton ID="actionbtn" CommandName="DoAction" ImageUrl="images/gobtn.gif" Runat="server" CausesValidation="False"></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("actionlist"
ImageButton imgbtn = new ImageButton();
imgbtn = (ImageButton) e.Item.FindControl("actionbtn"
imgbtn.Attributes.Add("onclick", "javascript:chkAction('" +
ddl.ClientID.ToString() + "');"
}
}
protected void dgDoCommand(object s, DataGridCommandEventArgs e)
{
if (e.CommandName == "DoAction"
{
"Do Some Action";
}
}
###The javascript function (stripped down to base elements)...###
function chkAction(ddlid)
{
var ddlObj = new Object();
ddlObj = eval("document.getElementById('" + ddlid + "')"
return confirm("Proper Message for: " + ddlObj.value);
}
**********CODE END**********
When the ImageButton is clicked, a dialog pops up and properly displays the message. However, if "CANCEL" 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!