locked
I want to change CSS file name of content page at run time , the file name is taken from database ,i got "System.Web.UI.HtmlControls.HtmlGenericControl Does not contain a definition for Href" this error when i tried to use href attribute of Style RRS feed

  • Question

  • User-108112679 posted

    //Aspx page

    <%@ Page Title="" Language="C#" MasterPageFile="~/UserMenuMaster.master" AutoEventWireup="true" CodeFile="divMaster.aspx.cs" Inherits="divMaster" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <link href="~/Styles/Normal.css" rel="stylesheet" type="text/css" id="stylesheet" runat="server"/>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <div id="output" class="output1" runat="server">

        </div>
        <asp:Label ID="mess" runat="server" ></asp:Label>
    </asp:Content>

    //c# page

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;

    public partial class div : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

            string sqlcmd, constr, sqlcmd1;
            constr = @"Data Source=.;Initial Catalog=sample;Integrated Security=True";
          
            sqlcmd = @"select body from aboutUs where id=1";
            sqlcmd1 = @"select Cssfile from CssFileName where Fname='Philosophy'";
            SqlConnection con = new SqlConnection(constr);
            SqlConnection con1 = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand(sqlcmd, con);
            SqlCommand cmd1 = new SqlCommand(sqlcmd1, con1);
            //SqlCommand cmd1=new SqlCommand(sqlcmd1,con);
            SqlDataReader rd, rd1;
            String s="";
            try
            {

                con1.Open();
                rd = cmd1.ExecuteReader();
                //rd1 = cmd1.ExecuteReader();
                while (rd.Read())
                {
                    s = rd[0].ToString();
                }
                //Response.Write(s);
                stylesheet.Href = s;

                rd.Close();
                con1.Close();


            }
            catch (Exception ee)
            {
                mess.Text = ee.Message;
            }
            finally
            {
                con1.Close();
            }

            try
            {

                con.Open();
                rd1 = cmd.ExecuteReader();
                //rd1 = cmd1.ExecuteReader();
                while (rd1.Read())
                {

                    output.InnerHtml = rd1[0].ToString();
                }

                rd1.Close();
                con.Close();


            }
            catch (Exception ee)
            {
                mess.Text = ee.Message;
            }
            finally
            {
                con.Close();
            }
        }
    }

    Monday, April 8, 2013 2:20 PM

Answers

  • User1508394307 posted

    Use

    stylesheet.Attributes["href"] = s;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 8, 2013 3:06 PM
  • User281315223 posted

    You should be able to simply set the "href" attribute directly since you are using a runat="server" tag as well as an ID property : 

    protected void Page_Load(object sender, EventArgs e)
    {
        //Updates the "href" attribute of your stylesheet element
        stylesheet.Attributes["href"] = "new.css";
    //stylesheet.Attributes["href"] = s; }

    Before : 

    <!-- Your Original -->
    <link id="stylesheet" href="example.css" type="text/css" runat="server" />

    After : 

    <!-- Rendered Result -->
    <link id="stylesheet" href="new.css" type="text/css"/>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 8, 2013 3:19 PM

All replies

  • User1508394307 posted

    Use

    stylesheet.Attributes["href"] = s;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 8, 2013 3:06 PM
  • User281315223 posted

    You should be able to simply set the "href" attribute directly since you are using a runat="server" tag as well as an ID property : 

    protected void Page_Load(object sender, EventArgs e)
    {
        //Updates the "href" attribute of your stylesheet element
        stylesheet.Attributes["href"] = "new.css";
    //stylesheet.Attributes["href"] = s; }

    Before : 

    <!-- Your Original -->
    <link id="stylesheet" href="example.css" type="text/css" runat="server" />

    After : 

    <!-- Rendered Result -->
    <link id="stylesheet" href="new.css" type="text/css"/>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 8, 2013 3:19 PM
  • User-108112679 posted

    thanx for reply ..

    Tuesday, April 9, 2013 3:08 AM
  • User-108112679 posted

    thanx for reply ..

    Tuesday, April 9, 2013 3:08 AM