<%@ 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属性?
<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;"> [按钮文本] </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 也不对
var img=document.getElementById('<%=Imagecon1.ClientID%>').getElementsByTagName("img")[0];
if(img)
{
src=img.src;
}alert(src);
还是报为空或者找不到对象
protected override void Render(HtmlTextWriter writer)
{
base.RenderContents(writer);
// writer.Write("<script language=javascript src='js/JScript.js'></script>");
}
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(" " + this.Text + " "));
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();
}
[DefaultProperty("Text")]
[ToolboxData("<{0}:imagecon runat=server></{0}:imagecon>")]
//[ParseChildren(true, "Item")]
//[PersistChildren(false)]
public class imagecon : CompositeControl, INamingContainer
tbID = this.UniqueID;//本来是this.UniqueID + "_table"
t.ID = tbID;=》Table t = new Table();
t.ID = string.Format("{0}_table",ClientID);
然后使用var img=document.getElementById('<%=Imagecon1.TableID%>').getElementsByTagName("img")[0];
编译的时候'<%=Imagecon1.TableID%>'自动的变成Imagecon1_table
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];
<tr>
<td style="height:96px;"><img src="image\image2.jpg" style="border-width:0px;height:96px;" /></td><td> [按钮文本] </td><td id="Imagecon1_Imagecon1tcDown"><font size=1>▼</font></td>
</tr>
</table>
</p> 我把CSS去掉了 table的ID的确和编译的时候一样但是就是找不到
t.ID = "_table";
TableID = string.Format("{0}{1}{2}", UniqueID,this.IdSeparator,t.ID);
{
/// <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(" " + Text + " "));
tc_text.Height = tc_text.Height;
//tc_text.Width = tc_text.Width;
tr.Cells.Add(tc_text);
this.Controls.Add(t);
base.CreateChildControls();
} }
}
<%@ 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>
{
string strImageUrl = Path.Combine(ClientPath, "image\\" + ImageName);--------------------------------------------------------------------------------------
这里是硬性要求必须输入 ClientPath 和 ImageName 如果你希望有默认值的话,可以改回 ||