日期:2014-05-19  浏览次数:20517 次

救命啊!如何给用户控件赋值并获取啊?100分全部送上
用户控件   user.ascx(想在里面做一个变量username然后输出这个变量值,里面还有一个下拉选择菜单)

window.aspx(给user.ascx文件里的   username= "张三 ",然后获取ascx文件了里用户选则的那个菜单项的值)


两个文件,该怎么写程序啊,,,


要能运行的程序/不要例子,只要这个问题的程序   谢谢~~嘿嘿

------解决方案--------------------
先沙发
------解决方案--------------------
你把用户控件引到ASPX中的时候浏览这个aspx然后看源代码,你会发现你的用户控件的原来的名字:username 变成了username_id_14524 这类的名称,把这个ID复制下来再程序里调用就可以了。
------解决方案--------------------
在window类中申明一个user类的对象
通过这个对象就可以访问user类里的属性了
------解决方案--------------------
UP@!
------解决方案--------------------
protected WebApplication1.control.cont UserControl1;//与前台aspx中的id要一置.

前台 <uc1:cont id= "UserControl1 " runat= "server "> </uc1:cont>

------解决方案--------------------
user.ascx:
<%@ Control Language= "C# " AutoEventWireup= "true " CodeBehind= "user.ascx.cs " Inherits= "testweb.user " %>
<asp:DropDownList ID= "DropDownList1 " runat= "server ">
<asp:ListItem Selected= "True "> 张三 </asp:ListItem>
<asp:ListItem> 李四 </asp:ListItem>
</asp:DropDownList>

user.ascx.cs:
public partial class user : System.Web.UI.UserControl
{
public string UserName
{
get { return DropDownList1.SelectedValue; }
set { DropDownList1.SelectedValue = value; }
}

protected void Page_Load(object sender, EventArgs e)
{

}
}

(window)default.aspx:
<%@ Page Language= "C# " AutoEventWireup= "true " CodeBehind= "Default.aspx.cs " Inherits= "testweb._Default " %>

<%@ Register Src= "user.ascx " TagName= "user " 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:user id= "User1 " runat= "server ">
</uc1:user>
<asp:TextBox ID= "TextBox1 " runat= "server "> </asp:TextBox>
<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Text= "Button " /> </div>
</form>
</body>
</html>

default.aspx.cs:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
User1.UserName = "李四 ";
}

protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = User1.UserName;
}
}

上面是完整的代码了