ASP.Net声明的价值传递从页面属性到用户控件 [英] ASP.Net Declaratively Passing Value from Page Property to User Control

查看:145
本文介绍了ASP.Net声明的价值传递从页面属性到用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一种方法来声明(而不是在文件后面code)传递一个属性的值在一个ASP.Net网页的用户控件。下面是什么,我试图做一个简单的例子,但我不能得到它的工作。
下面是aspx页面在这里我创建的用户控件的对象标记:

I need to find a way to declaratively (not in the code behind file) pass the value of a property in an ASP.Net web page to a user control. Following is a simple example of what I'm trying to do, but I can't get it to work. Here is the markup for the aspx page where I'm creating an object of the user control:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"  Inherits="_Default" %>
<%@ Register Src="~/MyUserControl.ascx" TagName="MyUserControl" TagPrefix="uc1" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <uc1:MyUserControl ID="MyUserControl1" runat="server"
      UserControlProperty = '<%# Bind("PageProperty") %>' />
    </div>
    </form>
</body>
</html>

下面是从后面的aspx页面(aspx.cs)文件中的code:

Here is the code behind (aspx.cs) file from the aspx page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    public int PageProperty { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        PageProperty = 42;

    }
}

下面是从用户控件(ASCX文件)的标记:

Here is the markup from the usercontrol (ascx file):

<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="MyUserControl.ascx.cs" Inherits="MyUserControl" %>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

这背后是从用户的控制文件(ascx.cs)在code:

And here is the code behind file (ascx.cs) from the user control:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyUserControl : System.Web.UI.UserControl
{
    public int UserControlProperty { get; set; }


    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Text = UserControlProperty.ToString();
    }
}

因此​​,所有我想要做的是通过在aspx页面定义成用户控件,设置它的UserControlProperty,然后在用户控件一个文本框显示该值的PageProperty属性的值。谁能告诉我,我做了什么错在这里?

So, all I'm trying to do is pass the value of the PageProperty property defined in the aspx page into the user control to set it's UserControlProperty, and then display that value in a textbox in the usercontrol. Can anyone tell me what I've done wrong here?

推荐答案

尝试

UserControlProperty = '<%= this.PageProperty %>'

或考虑阅读: http://support.microsoft.com/kb/307860#1b
有这样的事情的Page.DataBind()和Control.DataBind()。我不是很肯定,如果你应该明确地给他们打电话,但它可能是这样的...

or consider reading: http://support.microsoft.com/kb/307860#1b there's such a thing as Page.DataBind() and Control.DataBind(). I'm not quite sure if you should call them explicitly, but it might be the case...

如果你还想要你的情况,你可以尝试通过字符串来做到这一点:

if you still want your case, you can try to do it through string:

public string UserControlProperty { get; set; }

它完美的作品。

这篇关于ASP.Net声明的价值传递从页面属性到用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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