Question

Datagrid problem

Asked by: TheInnovator

I created a gridview that renders 5 columns to a page.
In the last column, the data that is rendered is based on
data from a column data in a table.  Also, I want the last column to be
a link as a querystring.  I'm a little confused b/c my codebehind
is on a different page.  In PHP, b/c the script and html were on the
same page, it was easier.  Below I have PHP code and I just want to
do the same in ASP.NET.  I also have the ASP.NET and my codebehind (C#) below.

PHP
------
  <TR style="font-size:12px;font-family:arial;background-color:<?php echo $row_color; ?>">
	  	  	<TD><?php echo $row["medDte"]; ?></TD>
	  	  	<TD><?php echo getMediaName($row["medType"]); ?></TD>
	  	  	<TD><?php echo $row["medCopy"]; ?></TD>
	  	  	<TD><?php echo $row["medPurpose"]; ?></TD>
	  	  	<TD align="center">
	  	  	<?php
	  	  		//echo "copies: ".$row["medCopy"];
	  	  		if ($row["medCopy"] > 1)
	  	  		{
	  	  			echo "<a href='mediaDetail.php?medid=".$row["medID"]."&reqID=".$row["ReqID"]."'>Detail</a>";
	  	  		}
	  	  		else
	  	  		{
	  	  			$sql = "SELECT
	  	  						dbo.tblRequestor.ReqID AS Expr1,
	  	  						dbo.tblRequestor.pid,
	  	  						dbo.tblMediaTracker.statusID AS medStatusID,
	  	  						dbo.tblMediaTracker.MediaID,
	  	  						dbo.tblMediaTracker.MediaIDNumber As medIDNumber,
	  	  						dbo.tblMediaTracker.mediaTrackID as mediaTrackID
							FROM
								dbo.tblMediaTracker INNER JOIN
                      			dbo.tblRequestor ON dbo.tblMediaTracker.reqID = dbo.tblRequestor.ReqID WHERE dbo.tblRequestor.pid=".$_SESSION["pid"]." AND dbo.tblMediaTracker.MediaID=".$row["medID"];
                      			//echo $sql;
					$results = mssql_query($sql);
					if ($rows=mssql_fetch_array($results))
					{
						if ($rows["medStatusID"] == 4)
						{
							echo "<a href='cancelRequest.php?trackId=".$rows["mediaTrackID"]."&type=single&medid=".$row["medID"]."'><img border=0 src='image/delete.gif' alt='Delete Request'></a>";
						}
						elseif ($rows["medStatusID"] != 3)
						{
							echo "<A HREF=javascript:popUp('statusPop.php?trackId=".$rows["mediaTrackID"]."&statusId=".$rows["medStatusID"]."&typed=".$row["medType"]."&cnt=(1)')>".getStatusName($rows["medStatusID"])."</a>";
							echo "<br><span style=font-size:10px;font-weight:bold>Media ID #:".$rows["medIDNumber"]."</span>";
						}
						else
						{
							echo "<span style='font-style:italics;color:red'>DESTROYED</span>";
						}
					}
	  	  		}
	  	  	?>
	  	  </TD>
	  	  <!-- <TD><a HREF=javascript:popUp('statusPop.php')>Cancel</a></TD> -->
	  	  </TR>
 
 
 
 
 
 
ASP.NET
----------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="myRequest.aspx.cs" Inherits="RequestMedia.myRequest" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <link href="mediaCSS.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
	<!-- ****************** BEGIN OUTER TABLE ****************** -->
	<table width="95%" cellpadding="0" cellspacing="0" border="0" align="center">
	  <tr>
	    <td width="15%" bgcolor="#ffffff" valign="top">
	        <div id="navcontainer">
                <ul id="navlist">
                    <li><a href="default.aspx" id="newNavMed" style="">New Media Request</a></li>
                    <li><a href="#" id="navReq" style="">My Media Requests</a></li>
                </ul>
            </div>
	    </td>
	    <td bgcolor="#dddddd">
	    <!-- ****************** BEGIN 1ST INNER TABLE ****************** -->
	  	<table width="100%" cellpadding="3" cellspacing="1" border="0">
	  	  <tr>
			  <td class="tblHeading" colspan="2">New Media Request</td>
		  </tr>
		  <tr>
			  <td class="formFields" colspan="2">    
                <asp:GridView ID="myRequestGridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="false" Width="100%">
                <Columns>
                    <asp:BoundField DataField="medDte" HeaderText="Request Date" HeaderStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="medType" HeaderText="Type" HeaderStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="medCopy" HeaderText="# of Media" HeaderStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="medPurpose" HeaderText="Purpose" HeaderStyle-HorizontalAlign="Left" />      
                    <asp:templatefield>
                        <ItemTemplate>
                            <label id="mediaLink"><a href="requestDetail.aspx?">Detail</a></label>
                        </ItemTemplate>
                    </asp:templatefield>    
                </Columns>
                </asp:GridView>
              </td>
            </tr>
        </table>
        </td>
    </tr>
    </table>
    </form>
</body>
</html>
 
Code Behind
---------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
 
namespace RequestMedia
{
    public partial class myRequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["toucanMedia"].ConnectionString;
            conn.Open();
 
            SqlCommand cmd = new SqlCommand("getUserMedID", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@pid", 1208));
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
 
            DataSet media = new DataSet();
            adapter.SelectCommand = cmd;
            adapter.Fill(media);
 
            myRequestGridView1.DataSource = media;
            myRequestGridView1.DataBind();
 
            conn.Close();
        }
 
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
    }
}

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-09-29 at 11:36:00ID24771121
Tags

ASP.NET

Topics

Programming for ASP.NET

,

C# Programming Language

Participating Experts
2
Points
500
Comments
22

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. localmachinename\ASPNET
    Could someone give me a description of the ASPNET user or direct me to a link? Thanks
  2. DATAGRID EDITING / CUSTOMIZATION ASP.NET / VB.…
    Hi all. I have a datagrid that I need to dispaly values in. More specifically, it will list a students in a class that a professor teaches along with their grades. Here is the catch: I want the grades to appear in a dropdownlist box with the database grade value already sele...
  3. Gridview - populating dropdownlist in codebehind
    hi Guys, I am having a nightmare with this, i really hate using gridviews. Anyway, basically i have a dropdown list in a gridview, which i want to bind to a datasource and populate it from codebehind (this allows me to populate it from a file listing). I have tried to popula...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: locke_aPosted on 2009-09-29 at 11:46:15ID: 25451999

In your item template you can call a method that will evaluate the data and return a string, like:

<itemTemplate>
<%#GetMediaLink(Eval("medStatusID"))%>
</itemTemplate>


Then in your code behind:

public string GetMediaLink(string statusID)
{
   //Do Whatever if statements etc
   return "<a href=...";
}

 

by: robkolskyPosted on 2009-09-29 at 11:47:44ID: 25452012

<label id="mediaLink"><a href="requestDetail.aspx?somevariable=<%# DataBinder.Eval (Container.DataItem, "somefield") %>">Detail</a></label>

That should place the value for that row for that field exactly where you put it in the itemtemplate

 

by: TheInnovatorPosted on 2009-09-29 at 12:13:33ID: 25452296

I'm still sort of confused b/c the code behind concept is new to me.

The last column can read "Cancel", "Delete", or "Archived" based on the data from the dtabase table.
What would the code look like?
Thx.

 

by: locke_aPosted on 2009-09-29 at 12:31:20ID: 25452501

I don't know what your values are, but something like:

public string GetMediaLink(string statusID)
{
   string returnValue = "";
   switch(statusID)
   {
      case "1":
         returnValue = "<a href=\"somepage.aspx\">Cancel</a>";
         break;
      case "2":
         returnValue = "<a href=\"somepage.aspx\">Delete</a>";
         break;
   }
 
   return returnValue;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:

Select allOpen in new window

 

by: TheInnovatorPosted on 2009-09-30 at 05:35:18ID: 25457991

OK.  I did a little bit more research and used a dataTable because I needed the id in my queryString, so I modified my code some and got stuck.

How can I use the information in my dataTable (drInsert["ReqID"],drInsert["medID"],etc.) and use it in the queryString that's in the GetStatusLink()?

Also, I get a blue squigly linke under "foreach" that says, "foreach statement cannot operate on variables of type 'System.Data.DataTable' becuase 'System.Data.DataTable' does not contain a public definition for 'GetEnumerator'".

How do I resolve that?

Thanks.

ASP.NET CODE
------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="myRequest.aspx.cs" Inherits="RequestMedia.myRequest" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <link href="mediaCSS.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
	<!-- ****************** BEGIN OUTER TABLE ****************** -->
	<table width="95%" cellpadding="0" cellspacing="0" border="0" align="center">
	  <tr>
	    <td width="15%" bgcolor="#ffffff" valign="top">
	        <div id="navcontainer">
                <ul id="navlist">
                    <li><a href="default.aspx" id="newNavMed" style="">New Media Request</a></li>
                    <li><a href="#" id="navReq" style="">My Media Requests</a></li>
                </ul>
            </div>
	    </td>
	    <td bgcolor="#dddddd">
	    <!-- ****************** BEGIN 1ST INNER TABLE ****************** -->
	  	<table width="100%" cellpadding="3" cellspacing="1" border="0">
	  	  <tr>
			  <td class="tblHeading" colspan="2">New Media Request</td>
		  </tr>
		  <tr>
			  <td class="formFields" colspan="2">    
                <asp:GridView ID="myRequestGridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="false" Width="100%">
                <Columns>
                    <asp:BoundField DataField="medDte" HeaderText="Request Date" HeaderStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="medType" HeaderText="Type" HeaderStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="medCopy" HeaderText="# of Media" HeaderStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="medPurpose" HeaderText="Purpose" HeaderStyle-HorizontalAlign="Left" />      
                    <asp:templatefield>
                        <ItemTemplate>
                            <% GetStatusLink(Eval("medCopy").ToString()); %>
                        </ItemTemplate>
                    </asp:templatefield>    
                </Columns>
                </asp:GridView>
              </td>
            </tr>
        </table>
        </td>
    </tr>
    </table>
    </form>
</body>
</html>
 
 
CODE BEHIND
-----------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
 
namespace RequestMedia
{
    public partial class myRequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["toucanMedia"].ConnectionString;
            conn.Open();
 
            SqlCommand cmd = new SqlCommand("getUserMedID", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@pid", 1208));
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
 
            DataSet media = new DataSet();
            adapter.SelectCommand = cmd;
            adapter.Fill(media);
 
            DataTable dtMedia = new DataTable();
            dtMedia.Columns.Add("medDte");
            dtMedia.Columns.Add("medType");
            dtMedia.Columns.Add("medCopy", typeof(int));
            dtMedia.Columns.Add("medPurpose");
            dtMedia.Columns.Add("statusLink");
 
            foreach (DataRow dr in media.Tables[0])
            {
                DataRow drInsert = dtMedia.NewRow();
                drInsert["medDte"] = dr["medDte"];
                drInsert["ReqID"] = dr["ReqID"];
                drInsert["medType"] = dr["medType"];
                drInsert["medCopy"] = dr["medCopy"];
                drInsert["medPurpose"] = dr["medPurpose"];
                drInsert["medID"] = dr["medID"];
                drInsert["statusLink"] = GetStatusLink(dr["medCopy"].ToString());
            }
            
            myRequestGridView1.DataSource = media;
            myRequestGridView1.DataBind();
 
            conn.Close();
        }
 
        public string GetStatusLink(string statusID)
        {
            string returnValue = "";
            switch (statusID)
            {
                case "1":
                    returnValue = "<a href=\"requestDetail.aspx?medid=\">Cancel</a>";
                    break;
                case "2":
                    returnValue = "<a href=\"requestDetail.aspx\">Delete</a>";
                    break;
            }
 
            return returnValue;
        }
 
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
    }
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:

Select allOpen in new window

 

by: locke_aPosted on 2009-09-30 at 07:00:27ID: 25458901

FYI.  The foreach message your getting is because a DataTable isn't a collection of rows, a DataTable HAS a collection of rows that you could itterate though like:

foreach (DataRow dr in media.Tables[0].Rows)
{
...
}

I don't believe this is how you want to do it though.  I'll re-work some code and post it in a bit.

 

by: locke_aPosted on 2009-09-30 at 07:17:36ID: 25459085

Try something more like:

public partial class myRequest : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                DataSet media = new DataSet();
 
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["toucanMedia"].ConnectionString))
                {
                    conn.Open();
 
                    using (SqlCommand cmd = new SqlCommand("getUserMedID", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@pid", 1208));
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                        
                        adapter.SelectCommand = cmd;
                        adapter.Fill(media);
                    }
 
                    conn.Close();
                }
 
                myRequestGridView1.DataSource = media;
                myRequestGridView1.DataBind();
            }
        }
 
        protected void myRequestGridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
 
        public string GetStatusLink(string reqId, string medId)
        {
            string returnValue = "";
            switch (reqId)
            {
                case "1":
                    returnValue = string.Format("<a href=\"requestDetail.aspx?medid={0}\">Cancel</a>", medId);
                    break;
                case "2":
                    returnValue = "<a href=\"requestDetail.aspx\">Delete</a>";
                    break;
            }
 
            return returnValue;
 
        }
   }

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:

Select allOpen in new window

 

by: locke_aPosted on 2009-09-30 at 07:18:51ID: 25459093

You don't need to loop through the rows in your table because the gridview will do that for you when you databind it.

If you want more values from your datarow in your getlink, you add the parameters in your code-behind and add them to the call in the datagrid as I have shown in the example above.

 

by: TheInnovatorPosted on 2009-09-30 at 07:52:01ID: 25459546

It says I'm missing an assembly.  Below are the errors

Error      5      The type or namespace name 'Page' could not be found (are you missing a using directive or an assembly reference?)      D:\Documents and Settings\My Documents\Visual Studio 2008\Projects\RequestMedia\RequestMedia\myRequest.aspx.cs      80      34      RequestMedia



Error 6 The type or namespace name 'EventArgs' could not be found (are you missing a using directive or an assembly reference? D:\Documents and Settings\My Documents\Visual Studio 2008\Projects\RequestMedia\RequestMedia\myRequest.aspx.cs      110      75      RequestMedia

 

by: locke_aPosted on 2009-09-30 at 08:03:23ID: 25459667

You should have only replaced the class portion of your code-behind.  The error you get is because you don't have the appropriate using statements, which indicates that you may have replaced the whole file with the contents I posted.

Sorry I wasn't clear about where to place the code...

 

by: locke_aPosted on 2009-09-30 at 08:04:04ID: 25459672

The class Page is in System.Web.UI and the EventArgs are in System.

 

by: TheInnovatorPosted on 2009-09-30 at 10:07:32ID: 25460944

Ok.  I see. not your fault...should have known.

Anyway, now I get a compilation error on line 39
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1501: No overload for method 'GetStatusLink' takes '1' arguments

Source Error:

 

Line 37:                     <asp:templatefield>
Line 38:                         <ItemTemplate>
Line 39:                             <% GetStatusLink(Eval("medCopy").ToString()); %>
Line 40:                         </ItemTemplate>
Line 41:                     </asp:templatefield>    
 


 

by: locke_aPosted on 2009-09-30 at 10:23:44ID: 25461110

Oh, right.  That one is my bad:

<% GetStatusLink(Eval("reqID").ToString(), Eval("medID").ToString()); %>

This is the change you'd make if you wanted to add other parameters as well.

 

by: TheInnovatorPosted on 2009-10-01 at 06:43:40ID: 25468517

I added the change and now I get
"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."  How do you bind an <itemTemplate> ?

 

by: locke_aPosted on 2009-10-01 at 08:05:34ID: 25469496

The ItemTemplate is not databound.  The ItemTemplate is within the GridView which is databound.

<asp:GridView ID="myRequestGridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="false" Width="100%">
                <Columns>
                    <asp:BoundField DataField="medDte" HeaderText="Request Date" HeaderStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="medType" HeaderText="Type" HeaderStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="medCopy" HeaderText="# of Media" HeaderStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="medPurpose" HeaderText="Purpose" HeaderStyle-HorizontalAlign="Left" />      
                    <asp:templatefield>
                        <ItemTemplate><% GetStatusLink(Eval("reqID").ToString(), Eval("medID").ToString()); %></ItemTemplate>
                    </asp:templatefield>    
                </Columns>
                </asp:GridView>
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window

 

by: locke_aPosted on 2009-10-01 at 08:06:19ID: 25469503

Can you post your code as you have it currently, I suspect in the course of copying and pasting partial examples we may have missed something.

Thanks!

 

by: TheInnovatorPosted on 2009-10-01 at 14:04:16ID: 25473219

Here you go:

ASP.NET
-------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="myRequest.aspx.cs" Inherits="RequestMedia.myRequest" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <link href="mediaCSS.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
	<!-- ****************** BEGIN OUTER TABLE ****************** -->
	<table width="95%" cellpadding="0" cellspacing="0" border="0" align="center">
	  <tr>
	    <td width="15%" bgcolor="#ffffff" valign="top">
	        <div id="navcontainer">
                <ul id="navlist">
                    <li><a href="default.aspx" id="newNavMed" style="">New Media Request</a></li>
                    <li><a href="#" id="navReq" style="">My Media Requests</a></li>
                </ul>
            </div>
	    </td>
	    <td bgcolor="#dddddd">
	    <!-- ****************** BEGIN 1ST INNER TABLE ****************** -->
	  	<table width="100%" cellpadding="3" cellspacing="1" border="0">
	  	  <tr>
			  <td class="tblHeading" colspan="2">New Media Request</td>
		  </tr>
		  <tr>
			  <td class="formFields" colspan="2">    
                <asp:GridView ID="myRequestGridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="false" Width="100%">
                <Columns>
                    <asp:BoundField DataField="medDte" HeaderText="Request Date" HeaderStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="medType" HeaderText="Type" HeaderStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="medCopy" HeaderText="# of Media" HeaderStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="medPurpose" HeaderText="Purpose" HeaderStyle-HorizontalAlign="Left" />      
                    <asp:templatefield>
                        <ItemTemplate>
                            <% GetStatusLink(Eval("reqID").ToString(), Eval("medID").ToString()); %>
                        </ItemTemplate>
                    </asp:templatefield>    
                </Columns>
                </asp:GridView>
              </td>
            </tr>
        </table>
        </td>
    </tr>
    </table>
    </form>
</body>
</html>
 
 
CODE BEHIND
-------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
 
namespace RequestMedia
{
public partial class myRequest : Page
    {
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet media = new DataSet();
 
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["toucanMedia"].ConnectionString))
                {
                    conn.Open();
 
                    using (SqlCommand cmd = new SqlCommand("getUserMedID", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@pid", 1208));
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
 
                        adapter.SelectCommand = cmd;
                        adapter.Fill(media);
                    }
 
                    conn.Close();
                }
 
                myRequestGridView1.DataSource = media;
                myRequestGridView1.DataBind();
            }
        }
 
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
 
        public string GetStatusLink(string reqId, string medId)
        {
            string returnValue = "";
            switch (reqId)
            {
                case "1":
                    returnValue = string.Format("<a href=\"requestDetail.aspx?medid={0}\">Cancel</a>", medId);
                    break;
                case "2":
                    returnValue = "<a href=\"requestDetail.aspx\">Delete</a>";
                    break;
            }
 
            return returnValue;
 
        }
    }
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:

Select allOpen in new window

 

by: locke_aPosted on 2009-10-01 at 14:09:08ID: 25473253

And, are you still getting an error when you run it; if so, what?

 

by: TheInnovatorPosted on 2009-10-02 at 06:22:52ID: 25477579

"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."

 

by: TheInnovatorPosted on 2009-10-02 at 06:23:49ID: 25477591

Also, the error points to this line

                     <ItemTemplate>
                            <% GetStatusLink(Eval("reqID").ToString(), Eval("medID").ToString()); %>     <--------
                        </ItemTemplate>

 

by: locke_aPosted on 2009-10-05 at 13:37:49ID: 25499454

You might try changing the line to read:

<% GetStatusLink(DataBinder.Eval(Container.DataItem,"reqID").ToString(),DataBinder.Eval(Container.DataItem,"medID").ToString()); %>

And see if it makes a difference.

http://aspadvice.com/blogs/joteke/archive/2008/08/24/Is-_2200_Databinding-methods-such-as-Eval_280029002C00_-XPath_280029002C00_-and-Bind_28002900_-can-only-be-used-in-the-context-of-a-databound-control_2E002200_-causing-you-grief_3F00_.aspx

 

by: locke_aPosted on 2009-10-05 at 13:43:36ID: 25499516

Oh, I see... could be a simple syntax oversight.

Try adding a # after <%

<%# GetStatusLink(Eval("reqID").ToString(), Eval("medID").ToString()) %>

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...