Wrox Programmer Forums
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 15th, 2010, 09:06 AM
Authorized User
 
Join Date: Nov 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default datagrid button commands.

I've been trying to figure this out but it's driving me nuts. I have the following datagrid:

Code:
<asp:DataGrid ID="dgOffertes" OnItemCommand="doSomething" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
                        GridLines="Horizontal" Width="620px"
                        AutoGenerateColumns="False" runat="server"
                        onitemdatabound="dgOffertes_ItemDataBound" DataKeyField="ID" 
                        AllowPaging="True" PageSize="5">
                <PagerStyle Mode="NumericPages" />
                <Columns>
                    <asp:BoundColumn DataField="ID" HeaderText="ID" ItemStyle-CssClass="hidden" HeaderStyle-CssClass="hidden">
                        <HeaderStyle CssClass="hidden" />
                        <ItemStyle CssClass="hidden" />                        
                    </asp:BoundColumn>
                    <asp:ButtonColumn DataTextField="OfferteNr" HeaderText="Offertenummer" CommandName="Select" ItemStyle-CssClass="gridRow" HeaderStyle-CssClass="gridHeader">
                        <HeaderStyle CssClass="gridHeader" />
                        <ItemStyle CssClass="gridRow" />
                    </asp:ButtonColumn>
                    <asp:ButtonColumn DataTextField="Naam" HeaderText="Naam" CommandName="Select" ItemStyle-CssClass="gridRow" HeaderStyle-CssClass="gridHeader">
                        <HeaderStyle CssClass="gridHeader" />
                        <ItemStyle CssClass="gridRow" />
                    </asp:ButtonColumn>
                    <asp:ButtonColumn DataTextField="Plaats" HeaderText="Plaats" CommandName="Select" ItemStyle-CssClass="gridRow" HeaderStyle-CssClass="gridHeader">
                        <HeaderStyle CssClass="gridHeader" />
                        <ItemStyle CssClass="gridRow" />
                    </asp:ButtonColumn>
                    <asp:ButtonColumn DataTextField="OfferteDatum" HeaderText="Datum" CommandName="Select" DataTextFormatString={0:d} ItemStyle-CssClass="gridRow" HeaderStyle-CssClass="gridHeader">
                        <HeaderStyle CssClass="gridHeader" />
                        <ItemStyle CssClass="gridRow" />
                    </asp:ButtonColumn>     
                    <asp:TemplateColumn>
                        <HeaderStyle CssClass="gridHeader" />
                        <ItemStyle CssClass="gridRow" />
                        <HeaderTemplate>Verwijderen</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Button id="btnDelete" runat="server" Text="Delete" CommandName="Delete" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>                     
            </asp:DataGrid>
The final column, is a button to delete that specific row. Whenever I click anywhere on the row, other then the button, I have to go to a page showing the detailed view of the selected item. When I click on the button, I have to get a little javascript pop-up asking if I'm sure.

Now, if I click on ok, it works. The debugger executes everything I associated with the "Delete" command. When I click on "cancel" however, it executes the code associated with the "Select" command.

Adding the javascript to the buttons:

Code:
 protected void dgOffertes_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            Button btn = e.Item.FindControl("btnDelete") as Button;
            if (btn != null)
            {
                btn.Attributes.Add("onclick", "return confirm_delete();");
            }

...
The javascript in question:
Code:
function confirm_delete()
        {
            if (confirm("Weet u zeker dat u deze offerte wilt verwijderen?") == true)
                return true;
            else
                return false;
        }
The piece where I select which command I want to follow:
Code:
protected void doSomething(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName.ToLower())
            {
                case "delete":
deleteOfferte(int.Parse(dgOffertes.DataKeys[e.Item.ItemIndex].ToString()));
                    break;
                case "select":
                    if(e.CommandArgument.ToString() != "noRedirect")
                        Response.Redirect("offerteinvoer.aspx?offerteID=" + dgOffertes.DataKeys[e.Item.ItemIndex]);
                    break;
            }
        }
What am I misunderstanding or doing wrong? It's driving me nuts.
 
Old December 17th, 2010, 06:00 AM
Authorized User
 
Join Date: Nov 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is there no one that can offer a solution to my problem? I really need one :(
 
Old December 17th, 2010, 06:23 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ferdy,

Can you show us the relevant code for the Delete button from the browser's HTML?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 17th, 2010, 07:35 AM
Authorized User
 
Join Date: Nov 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I hope it comes out somewhat readable, it looks like a mess in firefox. Anyway, here is the HTML generated for a single row, is this what you meant?

Code:
<tr onMouseOver="this.className = 'rowover'" onMouseOut="this.className = ''" onclick="__doPostBack('ctl00$ContentPlaceHolder1$dgOffertes$ctl03$ctl00','0')">
			<td class="hidden">82</td><td class="gridRow"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$dgOffertes$ctl03$ctl00','')">102</a></td><td class="gridRow"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$dgOffertes$ctl03$ctl01','')">test 3</a></td><td class="gridRow"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$dgOffertes$ctl03$ctl02','')">testerdam</a></td><td class="gridRow"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$dgOffertes$ctl03$ctl03','')">6-3-2010</a></td><td class="gridRow">

                            <input type="submit" name="ctl00$ContentPlaceHolder1$dgOffertes$ctl03$btnDelete" value="Delete" onclick="confirm_delete();" id="ctl00_ContentPlaceHolder1_dgOffertes_ctl03_btnDelete" />
                        </td>
		</tr>
 
Old December 17th, 2010, 07:39 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Take a look at this:

Code:
 
  <input type="submit" name="ctl00$ContentPlaceHolder1$dgOffertes$ctl03$btnDelete" value="Delete" onclick="confirm_delete();"
This doesn't line up with the code you posted earlier. There you assigned "return confirm_delete();" instead of just "confirm_delete();"

That means if you click cancel, the click is not cancelled and the page still posts back...

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 17th, 2010, 09:39 AM
Authorized User
 
Join Date: Nov 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

If I change the assign to simply "confirm_delete()" it sadly still does not work as another problem pops up. Instead of redirecting, it now enters the code for the delete action. Which it shouldn't, as my click should be cancelled.

I'm getting so confused here :(
It seemed like such a simple thing to do.
 
Old December 17th, 2010, 09:56 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

No, it's the other way around. You need to *add* return, in order for the delete button to cancel the click action.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 17th, 2010, 10:02 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

BTW, instead doing this programmatically, why not simply use OnClientClick?

Code:
OnClientClick="if (!confirm('Are you sure?')) { return false }"
Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 17th, 2010, 11:59 AM
Authorized User
 
Join Date: Nov 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm probably still doing something wrong, but it's not working. Neither with the onclientclick, nor by adding it programmatically. Yet in both cases I now get this HTML output:

Code:
<input type="submit" id="ctl00_ContentPlaceHolder1_dgOffertes_ctl03_btnDelete" onclick="confirm_delete();return false;" value="Delete" name="ctl00$ContentPlaceHolder1$dgOffertes$ctl03$btnDelete">
 
Old December 17th, 2010, 12:26 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

This doesn't make sense:
Code:
 
 
onclick="confirm_delete();return false;"
This way, the delete never firer as you always return false. In my case, false is only returned when you don't confirm. If with my code the page still posts back when you cancel, there must be something in your code that you're not showing us.

Did you try the OnClientClick I proposed and deleted your own code that does it programmatically?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Button on a DataGrid Ric_H ADO.NET 1 March 23rd, 2006 06:33 PM
Select Button on a DataGrid Ric_H C# 0 March 22nd, 2006 04:14 PM
Update Button in DataGrid Bibin .NET Framework 1.x 1 January 28th, 2006 08:22 AM
Button in Datagrid rodmcleay C# 1 September 25th, 2005 06:13 AM
button on a datagrid narbones ASP.NET 1.x and 2.0 Application Design 0 June 17th, 2003 07:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.