Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Executing code in container page from user control

Status
Not open for further replies.

MdotButler

Programmer
Jan 18, 2006
104
US
I have a user control that acts as a lookup button in that when clicked it presents the user with a list of choices from a database query. The user then selects the row they desire.

The easy part is I can set a hidden control or public variable to the key of the record they selected.

What I want to do as part of this click event is to then use this key in the container page, read the selected row and populate additional text boxes on the container page.

How do I execute or trigger the execution of code in the container page when the button is clicked in the user control?

TIA
Mark
 
I assume what you want is that when a user selects a row in the user control (after the click and db look up), then the conatining page uses the value(s) from that row? Is that correct?
 
Basically yes. The user control has a filtered grid with a select button on each row. When the select button is pressed I get the key value from the grid and store it to a hidden control (for now).

The container page needs to then have a routine run that uses this key, re-reads the record corresponding to the selected row and populate a number of controls on the container page.
 
Insted of a button, I would use a link button. Set the URL of the link button to the container page + a querystring parameter of the row ID. Then in the page_load of the containing page, check for that querystring parameter. If it exists, then call a sub or function to do what you want.
 
That would work except that it would throw away anything else the user entered on the form. I plan to do this for many lookups on the same container form using different controls for each lookup.

I was using comboboxes for these fields and in the "SelectedIndexChanged" event I would query the database and populate the form. Many of the lookup tables have since become very large and populating the combobox using a "SqlDataSource" made for too much information being sent to the client.

I really need to trigger an event in the container page so I do not loose anything keyed into any of the other fields on the form.
 
Sounds like a job for AJAX.
And don't use the datasource controls.
 
it's all about encapsulation. (which is what the link above is getting at). a user control should know nothing about the specific page it's contained in. in addition to that, the page should know nothing about the internal workings of the user control.

both should use a contract to communicate with one another. for example the user control could have public properties and the page is responsible to get/set the user control is responsible for where these values come from and go.
here is a really simple example of encapsulating a user controls textbox from the calling page.
Code:
<cc1:MyControl id="TheControl" runat="server" />
Code:
public void Page_Load(object sender, EventArgs e)
{
  if(IsPostBack) return;
  TheControl.SomeText = "foo";
}
Code:
<asp:textbox id="TheTextBox" runat="server"/>
Code:
public string SomeText
{
   get { return TheTextBox.Text; }
   set { TheTextBox.Text = value; }
}
by encapsulating the details of how/where values come from you can easily add the user control to any page without the user control needing to know what specific page it's on.
for example. you can now change TheTextBox to another control. dropdownlist, label, etc. and the page doesn't care. alternatively the page could change setting the value to a session value, another control, query string. database call, etc.

the less your objects know about each other the easier it is to adapt the system to new/changing requirements.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Absolutely correct. The test I put together actually has the same user control on the container page twice. The user control has a magnifying glass (lookup) button which is the only user visable portion of the control.

Once the button is pressed I present a filtered grid which has a select button on each row. The user then selects the appropriate row and the buttons click method fires and invokes the event in the container page.

This user control can now be dropped onto any page. All I need to do is set the address of the routine to execute in the container page in the user control.

So far it looks good....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top