问题描述
我需要在页面上使用用户控件 (.ascx),它是基于 2 个参数的相关帖子用户控件:
I need to use user controls (.ascx) on a page, it's a related post user control based in 2 parameters:
1. Current post
2. Relation type
页面需要有 3 个不同的此控件实例,每个实例具有相同的 Current post 参数,但关系类型不同(标题、作者、流派).
the page needs to have 3 different instances of this control, each having the same Current post parameter, but different relation type (title, author, genre).
第一个参数我可以通过url获取,但是第二个参数呢?
The 1st parameter I can get it through url, but what about the second parameter?
我已经在谷歌上搜索了一段时间,但我还没有找到答案.如何传递第二个参数,以便控件可以根据这些参数加载信息?我宁愿不为每个参数创建一个控件,否则最好不构建用户控件而是直接进入代码:(谢谢!
I've been googling for a while but i haven't found an answer yet. How can I pass the second parameter so the control can load the information based on these parameters? I'd rather not to create a control for each parameter, else would be better to build no user control but direct into code :( Thanks!
推荐答案
创建用户控件的公共属性,如:
Create public properties of the user-control like:
public partial class SampleUC : UserControl
{
public string CurrentPost {get;set;}
public string RelationType {get;set;}
//...
//...
}
使用标记从页面中分配那些,例如:
Assign those from the page using it either from markup like:
<%@ Register TagPrefix="cc" TagName="SampleUC" Src="SampleUC.ascx" %>
...
...
<cc:SampleUC id="myUC" runat="server" CurrentPost="Sample Post Title" RelationType="Title" />
或来自(使用它的页面的)代码隐藏:
or from code-behind (of the page using it):
protected void Page_Load(object sender, EventArgs e)
{
//...
myUC.CurrentPost = "Sample Post Title";
myUC.RelationType = "Title" ;
//...
}
这篇关于将自定义参数发送到用户控件 ascx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!