User Control an ID

  • Thread starter Thread starter Matej Kavèiè
  • Start date Start date
M

Matej Kavèiè

Hello, i have one problem.
I use loadcontrol in default.aspx page. On page i have placeholder and on
load i with loadcontral load dynamic some user control.
Some times i must load same user control twice, like test.ascx, ockey thats
no problem. But now in test.ascx is loaded twice so how can i now
determinete in load function in user control whic one is. I also want to
create that when i load user contol in default.aspx write some parameters in
some property maybe in session[], and than read that properties but i must
read right one, if the user control is difrent then i yust write on load
session["test"]="first" session["test2"]="second" ockej on load of user
control test.asc i youst read property from session["test"] and user control
test2.ascx read from session["test2"]. This is ok but when i have one user
control loaded twice how can now write difrent parameter for sam user
control...

thanks
 
Matej,
I hope I can answer your questions or atleast help a little bit.

You didn't provide us with any code, so let's say it looks something like
this:

Dim c1 as Control = Page.LoadControl("test.ascx")
Page.Controls.Add(c1)
Dim c2 as Control = Page.LoadControl("test.ascx")
Page.Controls.Add(c2)

Now, the solution to your problem is understanding that c1 and c2, while
both representing the same user control, are actually two separate instance
of a class. The trick to your parameter issue is simple, in the codebehind
of test.ascx, add a propeprty:

private _parameter as string
Public Property Parameter() as string
get
return _parameter
end get
set (value as string)
_parameter = value
end set


Then, back in your page, change your code to this:
Dim c1 as Test= ctype(Page.LoadControl("test.ascx"), test)
c1.Parameter = "first"
Page.Controls.Add(c1)
Dim c2 as Test= ctype(Page.LoadControl("test.ascx"), test)
c2.Parameter = "second"
Page.Controls.Add(c2)

Hope that helps.

Karl
 
Hi Matej:

A clean approach would be to add some public properties to the user
control. When the control is dynamically created you could set these
properties on the control. This approach means you dont have to put
data in the Session for later, and the control's do not need to figure
out in what order they were created.

Does this make sense?
Does this work for your requirements?
 
Thanks i do with public property in user control i create my own derived
user control, thanks for advice


Scott Allen said:
Hi Matej:

A clean approach would be to add some public properties to the user
control. When the control is dynamically created you could set these
properties on the control. This approach means you dont have to put
data in the Session for later, and the control's do not need to figure
out in what order they were created.

Does this make sense?
Does this work for your requirements?

--
Scott
http://www.OdeToCode.com

Hello, i have one problem.
I use loadcontrol in default.aspx page. On page i have placeholder and on
load i with loadcontral load dynamic some user control.
Some times i must load same user control twice, like test.ascx, ockey
thats
no problem. But now in test.ascx is loaded twice so how can i now
determinete in load function in user control whic one is. I also want to
create that when i load user contol in default.aspx write some parameters
in
some property maybe in session[], and than read that properties but i must
read right one, if the user control is difrent then i yust write on load
session["test"]="first" session["test2"]="second" ockej on load of user
control test.asc i youst read property from session["test"] and user
control
test2.ascx read from session["test2"]. This is ok but when i have one user
control loaded twice how can now write difrent parameter for sam user
control...

thanks
 

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

Back
Top