你如何得到另一个用户控件中的文本框? [英] How do you get at the Textbox that is in another User Control?

查看:51
本文介绍了你如何得到另一个用户控件中的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP Web 2005 express。


我在下面创建了用户控件,我列出了html代码。我

有2个文本框。一个是txtCompanyName。


主页面上有2个面板和一个按钮。 Panel1包含

用户control1,它是一个具有txtCompanyName的表单。 Panel2

包含另一个用户control2,这是另一种形式。


当我点击按钮时,我想用文本框做点什么
$ b使用C#在代码隐藏文件中使用$ b txtCompanyName。但是

背后的代码没有看到文本框,因为它在另一个aspx文件中,而不是

代码隐藏文件。那么如何设置它以便它看到文本框?


我是asp.net和.net的新手。我感到很沮丧,我不能用b $ b想出一个查看这个文本框的方法。必须有办法看到

每个文件中的所有内容。


谢谢,

Al

<%@ Control Language =" C#" AutoEventWireup =" true"
CodeFile =" Companies.ascx.cs"继承= QUOT;公司" %>

< table style =" width:348px">


< caption style =" color:#ffffff; background-color:#6666cc;

font-weight:bold; font-family:Arial;" align =" center">

新客户信息< / caption>


< tr>

< td style =" width:544px; font-family:Arial;身高:6px;

background-color:buttonface;"对齐= QUOT;右" bordercolor =" #ffffff"

bgcolor =" buttonface">

< asp:Label ID =" lblCompanyName" RUNAT = QUOT;服务器" Text =" Company

名称:"宽度= QUOT; 150像素" Font-Names =" Arial">< / asp:Label>< / td>

< td style =" width:318px;身高:6px; bordercolor =" #ffffff"

bgcolor =" buttonface">

< asp:TextBox ID =" txtCompanyName" runat =" server"

CausesValidation =" True" Width =" 220px">< / asp:TextBox>< / td>

< asp:RequiredFieldValidator ID ="

RequiredFieldValidator1"

runat =" server" ErrorMessage =必须输入公司名称

ControlToValidate =" txtCompanyName">< / asp:RequiredFieldValidator>< td

style =" height:6px ;宽度:3px; bordercolor =" #ffffff">


< tr>

< td style =" width:544px; font-family:Arial; background-color:

buttonface;身高:6px;对齐= QUOT;右" bgcolor =" buttonface">

< asp:Label ID =" lblUniqueCode" RUNAT = QUOT;服务器" Text =" Unique

客户代码:"宽度= QUOT; 150像素" Font-Names =" Arial">< / asp:Label>< / td>

< td style =" width:318px" bgcolor =" buttonface">

< asp:TextBox ID =" txtUniqueCode" RUNAT = QUOT;服务器" Wrap =" False"

Width =" 220px">< / asp:TextBox>< / td>

< / tr>


< / table>

I am using ASP Web 2005 express.

I created the user control below and I am listing the html code. I
have 2 textboxes. One is txtCompanyName.

The main page has 2 panels on it and a button. Panel1 contains the
user control1 which is a form that has the txtCompanyName. Panel2
contains another user control2 which is another form.

When I click the button, I want to do something with the textbox
txtCompanyName in the code behind file using C#. But the code behind
does not see the textbox, because it is in another aspx file, not a
code behind file. So how do I set this up so that it sees the textbox?

I am new to asp.net and .net. I am getting frustrated that I can not
come up with a way to see this textbox. There must be a way to see
everything in every file.

Thanks,
Al

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="Companies.ascx.cs" Inherits="Companies" %>
<table style="width: 348px">

<caption style="color: #ffffff; background-color: #6666cc;
font-weight: bold; font-family: Arial;" align="center">
New Client Information</caption>

<tr>
<td style="width: 544px; font-family: Arial; height: 6px;
background-color: buttonface;" align="right" bordercolor="#ffffff"
bgcolor="buttonface">
<asp:Label ID="lblCompanyName" runat="server" Text="Company
Name:" Width="150px" Font-Names="Arial"></asp:Label></td>
<td style="width: 318px; height: 6px;" bordercolor="#ffffff"
bgcolor="buttonface">
<asp:TextBox ID="txtCompanyName" runat="server"
CausesValidation="True" Width="220px"></asp:TextBox></td>
<asp:RequiredFieldValidator ID="
RequiredFieldValidator1"
runat="server" ErrorMessage="Must Enter Company Name"
ControlToValidate="txtCompanyName"></asp:RequiredFieldValidator><td
style="height: 6px; width: 3px;" bordercolor="#ffffff">

<tr>
<td style="width: 544px; font-family: Arial; background-color:
buttonface; height: 6px;" align="right" bgcolor="buttonface">
<asp:Label ID="lblUniqueCode" runat="server" Text="Unique
Client Code:" Width="150px" Font-Names="Arial"></asp:Label></td>
<td style="width: 318px" bgcolor="buttonface">
<asp:TextBox ID="txtUniqueCode" runat="server" Wrap="False"
Width="220px"></asp:TextBox></td>
</tr>

</table>

推荐答案

您必须在用户控制代码中添加一个访问者后面有

访问textControl对象。


问候,

Tasos

You have to put an accessor in your user control code behind to have
access to the textControl object.

Regards,
Tasos


Tasos,


什么是访问者?我告诉过你我是新人。


谢谢,

Al

Tasos,

What is an accessor? I told you I am new.

Thanks,
Al


Tasos ,


好​​的,我知道现在的访问者是什么。我在下面设置了代码。


现在我收到错误:

对象引用未设置为对象的实例。


我以为我实现了它。代码如下。


错误在这一行:uc2.CompanyName2 = uc1.CompanyName1


--------- ----------------------

公共类WebUserControl2

继承System.Web.UI.UserControl


#Region" Web窗体设计器生成的代码


''Web窗体设计器需要此调用。

< System.Diagnostics.DebuggerStepThrough()> Private Sub

InitializeComponent()

End Sub

受保护的事件uc1 As WebUserControl1

受保护的事件uc2 As WebUserControl2

Protected WithEvents uc1TextBox1 As new

System.Web.UI.WebControls.TextBox

受保护的事件uc2TextBox1为新的

System.Web.UI.WebControls.TextBox


受保护的WithEvents Button1 As System.Web.UI.WebControls.Button

''Protected WithEvents uc1TextBox1 As New


''注意:Web需要以下占位符声明

表单设计器。

''做不删除或移动它。

私有designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Init

''CODEGEN:Web表单设计器需要此方法调用

''不要使用代码编辑。

InitializeComponent()

结束子


#End Region


公共财产CompanyName2()As String

获取

返回uc2TextBox1.Text

结束获取

Set(ByVal Value作为字符串)

uc2TextBox1.Text =价值

结束集

结束物业


私人子Page_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load

''将用户代码置于此处初始化页面

End Sub


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click


uc2.CompanyName2 = uc1.CompanyName1

End Sub

结束班级


所以有什么不对?


看起来很简单。请帮忙!


谢谢,

Al

Tasos,

Ok, I know what an accessor is now. I set up the code below.

Now I get the error:
Object reference not set to an instance of an object.

I thought I instanciated it. Code is below.

Error is on this line: uc2.CompanyName2 = uc1.CompanyName1

-------------------------------
Public Class WebUserControl2
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

''This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents uc1 As WebUserControl1
Protected WithEvents uc2 As WebUserControl2
Protected WithEvents uc1TextBox1 As New
System.Web.UI.WebControls.TextBox
Protected WithEvents uc2TextBox1 As New
System.Web.UI.WebControls.TextBox

Protected WithEvents Button1 As System.Web.UI.WebControls.Button
''Protected WithEvents uc1TextBox1 As New

''NOTE: The following placeholder declaration is required by the Web
Form Designer.
''Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
''CODEGEN: This method call is required by the Web Form Designer
''Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Public Property CompanyName2() As String
Get
Return uc2TextBox1.Text
End Get
Set(ByVal Value As String)
uc2TextBox1.Text = Value
End Set
End Property

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
''Put user code to initialize the page here
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

uc2.CompanyName2 = uc1.CompanyName1
End Sub
End Class

So what is wrong?

It seems simple enough. Please help!

Thanks,
Al


这篇关于你如何得到另一个用户控件中的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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