473,378 Members | 1,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

access to image button in cs

1
Hi,
I'm sorry about my English.

I have this code in cs:
Expand|Select|Wrap|Line Numbers
  1. foreach (System.Data.DataRow oDataRow in oDataTable.Rows)
  2. {
  3.  
  4.   litContent.Text = litContent.Text +
  5.     string.Format("<div>"
  6.     + "<div class=" + "\"top\"" + ">"
  7.     + " <div class=\"comm_info\">"
  8.     + "<div class=\"comm_info_Title\">{0}</div>"
  9.     + "<div class=\"LikeDislike\">"
  10.     + "<div class=\"Count\">24</div>"
  11.     + "<div class=\"imgbutton\">"
  12.     + "<asp:imagebutton src=\"../Image/Like.png\" runat="\""server\" id=\"lnkLike\" onclick=\"lnkLike_OnClick\"/></div>"
  13.     + "<div class=\"imgbutton\">"
  14.     + "<asp:linkbutton id=\"lnkDislike\" runat="\""server\" cssclass=\"DislikeButton\" ></asp:linkbutton></div>"
  15.     + "<div class=\"Count\">34</div></div>"
  16.     + "<div class=\"fr_imgR\"></div>"
  17.     + "<div class=\"comm_info_content\">"
  18.     + "<div class=\"comm_info_name\">{0}</div>"
  19.     + "<span class=\"comm_sep\">|</span>"
  20.     + "<div class=\"comm_info_date\">{1}</div>"
  21.     + "</div><div class=\"fr_imgL\"></div></div>"
  22.     + "</p></div>"
  23.     + "<div class=" + "\"mid\"" + ">"
  24.     + "<div class=" + "\"in\"" + ">"
  25.     + "<div class=" + "\"body\"" + " style=" + "\"margin-bottom: 5px;\"" + ">"
  26.     + "{2}"
  27.     + "</div>"
  28.     + "</div>"
  29.     + "</div>"
  30.     + "<div class=" + "\"bottom\"" + ">"
  31.     + "</div>"
  32.     + "</div>", oDataRow["Title"].ToString(), System.Convert.ToDateTime(oDataRow["updatetime"]), oDataRow["Body"].ToString());
  33. }
But in the run time <asp:imagebutton.../> and <asp:linkbutton.../> don't work and it isn't seen and I can't click on it to run click function.

Please help me.

Thanks.
Nov 16 '11 #1
1 1741
Frinavale
9,735 Expert Mod 8TB
You cannot declare an ASP.NET control from within a String that you are applying to a Literal control on your page.

If you want to display data that you've retrieved from your database you need to use something like a Repeater, or GridView or something that can display this data.

These controls will bind to the underlying data source (that you've retrieved from your database) and will display the data in a way that you specify using a Template.

For example, in the following code I have a repeater defined in my ASP code for the page.

Notice how I have defined a template for how the data is going to be displayed for each row...notice how I have put your image button and link button into this template:
Expand|Select|Wrap|Line Numbers
  1.     <asp:Repeater ID="BookListing" runat="server">
  2.         <ItemTemplate>
  3.             <div class="top">
  4.                 <div class="comm_info">
  5.                     <div class="comm_info_Title">
  6.                         <%# DataBinder.Eval(Container.DataItem, "Title")%>
  7.                     </div>
  8.                     <div class="LikeDislike">
  9.                         <div class="Count">
  10.                             24
  11.                         </div>
  12.                         <div class="imgbutton">
  13.                             <asp:ImageButton runat="server" ID="lnkLike" OnClick="lnkLike_OnClick" AlternateText="Like" />
  14.                         </div>
  15.                         <div class="Count">
  16.                             34
  17.                         </div>
  18.                         <div class="imgbutton">
  19.                             <asp:LinkButton ID="lnkDislike" runat="server" CssClass="DislikeButton" Text="Dislike"> </asp:LinkButton>
  20.                         </div>
  21.                     </div>
  22.                     <div class="fr_imgR">
  23.                     </div>
  24.                     <div class="comm_info_content">
  25.                         <div class="comm_info_name">
  26.                             <%# DataBinder.Eval(Container.DataItem, "Title")%>
  27.                         </div>
  28.                         <span class="comm_sep">|</span>
  29.                         <div class="comm_info_date">
  30.                             <%# DataBinder.Eval(Container.DataItem, "updatetime")%>
  31.                         </div>
  32.                     </div>
  33.                     <div class="fr_imgL">
  34.                     </div>
  35.                 </div>
  36.             </div>
  37.             <div class="mid">
  38.                 <div class="in">
  39.                     <div class="body" style="margin-bottom: 5px;">
  40.                         <%# DataBinder.Eval(Container.DataItem, "Body")%>
  41.                     </div>
  42.                 </div>
  43.             </div>
  44.             <div class="bottom">
  45.             </div>
  46.         </ItemTemplate>
  47.     </asp:Repeater>
I am binding to the data in the DataTable using the <%#...%> syntax.

In my C# code I am simply setting the DataSource property of the Repeater to the data set containing the table which has the data to display in it. I then call the DataBind event in the Page PreRender event (because if you call it in the PagePreLoad event then you could possibly loose any event information about the Repeater)

Here is my C# code...in the method that creates the table you would call your database (I just don't have a database to call...)

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace WebApplication1
  9. {
  10.     public partial class WebForm1 : System.Web.UI.Page
  11.     {
  12.         private System.Data.DataSet oDataSet;
  13.         private System.Data.DataTable oDataTable;
  14.         protected void Page_Load(object sender, EventArgs e)
  15.         {
  16.             if (Session["oDataSet"] != null)
  17.             {
  18.                 oDataSet = (System.Data.DataSet)Session["oDataSet"];
  19.             }
  20.             else
  21.             {
  22.                 createDataTable();
  23.                 Session["oDataSet"] = oDataSet;
  24.             }
  25.  
  26.         }
  27.  
  28.         protected void lnkLike_OnClick(object sender, ImageClickEventArgs e)
  29.         {
  30.             //throw new NotImplementedException();
  31.         }
  32.  
  33.         protected void lnkDislike(object sender, EventArgs e)
  34.         {
  35.             //throw new NotImplementedException();
  36.         }
  37.         void Page_PreRender(object sender, EventArgs e)
  38.         {
  39.             BookListing.DataSource = oDataSet;
  40.             BookListing.DataBind();
  41.         }
  42.  
  43.         private void createDataTable()
  44.         {
  45.             oDataSet = new System.Data.DataSet();
  46.             oDataTable = new System.Data.DataTable();
  47.             oDataTable.Columns.Add("Title", typeof(string));
  48.             oDataTable.Columns.Add("Body", typeof(string));
  49.             oDataTable.Columns.Add("updatetime", typeof(DateTime));
  50.  
  51.  
  52.             for (int i = 0; i < 10; i++)
  53.             {
  54.                 System.Data.DataRow oDataRow = oDataTable.NewRow();
  55.                 string title = String.Format("{0}{1}", "Title", i);
  56.                 string body = string.Format("{0}{1}. {2}", "This is the body content for title ", i, "Lorem ipsum dolor sit amet, pede amet, tincidunt in nonummy scelerisque. Elementum nulla interdum ipsum erat lobortis mi. Gravida ut eu, non dolor vivamus velit eget sed lobortis, eu quis aenean pellentesque. Mi eleifend, potenti nec tempus nam a, donec congue convallis quisque aliquet nulla, integer nulla torquent fringilla. Cras maecenas fringilla in, viverra purus nulla massa maecenas tempor vitae, integer natoque aptent ut, quis vestibulum tincidunt lacinia per a, ante lorem odio rhoncus. Luctus rhoncus ligula a imperdiet. Eu nibh urna gravida tristique eleifend cras, aute class orci eros pede pharetra, amet phasellus bibendum dictum sollicitudin euismod magna, sodales dis. Turpis a ante nisl at dolor, feugiat iaculis at eu posuere ac imperdiet, gravida fringilla ante sed mollis, vestibulum aliquet quis at mauris molestie quis.");
  57.                 DateTime updatetime = DateTime.Now;
  58.                 oDataRow[0] = title;
  59.                 oDataRow[1] = body;
  60.                 oDataRow[2] = updatetime;
  61.                 oDataTable.Rows.Add(oDataRow);
  62.             }
  63.  
  64.             oDataSet.Tables.Add(oDataTable);
  65.  
  66.         }
  67.     }
  68. }
I have attached a fully working solution (created in Visual Studio 2010) for you to download and play with if you want to.

The following is whole ASP code for the page that I used in case you can't download the project:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.     <title>Repeater Example</title>
  7.     <style type="text/css">
  8.         body
  9.         {
  10.             color: #696969;
  11.             font-family: "Helvetica Neue" , "Lucida Grande" , "Segoe UI" ,Arial,Helvetica,Verdana,sans-serif;
  12.             font-size: 0.8em;
  13.         }
  14.         .Count, .imgbutton, .comm_info_name, .comm_sep, .comm_info_date, .comm_info_Title, .comm_info_content
  15.         {
  16.             float: left;
  17.             margin: .5em;
  18.         }
  19.         .comm_info_content
  20.         {
  21.             margin: 0;
  22.         }
  23.         .mid, .bottom, .fr_imgL
  24.         {
  25.             clear: both;
  26.             width: 100%;
  27.         }
  28.         .in
  29.         {
  30.             display: block;
  31.             padding: .5em;
  32.             border: 1px solid #cccccc;
  33.             width: 50%;
  34.             margin: 0px,auto,0px,auto;
  35.         }
  36.         a:link, a:visited
  37.         {
  38.             color: #034AF3;
  39.         }
  40.         .header
  41.         {
  42.             background: none repeat scroll 0 0 #4B6C9E;
  43.             margin: 0;
  44.             padding: 0;
  45.             width: 100%;
  46.         }
  47.         .header h1
  48.         {
  49.             border: medium none;
  50.             color: #F9F9F9;
  51.             font-size: 2em;
  52.             font-weight: 700;
  53.             line-height: 2em;
  54.             margin: 0 auto;
  55.             padding: 0 0 0 20px;
  56.             text-align: center;
  57.             width: 100%;
  58.         }
  59.     </style>
  60. </head>
  61. <body>
  62.     <form id="form1" runat="server">
  63.     <div>
  64.         <div class="header">
  65.             <h1>
  66.                 Demonstrating Repeaters in ASP.NET
  67.             </h1>
  68.         </div>
  69.         <asp:Repeater ID="BookListing" runat="server">
  70.             <ItemTemplate>
  71.                 <div class="top">
  72.                     <div class="comm_info">
  73.                         <div class="comm_info_Title">
  74.                             <%# DataBinder.Eval(Container.DataItem, "Title")%>
  75.                         </div>
  76.                         <div class="LikeDislike">
  77.                             <div class="Count">
  78.                                 24
  79.                             </div>
  80.                             <div class="imgbutton">
  81.                                 <asp:ImageButton runat="server" ID="lnkLike" OnClick="lnkLike_OnClick" AlternateText="Like" />
  82.                             </div>
  83.                             <div class="Count">
  84.                                 34
  85.                             </div>
  86.                             <div class="imgbutton">
  87.                                 <asp:LinkButton ID="lnkDislike" runat="server" CssClass="DislikeButton" Text="Dislike"> </asp:LinkButton>
  88.                             </div>
  89.                         </div>
  90.                         <div class="fr_imgR">
  91.                         </div>
  92.                         <div class="comm_info_content">
  93.                             <div class="comm_info_name">
  94.                                 <%# DataBinder.Eval(Container.DataItem, "Title")%>
  95.                             </div>
  96.                             <span class="comm_sep">|</span>
  97.                             <div class="comm_info_date">
  98.                                 <%# DataBinder.Eval(Container.DataItem, "updatetime")%>
  99.                             </div>
  100.                         </div>
  101.                         <div class="fr_imgL">
  102.                         </div>
  103.                     </div>
  104.                 </div>
  105.                 <div class="mid">
  106.                     <div class="in">
  107.                         <div class="body" style="margin-bottom: 5px;">
  108.                             <%# DataBinder.Eval(Container.DataItem, "Body")%>
  109.                         </div>
  110.                     </div>
  111.                 </div>
  112.                 <div class="bottom">
  113.                 </div>
  114.             </ItemTemplate>
  115.         </asp:Repeater>
  116.     </div>
  117.     </form>
  118. </body>
  119. </html>

-Frinny
Attached Files
File Type: zip WebApplication1.zip (24.6 KB, 78 views)
Nov 17 '11 #2

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

Similar topics

5
by: Ralph Freshour | last post by:
I'm trying to use an image as a submit button - I'm reading Sams Teach Yourself HTML and the chapter on this doesn't show me the details on how this is done - I mean I can't seem to see it or grasp...
4
by: Ester | last post by:
I have an interior design of a living room with lights and aircond image. My task is to load the living room image without any light and aircond images as background image of a WebForm after that I...
2
by: Ester | last post by:
Instead of drag and drop image button from Toolbox and name the image button id on the properties box, I would like to load image button ID from database. I created a database table that stores...
3
by: Russell | last post by:
I have a web page that i am using an image button as my submit button. The code behind the button works, but when I click enter on the web page, nothing happens. Can anybody tell me how to get my...
2
by: pmud | last post by:
Hi, I am designing a simple ASP.NET web page. I have an image button. What I want to do is: when the cursor is over the image button, a drop down meny should display. This menu should have tabs...
1
by: Phil_Cam | last post by:
Hello All On a webpage I have a standard paypal image button for purchases. I am trying to set it up so that it only shows up or is endabled when text is entered into a textbox and a button is...
0
by: Carz | last post by:
I have an image dynamically created. An image button is then created using this image. The problem is that it shows up perfectly on my machine, but on others it doesn't. The image is created by...
2
by: smokeyd | last post by:
i am trying to create a simple image button rollover.. i have searched this forum and found a number of solutions but none seem to work. i am just trying to get the onmouseover to swap the image...
3
by: glbdev | last post by:
Hi. I have a gridview in which each row has an image button attached. I have each button in each row set up to be associated with a DB field. When I hover above the button it shows me the...
4
by: glbdev | last post by:
Hi, I posted this question yesterday but didn't get the answer I needed. I am DESPERATE to get this working so I'm re-posting it because I don't think I worded it correctly. I have a GridView...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.