Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 17th, 2005, 06:32 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding a delete link to each record in a table

Hi,

I've got a table laid out and the last column is a graphic I want users to be able to click on when they want to delete that row. Since it is a delete action, I'd also like to popup a quick 'Are you sure?' dialog box.

I'm pretty new to ASP.NET and what I can't figure out initially is how to code the <img> code to fire my sub proc that will do the delete... I tried the following:

Code:
<img src="../images/delete.GIF" border="0" onClick="Delete_Click(<%# Container.DataItem( "env_id" ) %>);">
So Delete_Click is my sub proc that will do the delete -- very simple, just deletes where the primary key is equal to env_id, which is supposed to get pulled out of my SQL datareader. I tried making Delete_Click a function instead of a sub proc if the problem was passing the variable and it still didn't work, but I can't see what's wrong exactly, just when I click that link I get 'Object expected'.

Any help?

Pat
  #2 (permalink)  
Old January 18th, 2005, 09:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Can you post your code?

Also, this might help...
http://msdn.microsoft.com/library/de...istcontrol.asp


  #3 (permalink)  
Old January 19th, 2005, 05:47 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There's a lot of code, so I'll post what I think are the relevant parts. Here is my repeater control code:

Code:
<AlternatingItemTemplate>
<tr style="background-color:#F0F0F0">
<td height="17"><div align="center"><img src="../images/delete.GIF" border="0" onClick="Delete_Click(<%# Container.DataItem( "env_id" )%>);"></div></td>
<td><div align="center"><%# FormatDate(Container.DataItem( "cost_date" )) %></div></td>
<td><div align="center"><%# Container.DataItem( "task_desc_env" ) %></div></td>
<td><div align="left"><%# Container.DataItem( "description" ) %></div></td>
<td><div align="center"><%# Format2Decs(Container.DataItem( "units" ).ToString()) %></div></td>
<td><div align="center"><%# Format2Decs(Container.DataItem( "cost_per" ).ToString()) %></div></td>
<td><div align="right"><%# AddBling(Container.DataItem( "cost" )) %></div></td>    
</tr>
</AlternatingItemTemplate>
So it's the very first item in the list, where I've got an image I'm putting in that field and I wanted to set that images onClick property to point at the function Delete_Click, and pass along the data from the datareader as a parameter for Delete_Click to work with. I haven't got the code for Delete_Click written yet, I've just got a little test function that should take the same parameter and spit out a little text...

Code:
Function Delete_Click(string env_id)
Dim strTest
strTest = env_id
Response.Write( "I want to delete record: " & strTest)
End Function
When I click on the image that should lauch Delete_Click I just get an error: Object expected. Any thoughts on what I'm missing?

Pat
  #4 (permalink)  
Old January 20th, 2005, 09:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Can you post your HTML code?

I think you need to add Delete_Click and simply do an alert when the image is clicked.

  #5 (permalink)  
Old January 20th, 2005, 12:10 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the whole thing... on the repeater control my alternating and normal templates have different code to try to call that Delete_Click function, neither works of course...

Thanks!

Code:
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Direct Costs Administration</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<Link REL="stylesheet" Type="text/css" HREF="/apps/styles/ows.css">
<Link REL="stylesheet" Type="text/css" HREF="/apps/styles/env.css">
<style type="text/css">
<!--
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
    background-image: url(/apps/images/main_bg.jpg);
}
-->
</style>
<Script Runat="Server">
Dim Amount As Decimal
Dim Total As Decimal = 0
Dim BDate As String

Sub Page_Load
        If Not Page.IsPostBack Then
            Dim conTSE as SqlConnection
            Dim project_number

            project_number = Request.QueryString("proj_num")
            txtProjectNumber.Text = project_number

            conTSE = New SqlConnection("server=ENVWDB;uid=web;pwd=xzalr62yyp;database=TimesheetExpress;")
            conTSE.Open()

            Dim    cmdOldCosts    As SqlCommand
            Dim dtrOldCosts As SqlDataReader

            cmdOldCosts = New SqlCommand( "SELECT * FROM direct_costs WHERE proj_no = '" & project_number & "' ORDER BY cost_date DESC", conTSE )
            dtrOldCosts = cmdOldCosts.ExecuteReader()

            rptOldCosts.DataSource = dtrOldCosts
            rptOldCosts.DataBind()

            dtrOldCosts.Close()
            conTSE.Close()
        End If
End Sub

Function Delete_Click(string env_id)
    Dim strTest
    strTest = env_id
    Response.Write( "I want to delete record: " & strTest)
End Function


Function GetTotal()
  Return FormatCurrency(Total)
End Function

Function FormatDate(ADate as String)
  BDate = ADate
  Return BDate
End Function

Function AddBling(Cost As Decimal)
  Total += Cost
  Return FormatCurrency(Cost)
End Function

Function Format2Decs(x As String)
  If Len(x)=0 Then
      Return ""
  Else
    Return Decimal.Parse(x).ToString( "f" )
  End If
End Function


</SCRIPT>
</head>
<%
dim project_number
project_number = Request.QueryString("proj_num")
%>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="ms-main">
  <tr height="31">
    <td width="150" valign="top" background="/apps/images/top_blue_gold_bar.jpg"><img src="/apps/images/top_left_sp_icon.jpg"></td>
    <td valign="middle" align="right" style="padding-right: 7px" class="ms-banner" background="/apps/images/top_blue_gold_bar.jpg"><a href="http:\\envwdb\hr\">Up to Environics HR Web</a></td>
  </tr>
  <tr>
    <td><img src="/apps/images/tse_icon.jpg"></td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <tr><td width='6'><img src="/apps/images/spacer.gif" height="1" width="6"></td><td class="ms-titlearea">Environics HR Web</td></tr>
        <tr><td width='6'><img src="/apps/images/spacer.gif" height="1" width="6"></td><td class="ms-pagetitle">Direct Costs Administration</td></tr>
      </table>
    </td>
  </tr>
  <tr height="1">
    <td background="/apps/images/gold_bar_1x1.jpg" colspan="2"><img src="/apps/images/spacer.gif" height="1" width="1"></td>
  </tr>
  <tr>
    <td colspan="2">
      <table class="ql-header" cellpadding="4">
        <tr height="1"><td colspan="2"><img src="/apps/images/spacer.gif" height="1" width="1"></td></tr>
        <tr>
          <td width='5'>&nbsp;</td>
          <td><a href="add_costs.aspx">Add Cost Item</a></td>
        </tr>
        <tr>
          <td width='4'>&nbsp;</td>
          <td><a href="edit_costs_pick.aspx">Edit Cost Item</a></td>
        </tr>
        <tr>
          <td width='4'>&nbsp;</td>
          <td><a href="delete_costs_pick.aspx">Delete Cost Item</a></td>
        </tr>
      </table>
    </td>
  </tr>
</table>

<div id="gohome" style="position:absolute;left:30px;top:9px">
<p class="ms-banner"><a href="http:\\envwdb">Intranet Home</a></p>
</div>

<div id="main" style="position:absolute;left:157px;top:93px">
<table width="800" class="ms-vb" cellpadding="0" cellspacing="0" border="0">
  <tr height="17">
    <td valign="middle" background="../images/title_shading.jpg" class="ql-header">&nbsp;Delete Costs For <%=project_number%></td>
  </tr>
</table>
<form runat="server">
<table width="800" class="ms-vb" cellpadding="5" cellspacing="0" border="0">
  <tr>
    <td colspan="3">A full listing of current costs associated with this project is shown below.  Click the 'Delete' link beside any cost to edit it.</td>
  </tr>
  <tr>
    <td colspan="3" align="left">
        <asp:Repeater ID="rptOldCosts" runat="server">
                    <headertemplate>
                      <table border="1" style="border-collapse:collapse" font-name="verdana" Font-Size="8pt">
                        <tr style="background-color:#E1EDFD; color:#FFFFFF">
                          <th class="ql-header" align="center" width="80" height="17">Delete Cost</th>
                          <th class="ql-header" align="center" width="100">Cost Date</th>
                          <th class="ql-header" align="center" width="100">Expense Code</th>
                          <th class="ql-header" align="center" width="250">Description</th>
                          <th class="ql-header" align="center" width="80">Hours</th>
                          <th class="ql-header" align="center" width="80">Cost/Hour</th>
                          <th class="ql-header" align="center" width="80">Cost</th>

                        </tr>
                      </HeaderTemplate>

                    <itemtemplate>
                      <tr>
                        <td height="17"><div align="center"><a href='<%# "edit_this_cost.aspx?project=" & txtProjectNumber.Text & "id=" & Container.DataItem( "env_id" ) %>'><img src="../images/delete.GIF" border="0"></a></div></td>
                        <td><div align="center"><%# FormatDate(Container.DataItem( "cost_date" )) %></div></td>
                        <td><div align="center"><%# Container.DataItem( "task_desc_env" ) %></div></td>
                        <td><div align="left"><%# Container.DataItem( "description" ) %></div></td>
                        <td><div align="center"><%# Format2Decs(Container.DataItem( "units" ).ToString()) %></div></td>
                        <td><div align="center"><%# Format2Decs(Container.DataItem( "cost_per" ).ToString()) %></div></td>
                        <td><div align="right"><%# AddBling(Container.DataItem( "cost" )) %></div></td>

                      </tr>
                    </itemtemplate>

                    <AlternatingItemTemplate>
                      <tr style="background-color:#F0F0F0">
                        <td height="17"><div align="center"><img src="../images/delete.GIF" border="0" onClick="Delete_Click(<%# Container.DataItem( "env_id" )%>);"></div></td>
                        <td><div align="center"><%# FormatDate(Container.DataItem( "cost_date" )) %></div></td>
                        <td><div align="center"><%# Container.DataItem( "task_desc_env" ) %></div></td>
                        <td><div align="left"><%# Container.DataItem( "description" ) %></div></td>
                        <td><div align="center"><%# Format2Decs(Container.DataItem( "units" ).ToString()) %></div></td>
                        <td><div align="center"><%# Format2Decs(Container.DataItem( "cost_per" ).ToString()) %></div></td>
                        <td><div align="right"><%# AddBling(Container.DataItem( "cost" )) %></div></td>

                      </tr>
                    </AlternatingItemTemplate>

                    <FooterTemplate>
                      <tr align="right">
                        <th colspan="6" style="background-color:#F0F0F0; color:#333333" height="17">Total:</th>
                        <td><b><%# GetTotal() %></b></td>
                      </tr>
                    </table>
                    </FooterTemplate>
                    </asp:Repeater>
    </td>
  </tr>
</table> 
<asp:TextBox ID="txtProjectNumber" runat="server" visible="false"/> 
</form>
</div>
</body>
</html>
  #6 (permalink)  
Old January 25th, 2005, 06:42 PM
Authorized User
 
Join Date: Jan 2005
Location: Kolkata, West Bengal, India.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

just Try the following:

<img src="../images/delete.GIF" border="0" runat="server" onClick="Delete_Click(<%# Container.DataItem( "env_id" ) %>)">

Actually to fire any server side event or any server side method your control must include ' runat="server" '.




Hope It Helps !!
Bhaskar Ghosh
  #7 (permalink)  
Old January 27th, 2005, 10:10 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The repeater control doesn't have a DataKeys collection like the DataGrid so you have to come up with something a little different to get at the data you are binding to the control.

The way I would do something like this is like this:
1. Create a literal control inside the item template. Set the text of this control to the key of your data.
2. Create some server control in the template(s). Use anything you want that will generate a postback. Any postback event that occurs inside a repeater will fire off the Repeater.ItemCommand event.

<asp:repeater id="Repeater1" runat="server">
    <itemtemplate>
        <asp:button runat="server" text="Click Me" />
        <asp:literal runat="server" id="litUserID" text='<%# DataBinder.Eval(Container.DataItem, "USER_ID") %>' />
    </itemtemplate>
</asp:repeater>

3. In the ItemCommand event handler, find the literal control in the repeater item and get the text.
4. Perform your server side functions with the key that you pulled from the literal text.

Private Sub Repeater1_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs ) Handles Repeater1.ItemCommand
    Response.Write(CType(e.Item.FindControl("litUserID "), Literal).Text)
End Sub


-Peter
  #8 (permalink)  
Old January 27th, 2005, 11:25 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks guys, I'll play with those -- literal controls are new to me but I'll do some reading and experiment with that...
  #9 (permalink)  
Old May 25th, 2005, 02:33 PM
Authorized User
 
Join Date: Dec 2004
Location: New Smyrna Beach, Florida, USA.
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to datagram Send a message via AIM to datagram
Default

Okay, Im not getting mine to work. Here is my code:


<ItemTemplate>
<tr>
<td>
<asp:Button id="Button2" runat="server" Font-Size="XX-Small" Font-Names="Verdana" ForeColor="White"
BackColor="#1E3C7B" Height="18px" Width="60px" Font-Bold="True" BorderColor="#CBD8EB" BorderStyle="Outset"
BorderWidth="2px" Text="Delete"></asp:Button>
<asp:Literal Runat=server ID="moo" Text='<%# DataBinder.Eval(Container.Dataitem, "labor_id") %>' />
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "l_employee") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "l_hours") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "l_rate") %>
</td>
</tr>
</ItemTemplate>




    Private Sub Repeater1_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs ) Handles Repeater1.ItemCommand
        Dim hello As String = (CType(e.Item.FindControl("moo"), Literal).Text)
        TextBox1.Text = hello

    End Sub

 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to do record count for Link table in access ayazhoda Access VBA 0 April 16th, 2008 05:43 AM
Delete a record row, not just the record. Coby Access VBA 1 April 30th, 2007 07:29 AM
how can i delete records by clicking a link umair_rathore Classic ASP Databases 2 March 15th, 2005 08:46 AM
Trying to delete a record... Can't do it... lguzman Access VBA 11 August 13th, 2004 01:41 PM
Delete record but how? scifo Beginning PHP 3 August 1st, 2003 01:14 PM



All times are GMT -4. The time now is 10:29 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.