如何在用户控件中设置属性 [英] how to set attribute in user control
本文介绍了如何在用户控件中设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi
i创建了一个用户控件。
进入用户控件有图像元素。
hi
i created a user control.
into user control have image element.
<img id="imgNews" src="Images/twitter-256.png" runat="server" />
和创建的属性:
and created property:
string srcNews;
public string SrcNews
{
get { return srcNews; }
set { srcNews = value;
}
}
已经,我在我的网站上使用usercontrol。
i想要设置属性src来自我的网站的图像用户控制。
我怎么做?
already, i use usercontrol in my website .
i want set attribute src from image user control from my web site .
how i can ?
推荐答案
你应该注册的页面明智的
]]>
]]>
明智的身体部分
Your Page you Should Register Like wise
]]>
]]>
Body Section like wise
< snp:usernews id =userNewsrunat =Serverxmlns:snp =#unknown>
<snp:usernews id="userNews" runat="Server" xmlns:snp="#unknown">
< asp:button id =btn1runat =servertext =点击xmlns:asp =#unknown>
codeFile有:
<asp:button id="btn1" runat="server" text="Click" xmlns:asp="#unknown">
codeFile have :
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
userNews.SrcNews = "Chrysanthemum.jpg";
}
usercontrol修改属性
usercontrol modify the Property
public string SrcNews
{
get { return imgNews.Attributes["src"];}
set { imgNews.Attributes["src"] = value;}
}
i希望此代码能够正常工作
i hope this code will work
在您的源代码中,您应该更改SrcNews
属性如下:
In your source code you should change yourSrcNews
property like bellow:
public string SrcNews
{
get { return imgNews.Attributes["src"];}
set { imgNews.Attributes["src"] = value;}
}
您好
i有一个澄清。在访问您可以创建的属性之前该用户控件的实例并尝试访问该属性SrcNews。
Ex:
您已注册一个名为Usercontrol1的用户控件.ascx。
你明智地编写代码
Usercontrol1 objUserControl = Page.FindControl(Usercontrol1.ascx);
objUserControl.SrcNews =;
现在您可以访问此属性。
Hi
i have one clarification.before accessing the property you could create the instance for that user control and try to access that property "SrcNews".
Ex:
you have registered one usercontrol called "Usercontrol1.ascx".
you write the code like wise
Usercontrol1 objUserControl=Page.FindControl("Usercontrol1.ascx");
objUserControl.SrcNews="";
now you may access this property.
这篇关于如何在用户控件中设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文