424,054 Members | 1,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,054 IT Pros & Developers. It's quick & easy.

Confirmation Box appears on clicking twice on the link button of a template column.

P: 3
I have a DataGrid, to which I have added a template column for importing data to the data base.
I added the confirmation logic as discussed in this forum.
Now I have to click twice for the confirm box to appear.


I have a data grid with the template column. Below is the code:
Expand|Select|Wrap|Line Numbers
  1. <asp:DataGrid ID="dgImportList" runat="server" Width="100%" BorderStyle="Solid" AutoGenerateColumns="False">  
  2.   <AlternatingItemStyle CssClass="tabledata2"></AlternatingItemStyle>
  3.   <ItemStyle CssClass="tabledata1"></ItemStyle>
  4.   <HeaderStyle CssClass="tableHeader2"></HeaderStyle>
  5.   <Columns>
  6.     <asp:BoundColumn DataField="FileName" HeaderText="Name"></asp:BoundColumn>
  7.     <asp:BoundColumn DataField="Date" HeaderText="Date"></asp:BoundColumn>
  8.     <asp:ButtonColumn ButtonType="LinkButton"  Text="Import" CommandName="Import"></asp:ButtonColumn>
  9.     <asp:ButtonColumn ButtonType="LinkButton" Text="Check" CommandName="FormatCheck">
  10.     </asp:ButtonColumn>
  11.     <asp:TemplateColumn>
  12.     <ItemTemplate>
  13.       <asp:LinkButton ID="lbImportTemplate"
  14.         runat="server"
  15.         CausesValidation="false" 
  16.         CommandName="ImportTemplate"
  17.         Text="ImportTemplate"
  18.         OOnClick="ConfirmBtnClickHandler"></asp:LinkButton>
  19.     </ItemTemplate>
  20.   </asp:TemplateColumn>
  21.   </Columns>                                          </asp:DataGrid>

Also the asp.net code which asks for confirmation. Below is the code:
Expand|Select|Wrap|Line Numbers
  1. private void dgImportList_ItemCommand(Object sender, DataGridCommandEventArgs e)
  2.         {
  3.  
  4.             if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  5.             {
  6.                 LinkButton lb = e.Item.FindControl("lbImportTemplate") as LinkButton;
  7.                 lb.OnClientClick = "if(!confirm('Are you sure to import this item?')){return false;}";
  8.                 //Do something
  9.             }
I have to click on the link button twice, to get the confirm box populated.

Please help!
Nov 17 '14 #1
Share this Question
Share on Google+
4 Replies


Frinavale
Expert Mod 5K+
P: 9,731
Instead of waiting until the user clicks the link, use the GridView.RowDataBound Event to apply the client click JavaScript code that asks for confirmation.

Or change your Template to include the Client Click code like this:

Expand|Select|Wrap|Line Numbers
  1. <asp:TemplateColumn>
  2.     <ItemTemplate>
  3.       <asp:LinkButton ID="lbImportTemplate"
  4.         runat="server"
  5.         CausesValidation="false" 
  6.         CommandName="ImportTemplate"
  7.         Text="ImportTemplate"
  8.         OnClick="ConfirmBtnClickHandler"
  9.         OnClientClick="if(!confirm('Are you sure to import this item?')){return false;}"></asp:LinkButton>
  10.     </ItemTemplate>
  11.   </asp:TemplateColumn>
Nov 18 '14 #2

P: 3
Thanks Frinny for the response, but I have a DataGrid instead of GridView.
Nov 18 '14 #3

Frinavale
Expert Mod 5K+
P: 9,731
Ok,
The same concept still applies: use the DataGrid.ItemDataBound Event or modify your template (which is a lot easier to do).

-Frinny
Nov 18 '14 #4

P: 3
I modified the template and it worked.
Thank you so much Frinny. :)
Nov 19 '14 #5

Post your reply

Sign in to post your reply or Sign up for a free account.