关于两个自定义控件的取值问题

bigevolution 2013-01-26 03:34:49
一个.aspx的页面中,用到了两个用户控件,其中想做的到A控件有一个按钮,点击的时候获取到B控件中的一个textbox的值。
因为在生成的时候名字会改变,用findcontrol的时候名字该如何写呢?
另外像这种问题有几种解决的办法呢?
...全文
164 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hw2822517 2013-01-27
  • 打赏
  • 举报
回复
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; } } 不知各位觉得这个解决方法如何?
夜色镇歌 2013-01-26
  • 打赏
  • 举报
回复
bigevolution 2013-01-26
  • 打赏
  • 举报
回复
引用 1 楼 hjywyj 的回复:
直接写就行 FindControl("textbox1")
你说在主页面中?
  • 打赏
  • 举报
回复
直接写就行 FindControl("textbox1")

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧