How do you pass a value from one user control to another?

There must be an example of this somewhere but if there is I can't find it.

I have a web form containing 2 user controls.

The first displays a list of people. The user selects the person they are interested in and details of that person are to be displayed in the second user control.

What is the best way of passing the selected item from the first user control to the second.

I can do it with session variables, but there must be a better way than that.

thanks

0
kevins1966
6/29/2006 3:26:27 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

6 Replies
615 Views

Similar Articles

[PageSpeed] 54

You can use properties for that.

User Control

private string m_Person;

public string Person
{
    get { return m_Person; }
    set { m_Person = value; }
}

Web Form

<TagPrefix:TagName ID="MyUserControl" Person="Bob" runat="server" />

or

MyUserControl.Person = "Bob";

Then in the user control you can use the m_Person variable to get the value passed to the user control.

Ryan Olshan
ASPInsider | Microsoft MVP, ASP.NET
http://ryanolshan.com

How to ask a question
0
StrongTypes
6/29/2006 3:40:15 PM

Use Model-View-Controller pattern. First user control (View) should call Controller and pass selected person. Controller will store this info in Model and fire an event. The second user control will handle this event and request selected person from Controller.

If MVC is overkilling for your application then use Session or properties. I cannot imagine anything else...

0
delphos
6/29/2006 5:20:11 PM
Hi
Its good you use Properties defined for your user controls as StrongTypes suggested.

Thanks

Mark post(s) as "Answer" that helped you

Electronic Screw
Website||Blog||Dub@i.net
0
e_screw
6/29/2006 5:26:10 PM

Thanks for the reply.

I can see that you can set a property for the second user control which can be set in the tag for that user control or directly assingned in the code behind that user control, however the example uses a value set at design time.

How does a value selected at run time get passed into the second user control?

0
kevins1966
6/30/2006 10:14:06 AM

I should have in cluded in that last entry of mine that it was a reply to StrongTypes suggestion.

 

Thanks

0
kevins1966
6/30/2006 10:16:02 AM
StrongTypes:

<TagPrefix:TagName ID="MyUserControl" Person="Bob" runat="server" />

or

MyUserControl.Person = "Bob";

Then in the user control you can use the m_Person variable to get the value passed to the user control.



He has given you both ways to do it. You pass the values to the other user control depending on the event.

Thanks

Mark post(s) as "Answer" that helped you

Electronic Screw
Website||Blog||Dub@i.net
0
e_screw
6/30/2006 10:28:43 AM
Reply:

Similar Artilces:

Pass value from one User Control to another User Control
I have tried to pass value from one user control to another user control. Because of Page Auto Postback, Contrls are load before page is loaded. So I get the value in 2nd UC after one more post back operation. I have visited many suggessions but I cant solve this. Is anyone can help me to solve this. Thank U mfhossain@gmail.com Muhmmad Fakhrul HossainEmail: mfhossain@gmail.comPhone: +88 01715 111512Web: http://www.mfhossain.info You'd have to do that through the parent probably in it's Page_Load event handler.   userControl1.publicProperty = userControl2.publicProperty;  ...

How to pass control values from one ascx user-control to another???
Our site is set up using a primary default aspx page that is used as the "base page" for everything. On this page, we dynmaically place various user controls at run-time. This gives us a functionality similar to using frames where the outer-frame actually just sits there while various webfiles are loaded inside of it. Works great except for one thing... trying to figure out the best way to pass control names/values accross user controls. When the same usercontrol is posting back to itself this is a non issue. So far most of our functionality has worked well using the local...

want to call one c#.net function in one user control from another user control
hi i have two user controls info.ascx and personal.ascx i have a function like in info.ascx i have a function like showdetails() {   } now i want to access this function from personal.ascx can i do this ? thanks in advance. shwetaThanksshwetaIn a day when don't come across any problem -you can be sure that you are traveling in wrong path  you should better create a class where you will define the "functions" that you need in both files. declare "static" the function and call it like nameofclass.nameoffunction In a web project you can...

Passing Values from a Web Form to Another Web Form
Hello All! I am trying to pass values from one web form to another web form. I know that there are 3 ways of doing this: queryString, Server.Transfer and Session. I am using the Server.Transfer method. But I am not able to create, in my 2nd web form, an instance of my 1st web form. My 1st Web form is named CallTrack.aspx and my 2nd is named Export.aspx. What I mean is something like this: Public value as CallTrack. I can't create it in my 2nd web form because when the intellisense is displayed, there's no CallTrack in the options. Thanks!   How about using cross-...

I have a web user control being used by another web user control, but the one being used will not display the labels in the resx file.
I am using DotNetNuke version 3.13 (might be 3.013) on a Window's XP machine, SP2. I have what I find to be an extremely odd situation. I have a project called TDS.Controls. This project is a web control library. Each control has its own resx file, which is located in the App_LocalResources directory. Each control inherits from Framework.UserControlBase, just like DotNetNuke's User control in DNN\Controls\User.ascx.vb. As a matter of fact, this control is doing the exact same thing I am trying to do. It is a web user control with its own resx file being used by another web user control (See ...

pass value from one user control to another
i have a web page (Page.aspx) that contains a main user control (Main.ascx) which has two other controls within it (A.ascx, B.ascx). I have a text box and a button in A.ascx, when the user clicks the button i want the text in the textbox to be captured and then displayed on B.ascx. I have seen a few discussions on this using RaiseEvent but I'm not sure how to do it. Does anyone have any ideas? check this tutorial in codeproject thanks rasheeda, this post helped me out alot also. http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=115712 ...

Passing values from one web control to another.
I'm trying to pass a value from one user control to another. Basically, the first user control selects an id number and then the second user control takes that id number and updates a record in a database. I have the second control set up to accept values using public string idnumber; public string IDnumber { set { idnumber = value; } } The second user control has two buttons that do postbacks, depending on which is clicked, a value is updated in the database identified by this number. The problem I am having is that before I click th...

To pass values from one user control to another
Hi,       I have a page where there is a user control. inside that user control(let's say Usercontrol1) i have another user control(lets say Usercontrol2). in the usercontrol2 i have a tree and a button. When the page gets loaded first the treeview in the usercontrol2 should not get populated only when i click on a button in the Usercontrol1, then only the treeview in the Usercontrol2 should get populated. also i want to pass the text of a selected treenode when i click on the usercontrol2's button to a textbox in the userc...

Pass Values from a user control to a web form
Hi All,  I am currently trying to add captcha to my webform. I have a question on how to pass a number/string from a .ascx to a .aspx page. The aspx is being used in the .ascx (user control) in an image tag. <asp:Image ID="imgCaptcha" runat="server" ImageUrl="Image.aspx"/> The aspx page is being rendered as a jpeg image. So Inorder for the captcha to be displayed with the image, I need to pass the code from the user control to the aspx page. I have an option to pass the number as a query string or use it in session. But is there any ot...

Pass values from one user control to a user control on a different aspx page
Hello, I have an aspx page titled Search.aspx. Within this page, I have a user control titled Search.ascx. I want a user to input search terms, then on the click of a button, I want to pass the values to another page (SearchResults.aspx)....which will then display the results in a user control. Can someone please give me some suggestions on how to do this? Thanks a lot! Hello, what you can do is: inside ur usercontrol, create a method that returns a dataset or datareader. that is, since u are searching using the usercontrol, then the result of ur search will be return...

How to use a web user control from one web app in another web app?
I have a production web app project (at work) that has custom web user controls in it.I have another web app project for experimenting and I want to use some web controls from the production web app. The big trick is that I need to load the controls dynamically.LoadControl() doesn't seem to work - it complains about loading controls from a different application. I also tried adding a project reference to the production web app and using "UC1 newUC = new UC1();" and then "this.Controls.Add(newUC);", but the controls (like TextBoxes) on the web user control don't seem to exist on the new UC1s....

Trying to pass values from one control to another control
I have 2 controls on my page. CaseList and Email Reviwer. CaseList contain a datagrid that when the button (5th column) is clicked 2 values should be passed to the email revier control. In the email Reviwer Control I have declared: I have: public void sendCaseIdCatId(int catid,int caseid) { ViewState["EmailReviewerCategoryId"] = catid; ViewState["EmailReviewerCaseid"] = caseid; } in my EmailReviwer control   I tried: //Display the email reviewer panel EmailCaseReviewer.Visible=true; string strcatid = e.Item.Cells[1].Text.ToString(); string strcaseid = e.Item.Cells[0].Text.T...

Setting control values from one user control to another
I have two user controls on a page.  Control 1 is view. Control2 is search.  User selects value on Control2 and I want to update Control1(View).  I cannot find syntax to change values on Control1 from Control2.  The following is function on Control1 that is called from Control2 after pageInit and load on Control1.  I have tried many many ways to findControl but all fail. 'Function on Control1 called from Control2 after Control1 PageInit/load:         Public Function loadForm...

Pass data from Web User Control to Web Form
I have a Web Form that contains a Web User Control. The WUC displays dynamic content. I want to change the TITLE property of the Web Form, based on the contents of the WUC. For example, if the WUC displays information about Horses, I want to change the page title to "HORSES" by passing the string to the parent Web Form. I have tried playing with parameters and such, but I can't remember how I did this in the past. PLEASE HELP!~o0 Spin-Monkey Oo~ The best way to do this is by creating a public property on your web user control and accessing that from your web form. Check ...

Web resources about - How do you pass a value from one user control to another? - asp.net.web-forms

FBI Recovers Items From San Bernardino Lake
Washington Post FBI Recovers Items From San Bernardino Lake ABC News FBI agents recovered a number of items from a lake in San Bernardino ...

Finally, the CORRECT Star Wars watching order
Fans want to watch the Star Wars films in the most logical order possible, and the debate rages as to what that order is. We're here to help. ...

Final Draft of Climate Change Accord to Be Released on Saturday
New York Times Final Draft of Climate Change Accord to Be Released on Saturday New York Times LE BOURGET, France — Negotiators on Saturday ...

Reality Editor, an iOS app developed by MIT researchers, lets you connect smart objects by drawing lines ...
John Brownlee / Co.Design : Reality Editor, an iOS app developed by MIT researchers, lets you connect smart objects by drawing lines between ...

Fantasy sports sites get temporary stay to continue operation
Mere hours after a Manhattan judge banned fantasy-sports sites FanDuel and DraftKings from operating in the state pending a trial to determine ...

Anger in Greek refugee camp after Idomeni eviction
At least 1,000 refugees have been sent back to Athens, because they are not from Syria, Iraq or Afghanistan.

Bryan Singer Breaks Down the ‘X-Men: Apocalypse’ Trailer
The X-Men: Apocalypse trailer hit the web today, and based on the 200 comments left on that post , it’s fair to say the response has been mixed ...

Elon Musk Forms Nonprofit To Keep AI Beneficial
The concept of artificial intelligence (AI) is an amazing one. Right now computers only do what they are told, but for a computer to eventually ...

Taliban Attack Near Spanish Embassy in Afghanistan Kills 7
The Nation Taliban Attack Near Spanish Embassy in Afghanistan Kills 7 New York Times KABUL, Afghanistan — At least seven people have been ...

Alibaba to acquire South China Morning Post to deepen its media reach
Apart from the newspaper, Alibaba said it will acquire other media assets of the SCMP Group.

Resources last updated: 12/12/2015 12:37:56 PM