<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><%@ Register Assembly="imagecon" Namespace="imagecon" TagPrefix="cc1" %><!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>无标题页</title>    
    <script type="text/javascript" language="JavaScript">
        var image=document.getElementById(Imagecon1.ClientID).getAttribute("ImageName");
      </script>        
</head>
    <body>    
    <form id="form1" runat="server"> 
    <div>
        <cc1:imagecon ID="Imagecon1" runat="server" ImageName="image2.jpg">        
        </cc1:imagecon>
    </div>      
    </form>   
</body>
</html>
请问如何读取自定义控件中的ImageName属性?

解决方案 »

  1.   

    看你自定义控件 生成的 html 代码
      

  2.   

    用这个看看。。  $("#<%=txtUserID.ClientID%>").attr("ImageName");
      

  3.   

    本人新学.net 还不是很懂 如何查看?
      

  4.   

    <table id="Imagecon1_Imagecon1_table" cellspacing="0" cellpadding="0" onselectstart="return false;" border="0" style="border-width:0px;height:140px;width:380px;border-collapse:collapse;Cursor:default;">
    <tr style="border-width:0px;">
    <td style="background-color:menu;border-color:buttonshadow;border-width:1px;border-style:solid;height:140px;border-Width:1px 0px 1px 1px;"><img src="image\image2.jpg" style="border-width:0px;height:140px;" /></td><td style="background-color:menu;border-color:buttonshadow;border-width:1px;border-style:solid;border-Width:1px 0px 1px 0px;">&nbsp;[按钮文本]&nbsp;</td><td id="Imagecon1_Imagecon1tcDown" onmouseenter="this.style.borderStyle='inset';" onmouseleave="this.style.borderStyle='outset';" style="background-color:menu;border-color:buttonshadow;border-width:1px;border-style:solid;"><font size=1>▼</font></td>
    </tr>
    </table>本人的自定义控件定义了个1行3列的表,图片存在单元格1里,刚试了下好像用Imagecon1_Imagecon1_table 也不对
      

  5.   

    <img src="image\image2.jpg" 你想获取  image2.jpg 还是  image\image2.jpg
      

  6.   

    img src="image\image2.jpg" ?整个 img 标签 么?
      

  7.   

    var src="";
    var img=document.getElementById('<%=Imagecon1.ClientID%>').getElementsByTagName("img")[0];
    if(img)
    {
        src=img.src;
    }alert(src);
      

  8.   

    var img=document.getElementById('Imagecon1').getElementsByTagName("img")[0];
    还是报为空或者找不到对象
      

  9.   

    var img=document.getElementById('<%=Imagecon1.ClientID%>').getElementsByTagName("img")[0];
      

  10.   

    我调试的时候'<%=Imagecon1.ClientID%>'自动专成了'Imagecon1'
      

  11.   

    那你生成的 table 的 id 怎么是 Imagecon1_Imagecon1_table
      

  12.   

    var image=document.getElementById('<%=Imagecon1.ClientID%>').getAttribute("ImageName");
      

  13.   

    你把自定义控件 的 Render 方法贴一下
      

  14.   


     protected override void Render(HtmlTextWriter writer)
            {                      
                base.RenderContents(writer);
                // writer.Write("<script   language=javascript   src='js/JScript.js'></script>");         
            }
      

  15.   

    我想看下 你生成 table 的代码
      

  16.   


      protected override void CreateChildControls()
            {
                Table t = new Table();
                tbID = this.UniqueID;//本来是this.UniqueID + "_table"
                t.ID = tbID;            TableRow tr = new TableRow();
                t.Rows.Add(tr);            TableCell tc_img = new TableCell();
                tc_img.Height = this.Height;
                //tc_img.Width = this.Width;
                if (this.ClientPath != String.Empty || this.ImageName != String.Empty)
                {             
                    string strImageUrl = Path.Combine(this.ClientPath, "image\\" + this.ImageName);
                    System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
                    image.BorderWidth = Unit.Pixel(0);
                    image.ImageUrl = strImageUrl;
                    image.Height = tc_img.Height;
                    //image.Width = tc_img.Width;
                    tc_img.Controls.Add(image);            
                    tr.Cells.Add(tc_img);
                   
                }            TableCell tc_text = new TableCell();
                tc_text.Controls.Add(new LiteralControl("&nbsp;" + this.Text + "&nbsp;"));
                tc_text.Height = tc_text.Height;
                //tc_text.Width = tc_text.Width;
                tr.Cells.Add(tc_text);            TableCell tc_downmenu = new TableCell();
                tcDownID = this.UniqueID + "tcDown";
                tc_downmenu.ID = tcDownID;
                tc_downmenu.Controls.Add(new LiteralControl("<font size=1>▼</font>"));
                tr.Cells.Add(tc_downmenu);            this.Controls.Add(t);
                base.CreateChildControls();
            }
      

  17.   

    你必须要把ImageName属性渲染到你的自定义控件的html代码中
      

  18.   

    你的自定义控件 继承的那个类? , 实现INamingContainer接口 没
      

  19.   

     
    [DefaultProperty("Text")]
        [ToolboxData("<{0}:imagecon runat=server></{0}:imagecon>")]
        //[ParseChildren(true, "Item")] 
        //[PersistChildren(false)] 
        public class imagecon : CompositeControl, INamingContainer
      

  20.   

    Table t = new Table();
                tbID = this.UniqueID;//本来是this.UniqueID + "_table"
                t.ID = tbID;=》Table t = new Table();
                t.ID = string.Format("{0}_table",ClientID);
      

  21.   

    然后将自定义控件编译一下--------------------------------var img=document.getElementById('<%=Imagecon1.ClientID%>').getElementsByTagName("img")[0];=》var img=document.getElementById('<%=Imagecon1.ClientID%>_table').getElementsByTagName("img")[0];or你给自定义控件增加一个 public 的 字段 命名为 TableID Table t = new Table();TableID =string.Format("{0}_table",ClientID);  t.ID = TableID;
    然后使用var img=document.getElementById('<%=Imagecon1.TableID%>').getElementsByTagName("img")[0];
      

  22.   

    依然无用...
    编译的时候'<%=Imagecon1.TableID%>'自动的变成Imagecon1_table
      

  23.   

    那这样Table t = new Table();TableID =string.Format("{0}_table",ClientID);  t.ID = "_table";其余不变
      

  24.   

    使用var img=document.getElementById('<%=Imagecon1.TableID%>').getElementsByTagName("img")[0];
      

  25.   

    还是不行..就算我直接写getElementById(Imagecon1_Imagecon1_TableID%)依然找不到..
      

  26.   

    你现在是怎么写的自定义控件的代码 和生成的 html 代码
      

  27.   


     public string TableID="";protected override void CreateChildControls()
            {
                Table t = new Table();
                TableID = string.Format("{0}_table", ClientID);
                t.ID = "_table";  var img=document.getElementById('<%=Imagecon1.TableID%>').getElementsByTagName("img")[0];
      

  28.   

    在看下 自定义控件生成的 html 代码
      

  29.   

     <table id="Imagecon1__table" border="0">
    <tr>
    <td style="height:96px;"><img src="image\image2.jpg" style="border-width:0px;height:96px;" /></td><td>&nbsp;[按钮文本]&nbsp;</td><td id="Imagecon1_Imagecon1tcDown"><font size=1>▼</font></td>
    </tr>
    </table>
       </p>   我把CSS去掉了 table的ID的确和编译的时候一样但是就是找不到
      

  30.   

    Table t = new Table();
                t.ID = "_table";
                TableID = string.Format("{0}{1}{2}", UniqueID,this.IdSeparator,t.ID);
      

  31.   

    namespace MyCustomControl
    {
        /// <summary>
        /// Summary description for CustomControl
        /// </summary>
        public class CustomControl : CompositeControl
        {
            public string TableID { set; get; }        public string Text { set; get; }        public string ClientPath { set; get; }        public string ImageName { set; get; }        
            protected override void CreateChildControls()
            {
                Table t = new Table();            
                t.ID = "table";
                TableID = string.Format("{0}{1}{2}", UniqueID, "_", t.ID);
                TableRow tr = new TableRow();
                t.Rows.Add(tr);            TableCell tc_img = new TableCell();
                tc_img.Height = this.Height;            if (ClientPath != String.Empty && ImageName != String.Empty)
                {
                    string strImageUrl = Path.Combine(ClientPath, "image\\" + ImageName);
                    System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
                    image.BorderWidth = Unit.Pixel(0);
                    image.ImageUrl = strImageUrl;
                    image.Height = tc_img.Height;
                    //image.Width = tc_img.Width;
                    tc_img.Controls.Add(image);
                    tr.Cells.Add(tc_img);            }            TableCell tc_text = new TableCell();
                tc_text.Controls.Add(new LiteralControl("&nbsp;" + Text + "&nbsp;"));
                tc_text.Height = tc_text.Height;
                //tc_text.Width = tc_text.Width;
                tr.Cells.Add(tc_text);
                           this.Controls.Add(t);
                base.CreateChildControls();
            }    }
    }
      

  32.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="A.aspx.cs" Inherits="ajax_A" %><%@ Register Namespace="MyCustomControl" TagPrefix="customTable" %><!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></title>
        <script defer="defer">
            var src = "";
            var img = document.getElementById('<%=cc.TableID%>').getElementsByTagName("img")[0];
            if (img) {
                src = img.src;
            }        alert(src);
        </script>
    </head>
    <body>   
        <customTable:CustomControl  ID="cc" runat="server" ClientPath="sandy" ImageName="1.jpg" />
        
    </body>
    </html>
      

  33.   

    if (ClientPath != String.Empty && ImageName != String.Empty)
                {
                    string strImageUrl = Path.Combine(ClientPath, "image\\" + ImageName);--------------------------------------------------------------------------------------
    这里是硬性要求必须输入 ClientPath  和 ImageName 如果你希望有默认值的话,可以改回 ||
      

  34.   

    看看预览后生成的代码,然后就知道它的id了,这样就可以用js引用了