Assigning properties to usercontrols

M

miketayloruk

Hi,

I’m trying to assign a property (record _id) to a user control from
its parent page. I have set up a public property in my user control
and tried to assign a value to it from the parent.

I presumed that once the value had been passed to the user control I
could then display it via a button click as per my example, which
doesn’t appear to work, ie when the button is clicked it does show the
ID value it should have received.

I eventually want to nest usercomtrols a few layers deep and I’m
trying to understand how I can pass information up and down the
layers.

User control:

<%@ Control Language="VB" ClassName="test" %>

<script runat="server">
Private _myid As Integer

Public Property myid() As Integer

Set(ByVal value As Integer)

_myid = value
'display the value recieved
lblset.Text = "_myid being assigned value = " & _myid

End Set

Get
Return _myid
End Get

End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
lblload.Text = "Value at page load event = " & _myid
End Sub

Protected Sub lblafterclick_Click(ByVal sender As Object, ByVal e
As System.EventArgs) Handles btnSee.Click
lblreceived.Text = _myid
End Sub
</script>
<asp:Label ID="lblset" runat="server" Text="When the public property
is changed it will be displayed here" Width="100%"></asp:Label><br />
<br />
<asp:Label ID="lblload" runat="server" Text="The public property at
the page_load event will be displayed here" Width="100%"></
asp:Label><br />
<br />
<asp:Button ID="btnSee" runat="server" Text="Click to see ID Value
received" /><br />
<br />
<asp:Label ID="lblreceived" runat="server"></asp:Label>

Parent Page:

<%@ Page Language="VB" %>

<%@ Register Src="TCV3_controls/test.ascx" TagName="test"
TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Test1.myid = 42
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="2" cellspacing="2">
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Send
ID Value (42)" OnClick="Button1_Click" /></td>
</tr>
<tr>
<td bgcolor="#ffffcc" >
<uc1:test ID="Test1" runat="server" />
</td>
</tr>
</table>
<br />

</div>
</form>
</body>
</html>
 
M

miher

Hi,

When working with ASP.Net You should always remember how it works. Basically
You should think about it as:
Every time when Your browser makes a request to the web server, ASP.Net will
create a new page object(including the controls on it), runs the event
handlers(in case the request is caused by postback ), then renders the
result to html, and send that back. (Of course this is a very basic,
overall description, if You would like to know more about this try
websearching "asp.net page lifecycle").

This means that when You press "Send ID Value.." button (among other things)
this will happen:
1., a new page object, and also a new instance of your control is created
in it (with the default value of 0 fo _myid). This means that the labels in
Your control has empty string as text, since those are newly created too.
2., the event handler for "Send ID Value.." button runs, setting _myid and
"lblset" labels text to the meaning of life (42).
3., the page object is rendered to html, and returned to Your browser. (You
see everything ok so far, seeing the "_myid being assigned value = 42")

When You press "Click to see ID..." button afterwards these will happen
(among other things again)
1., same as step 1 above !
2., the pageload handler and the event handler for "Click to see ID..."
button runs. Both will take the value of _myid (what is the default 0 since
this is a brand new object) and display it in the labels.
3., the page is rendered to html and returned to Your browser.

To keep state beetwen requests, ASP.Net offer many options (again search the
web for "asp.net state management"). In this case I think using the
ViewState would suffice.

Hope You find this useful.
-Zsolt
 
M

miketayloruk

Thanks for your reply,

I think maybe my example code was a little flawed because it involved
two hits to the server, in my actual project I'm trying to bind the
user control inside a datalist and there is only one round trip to the
server.

I'll do some more test and if I cant figure it out I'll post a better
example.

Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top