change an images alt text server side

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • trampman

    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


    Code:
    foreach (DataRow theRow in ds.Tables[0].Rows)
    {foreach (DataRow theRow in ds.Tables[0].Rows)
    {
    string control = theRow["ContentId"].ToString();
    HtmlContainerControl ctrl = FindControlRecursive(this, control) as HtmlContainerControl;
    if (ctrl != null)
    {
        if (theRow["ContentType"].ToString() == "text")
        {
    	ctrl.InnerHtml = theRow["PageContent"].ToString();
        }
        if (theRow["ContentType"].ToString() == "link")
        {
    	//ctrl.HRef = theRow["PageContent"].ToString();
    	//doesnt work 
        }
        if (theRow["ContentType"].ToString() == "alt")
        {
    	//ctrl.Alt = theRow["PageContent"].ToString();
    	//doesnt seem to find the control and even if i hard code the the id in it doesnt work
        }
    }
    }
    an example of my images is
    <img class="socialIc on" src="/images/facebook.png" id="facebookIco n" runat="server" alt="Facebook icon" width="24" height="20" border="0" />

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

    thanks
    Last edited by Niheel; Oct 14 '10, 03:23 PM.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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

    Comment

    • danp129
      Recognized Expert Contributor
      • Jul 2006
      • 323

      #3
      Using the server controls like so:
      Code:
          <form id="form1" runat="server">
          <ul style="list-style-type: none">
              <li>
                  <asp:Image ID="Image1" runat="server" src="pic1.gif" AlternateText="old1" /><asp:hyperlink runat="server" ID="Link1"> LinkText1</asp:hyperlink>
              </li>
              <li>
                  <asp:Image ID="Image2" runat="server" src="pic1.gif" AlternateText="old2" /><asp:hyperlink runat="server" ID="Link2"> LinkText2</asp:hyperlink>
              </li>
              <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/pic1.gif" AlternateText="old1"  />
          </ul>
          </form>
      This will work (Written in VB and converted to C#!)
      Code:
      using Microsoft.VisualBasic;
      using System;
      using System.Collections;
      using System.Collections.Generic;
      using System.Data;
      using System.Diagnostics;
      using System.Data.SqlClient;
      partial class _Default : System.Web.UI.Page
      {
          protected void Page_Load(object sender, System.EventArgs e)
          {
              DataSet ds = MakeMockDataset();
              if (Page.IsPostBack) {
              } else {
                  foreach (DataRow theRow in ds.Tables[0].Rows) {
                      string control = theRow["ContentId"].ToString();
      
                      // You'll need your recursive find control function if the image is wrapped inside the hyperlink
                      object ctrl = this.FindControl(control);
      
                      if (ctrl is HyperLink) {
                          //Dim MyLink As HyperLink = ctrl
                          //MyLink.Text = theRow("PageContent")
                          ctrl.Text = theRow["PageContent"];
                      } else if (ctrl is Image) {
                          //Dim MyImage As Image = ctrl
                          //MyImage.AlternateText = theRow("PageContent")
                          ctrl.AlternateText = theRow["PageContent"];
                      }
                  }
              }
          }
          public DataSet MakeMockDataset()
          {
              DataSet ds = new DataSet();
              ds.Tables.Add("test");
              var _with1 = ds.Tables[0];
              _with1.Columns.Add(new DataColumn("ContentId"));
              _with1.Columns.Add(new DataColumn("ContentType"));
              _with1.Columns.Add(new DataColumn("PageContent"));
              _with1.Rows.Add(new string[] {
                  "Link1",
                  "link",
                  "http://bytes.com/"
              });
              _with1.Rows.Add(new string[] {
                  "Image1",
                  "alt",
                  "Text for my first image"
              });
              _with1.Rows.Add(new string[] {
                  "Link2",
                  "link",
                  "http://bytes.com/"
              });
              _with1.Rows.Add(new string[] {
                  "Image2",
                  "alt",
                  "Text for my second image"
              });
              return ds;
          }
          public _Default()
          {
              Load += Page_Load;
          }
      }

      Comment

      Working...