如何从aspx页面后面的代码添加值并将其传递给Usercontrol [英] How to add and pass value to Usercontrol from code behind aspx page

查看:92
本文介绍了如何从aspx页面后面的代码添加值并将其传递给Usercontrol的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内容页面,在该内容页面上,我正在使用用户控件.我需要从代码后面添加用户控件,并同时传递值.任何帮助

以下我将用户控件添加到我的aspx页面,但我不知道如何将值传递给用户控件.

注意::我可以从HTML做到这一点,但我需要从
后面的代码中完成

I have a content page , on the content page i am using a user control . I need to add User control from code behind and pass the value in same time. Any help

Following i am adding user control to my aspx page but i don''t know how to pass value to user control.

NOTE:: I can do this from HTML but i need to do from code behind


Dim MainContent As ContentPlaceHolder = TryCast(Master.FindControl("MainContent"), ContentPlaceHolder)
 Dim _usercontrol As UserControl = CType(Page.LoadControl("~\Usercontrols\dashboard_Employer.ascx"), Control)
MainContent.Controls.Add(_usercontrol)



我在用户控件中设置了名为ValueID的属性.我需要从后面的代码传递此ID.



I have property set in User Control Called ValueID. I need to pass this ID from code behind.

Public Property ValueID() As Integer
       Get
           Return _ValueID
       End Get
       Set(ByVal value As Integer)
           _ValueID = value
       End Set
   End Property

推荐答案

您始终可以将值添加为属性

You could always add the value as an attribute

Dim MainContent As ContentPlaceHolder = TryCast(Master.FindControl("MainContent"), ContentPlaceHolder)
 Dim _usercontrol As UserControl = CType(Page.LoadControl("~\Usercontrols\dashboard_Employer.ascx"), Control)
MainContent.Controls.Add(_usercontrol)

_usercontrol.Attributes.Add("Value", "1")



在您的用户控件中,您可以设置



In your user control you could set

Public Property ValueID() As Integer
        Get
            Return If(Me.Attributes("ValueID").Length < 1, 0, CType(a.Attributes("ValueID"), Integer))
        End Get
        Set(ByVal value As Integer)
            Me.Attributes.Add("ValueID", value.ToString())
        End Set
    End Property


我不要不知道将值传递给用户控制.
在用户控件中公开get-set属性并使用它们.
i don''t know to pass value to user control.
Expose get-set properties in your user control and use them.


这篇关于如何从aspx页面后面的代码添加值并将其传递给Usercontrol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆