I am trying to write. 1 aspx page containing 2 user controls. The Page contains 1 button, Control 1 contains 1 Text Box, Control 2 contains on Label.
The user types something into the Text Box on Control 1, presses the button on the page, and text is displayed in the Label on Control 2. I will post a sample of what I have shortly.
Can anyone help me do this as a last ditch effort to understand both Page to User Control Communication as well as User Control to User Control Communication?Umm? Color me confused but I don't understand?
Ok a comment used to be in between my first two comments but its been delete so it looks a little stupid.
Anyway heres the beginnings of what I have:
Page
<%@. Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Communication.WebForm1" %>
<%@. Register TagPrefix="uc1" TagName="Bottom" src="http://pics.10026.com/?src=Bottom.ascx" %>
<%@. Register TagPrefix="uc1" TagName="Top" src="http://pics.10026.com/?src=Top.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout"
<form id="Form1" method="post" runat="server">
<uc1:Top id=Top1 runat="server"></uc1:Top
<br>
<uc1:Bottom id=Bottom1 runat="server"></uc1:Bottom
<asp:Button id=Button1 style="Z-INDEX: 101; LEFT: 64px; POSITION: absolute; TOP: 328px" runat="server" Text="Button"></asp:Button
</form
</body>
</HTML>
Top Control
<asp:TextBox id=TextBox1 runat="server"></asp:TextBox>
Bottom Control
<asp:Label id=Label1 runat="server" Width="224px" Height="40px">Label</asp:Label>
So what is the best way to go about this I mean do I set up public properties for the controls and pass the information through the page somehow? I really dont know what the best way to do this is in this situation.
Not sure where your confusion lies.
When a page with user controls is rendered, all the user control's child controls are rendered along with the child controls of other user controls, all in the same page.
The browser has no concept of how you encapsulate all of these controls. All it knows is what the Javascript that is generated by ASP.Net (either the standard stuff that .Net throws in, or your custom stuff) is telling it to do. And, usually, what that is is, when there is a postback, to send a whole bunch of information back to the server. This information includes things like the current values of certain properties of the controls that were rendered, and information about any event that may have occurred client-side since the last postback (not all events trigger a postback).
When the server gets all this information, it applies it to the server-side code in your page. That code will include anything that was encapsulated by your user controls.
Does this help?
Martin
Sorry, our last posts crossed. Click the tutorial tab at the top of this page. On the left there is a section 'Web Forms User Controls', which has a section 'Exposing User Control Properties'. Which will eventually lead you tothis example.
(so much for this site being bookmarkable!)
Martin
Ok I can not do this
<script language="C#" runat="server"
void SubmitBtn_Click(Object sender, EventArgs E) {
MyMessage.Text = "Message text changed!";
MyMessage.Color = "red";
}</script>
what magic is there that is supposed to make this work. MyMessage does not exist it doesnt see user control properties the way they are showing it. Unless they have PFM VS 2k3
Arrggh is there a single working example that actually shows GETTING A VALUE from a control on a User Control AND THEN taking that value and giving it to ANOTHER USER CONTROL!!!!!!!!!!!
5.00 paypal if someone shows a freaking working example of this
protected void SubmitBtn_Click(Object sender, EventArgs E)
{MyLabel.Text += "Shipping Address: "
+ ShipAddr.Address + ", "
+ ShipAddr.City + ", "
+ ShipAddr.State + ", "
+ ShipAddr.Zip + "<br>";MyLabel.Text += "Billing Address: "
+ BillAddr.Address + ", "
+ BillAddr.City + ", "
+ BillAddr.State + ", "
+ BillAddr.Zip + "<br>";
}
Where BillAddr and ShipAddr are user controls. This doesn't fly in VS ? works just fine in the Web Matrix.
You have placed your control on the page as
<uc1:Top id=Top1 runat="server"></uc1:Top>That is the control that the page can see. The control contains a TextBox that has a Text property. However, because you have encapsulated the TextBox in your user control, it has been abstracted. The TextBox Text property is not automatically made available as a property of your user control.
If you were designing a custom control I would say create a Text property for the custom control and assign it with the value of TextBox Text. I'm not sure if you can do that with a user control.
So, you may like to try this
void Page_Load()
{
string str = Top1.FindControl("TextBox1").Text;
}
0 comments:
Post a Comment