将自定义参数发送到用户控件 ascx

send custom parameters to user control ascx(将自定义参数发送到用户控件 ascx)
本文介绍了将自定义参数发送到用户控件 ascx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在页面上使用用户控件 (.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)