关于两个自定义控件的取值问题
一个.aspx的页面中,用到了两个用户控件,其中想做的到A控件有一个按钮,点击的时候获取到B控件中的一个textbox的值。因为在生成的时候名字会改变,用findcontrol的时候名字该如何写呢?
另外像这种问题有几种解决的办法呢? --------------------编程问答-------------------- 直接写就行
FindControl("textbox1") --------------------编程问答-------------------- 你说在主页面中? --------------------编程问答-------------------- Refer:
http://www.cnblogs.com/insus/archive/2013/01/26/2878213.html
--------------------编程问答-------------------- --------------------编程问答-------------------- 3楼的做法挺好的,但是我觉得有点麻烦。我的做法是给包含button的UcButton控件添加一个属性[Content],来获取另一个UcText控件中TextBox的值。 代码如下:
UcButton.ascx代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UcButton.ascx.cs" Inherits="FrameSetDemo.UserControlTest.UcButton" %>
<asp:Button ID="btnSubmit" runat="server" Text="提交" OnClick="btnSubmit_Click" />
UcButton.ascx.cs代码:
public partial class UcButton : System.Web.UI.UserControl
{
public string Content { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), "alert", "alert('" + Content + "');", true);
}
}
UcText.ascx代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UcText.ascx.cs" Inherits="FrameSetDemo.UserControlTest.UcText" %>
<asp:TextBox ID="txtContent" runat="Server"></asp:TextBox>
主页面--WebForm1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FrameSetDemo.UserControlTest.WebForm1" %>
<%@ Register src="~/UserControlTest/UcButton.ascx" tagname="UcButton" tagprefix="uc1" %>
<%@ Register Src="~/UserControlTest/UcText.ascx" TagName="UcText" TagPrefix="uc2" %>
<!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>
<fieldset>
<legend>
UcButton
</legend>
<uc1:UcButton ID="UcButton1" runat="server" />
</fieldset>
<fieldset>
<legend>
UcText
</legend>
<uc2:UcText ID="UcText1" runat="server" />
</fieldset>
</div>
</form>
</body>
</html>
WebForm1.aspx.cs代码:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox txtContent = (TextBox)this.Page.FindControl("UcText1").FindControl("txtContent");
if (txtContent != null)
UcButton1.Content = txtContent.Text;
}
}
不知各位觉得这个解决方法如何?
补充:.NET技术 , ASP.NET