ASP.NET 1.1As 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 .
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:
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'.
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?
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'> </td>
<td><a href="add_costs.aspx">Add Cost Item</a></td>
</tr>
<tr>
<td width='4'> </td>
<td><a href="edit_costs_pick.aspx">Edit Cost Item</a></td>
</tr>
<tr>
<td width='4'> </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"> 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>
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.
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
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