469,282 Members | 1,895 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

change an images alt text server side

Hi all

I'm trying to change the alt text of some images dynamically. all the content for the page including links url copy and image alt text is stored in a db so when a user changes the language preference the site copy changes etc.

this is what i have so far

i have a dataset that is cached from the db then i loop through each row


Expand|Select|Wrap|Line Numbers
  1. foreach (DataRow theRow in ds.Tables[0].Rows)
  2. {foreach (DataRow theRow in ds.Tables[0].Rows)
  3. {
  4. string control = theRow["ContentId"].ToString();
  5. HtmlContainerControl ctrl = FindControlRecursive(this, control) as HtmlContainerControl;
  6. if (ctrl != null)
  7. {
  8.     if (theRow["ContentType"].ToString() == "text")
  9.     {
  10.     ctrl.InnerHtml = theRow["PageContent"].ToString();
  11.     }
  12.     if (theRow["ContentType"].ToString() == "link")
  13.     {
  14.     //ctrl.HRef = theRow["PageContent"].ToString();
  15.     //doesnt work 
  16.     }
  17.     if (theRow["ContentType"].ToString() == "alt")
  18.     {
  19.     //ctrl.Alt = theRow["PageContent"].ToString();
  20.     //doesnt seem to find the control and even if i hard code the the id in it doesnt work
  21.     }
  22. }
  23. }
an example of my images is
<img class="socialIcon" src="/images/facebook.png" id="facebookIcon" runat="server" alt="Facebook icon" width="24" height="20" border="0" />

and an example of my link is
<a href="http://www.facebook.com" target="_blank" id="facebookHref" runat="server">

thanks
Oct 14 '10 #1
2 1783
Frinavale
9,735 Expert Mod 8TB
I strongly recommend changing your HTML <img> and <a> into ASP.NET controls (the Image control and the HyperLink control) so that you can easily accomplish what you want to do in your server-side code.

You also may want to look into the .NET concepts for Localization and Globalization.

-Frinny
Oct 15 '10 #2
danp129
321 Expert 100+
Using the server controls like so:
Expand|Select|Wrap|Line Numbers
  1.     <form id="form1" runat="server">
  2.     <ul style="list-style-type: none">
  3.         <li>
  4.             <asp:Image ID="Image1" runat="server" src="pic1.gif" AlternateText="old1" /><asp:hyperlink runat="server" ID="Link1"> LinkText1</asp:hyperlink>
  5.         </li>
  6.         <li>
  7.             <asp:Image ID="Image2" runat="server" src="pic1.gif" AlternateText="old2" /><asp:hyperlink runat="server" ID="Link2"> LinkText2</asp:hyperlink>
  8.         </li>
  9.         <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/pic1.gif" AlternateText="old1"  />
  10.     </ul>
  11.     </form>
This will work (Written in VB and converted to C#!)
Expand|Select|Wrap|Line Numbers
  1. using Microsoft.VisualBasic;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Data.SqlClient;
  8. partial class _Default : System.Web.UI.Page
  9. {
  10.     protected void Page_Load(object sender, System.EventArgs e)
  11.     {
  12.         DataSet ds = MakeMockDataset();
  13.         if (Page.IsPostBack) {
  14.         } else {
  15.             foreach (DataRow theRow in ds.Tables[0].Rows) {
  16.                 string control = theRow["ContentId"].ToString();
  17.  
  18.                 // You'll need your recursive find control function if the image is wrapped inside the hyperlink
  19.                 object ctrl = this.FindControl(control);
  20.  
  21.                 if (ctrl is HyperLink) {
  22.                     //Dim MyLink As HyperLink = ctrl
  23.                     //MyLink.Text = theRow("PageContent")
  24.                     ctrl.Text = theRow["PageContent"];
  25.                 } else if (ctrl is Image) {
  26.                     //Dim MyImage As Image = ctrl
  27.                     //MyImage.AlternateText = theRow("PageContent")
  28.                     ctrl.AlternateText = theRow["PageContent"];
  29.                 }
  30.             }
  31.         }
  32.     }
  33.     public DataSet MakeMockDataset()
  34.     {
  35.         DataSet ds = new DataSet();
  36.         ds.Tables.Add("test");
  37.         var _with1 = ds.Tables[0];
  38.         _with1.Columns.Add(new DataColumn("ContentId"));
  39.         _with1.Columns.Add(new DataColumn("ContentType"));
  40.         _with1.Columns.Add(new DataColumn("PageContent"));
  41.         _with1.Rows.Add(new string[] {
  42.             "Link1",
  43.             "link",
  44.             "http://bytes.com/"
  45.         });
  46.         _with1.Rows.Add(new string[] {
  47.             "Image1",
  48.             "alt",
  49.             "Text for my first image"
  50.         });
  51.         _with1.Rows.Add(new string[] {
  52.             "Link2",
  53.             "link",
  54.             "http://bytes.com/"
  55.         });
  56.         _with1.Rows.Add(new string[] {
  57.             "Image2",
  58.             "alt",
  59.             "Text for my second image"
  60.         });
  61.         return ds;
  62.     }
  63.     public _Default()
  64.     {
  65.         Load += Page_Load;
  66.     }
  67. }
  68.  
Oct 15 '10 #3

Post your reply

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

Similar topics

1 post views Thread by yaya coco | last post: by
2 posts views Thread by VB Programmer | last post: by
4 posts views Thread by Lee Chapman | last post: by
5 posts views Thread by Jerry Camel | last post: by
5 posts views Thread by B.J. | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.