Click to See Complete Forum and Search --> : Web User Control - Newbie...(must be)


jpfo
April 21st, 2003, 09:59 AM
***This post was been edited and the question reformulated, since no one replayed yet.

Hi all!
This must be a simple question... :o
I've a Web User Control with a text box and a button. I want to pass the value from the textbox to the aspx form page when the button is clicked.
I dont want to use global vars or Contents.

Is there any way to do this using classes for exemple?

Tks in advance

V. Lorenzo
April 25th, 2003, 11:50 AM
What exactly do you need to do with this edited value? Use the control's value in the same page that it is contained? Use that value from another page?

VLorz

jpfo
April 28th, 2003, 03:13 AM
Yes... thats the all ideia.

Get the value from my textbox on the Web User Control and then call a void on the aspx page (were the control is contained) that will use this value.

Whats the best aproch?
Tks in advance.

V. Lorenzo
April 28th, 2003, 04:05 AM
Hi:

I still don't get it all, but let's do some sample coding...

1- Create a new WebForm page.
2- Insert a Label (named label1) and set it's Text property to 'Type some text :'.
3- Insert a TextBox (named TextBox1) and clear it's Text property (just erase it's content).
4- Insert a Button and double click on it to set it's OnClick event handler.

Notice now that the IDE has inserted a new method for the WebForm class...

5- In the newly created method content, type in the code below :

Label1.Text = string.Format( "Text written : \"{0}\"", TextBox1.Text );
TextBox1.Visible = false;
Button1.Visible = false;


This sample code uses the value typed in the TextBox input control to create a string, whish is then assigned to a Label's text and shown in the page.

Just as another sample of actions that could be done, the TextBox and the Button are 'hidden' (made not visible).

Bye

jpfo
April 28th, 2003, 04:25 AM
Ok Lorenzo... maybe i've not been explicit about the question.

I've a Web User Control (ASCX) with a combox, and the user will select some value from it. When he does and under the SelectedIndexChanged i will trigger some EventHandler to call some void on my main page (ASPX), that will show some details regarding the selection made.

In this void i'll use the value from the combox on my Web User Control.

So...
1. Event Question:
How can i call a procedure from my form using SelectedIndexChanged(combox) located on my Web User Control

2. How can i pass a value from my Web User Control to my Form

Note: Not talking about regular controls, but WebUserControl ( extension ASCX)

V. Lorenzo
April 28th, 2003, 04:49 AM
Hi, again:

Some times it happends to, with the IDE, that I insert a user control into a page (ASCX control) and the IDE editor does not adds the control's reference to the class (the line like 'protected MyControl myControlInstance;'). So, in this cases I'm forced to add it by myself. After that, all the control properties and methods (provided they are declared 'public') are fully available from the Page code.

If you need to call some method from the Page in response to something (a so called Event) that happends inside your user control (e.g. the selected item, or something, has changed, as you say), declare a public event field in the control's class, something like this:

public class...(etc.) MyASCXControlClass
{
(etc.)
public event System.EventHandler MyEvent;
(etc.)
}

In the OnItemChanged (or whatever it's name is) handler for the List, you may add code like :

if (MyEvent != null)
MyEvent( this, System.EventArgs.Empty );

From the control's side, that's all you need to do, but still a bit more coding must be done in the client (ASPX Page) class: Create the handler method in the client class with a signature like :

protected void MyHandler( object sender, System.EventArgs e )
{
// Code that handles the event...and is, as desired, called by the control.
}

Now, in the Page_Init event handler (go to the Page properties and add the event handler if still not added) add the code to assign the Control's event handler as:

myASCXControlInstanceName.MyEvent += new EventHandler( this.MyHandler );

Now it's all set up.

Maybe you need to gain access to the value, text or index of the selected item, in that case you need to declare a public property whos' get{} and set {} methods implement the access to the internal list's values.

VLorz

jpfo
April 28th, 2003, 05:17 AM
Thx Lorenzo.

What you wrote makes sense. I will try it this afternoon and see what cames out. I'll catch you latter on that.

jpfo
April 29th, 2003, 07:09 AM
k... Was a little more hard than i though... had to create my custom Event class with the get/set method, then had to create a custom delegate that knows how to pass is values when i raise the event... beside work well and compile well i always get this debug message: " Could not find type 'Sicont.IEfeitos'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.". Any ideia whys that? Where the code.

*Custom Event class on my Web User Control

public class EfeitoComandoEventArgs: EventArgs{
private string _efeitoID="";

public EfeitoComandoEventArgs(string efeitoID){
_efeitoID = efeitoID;
}
public string EfeitoID{
get{
return _efeitoID;
}
}
}


*Web User Control

public delegate void EfeitoComandoEventHandler (object sender, EfeitoComandoEventArgs e);
public event EfeitoComandoEventHandler EfeitoSeleccionado;

// The method that will raise the event - DropDownList
protected void ddlEfeito_SelectedIndexChanged(object sender, System.EventArgs e){
string EfeitoID=ddlEfeito.SelectedItem.Text.ToString();
EfeitoComandoEventArgs eArgs = new
EfeitoComandoEventArgs(EfeitoID);
OnEfeitoSeleccionado(eArgs);
}

protected virtual void OnEfeitoSeleccionado(EfeitoComandoEventArgs e){
if(EfeitoSeleccionado !=null){
EfeitoSeleccionado(this,e);
}
}


*declaration added on my ASPX page to reference the WebUserControl

protected IEfeitos IEfeitos1;


*line added to InitializeComponent method on my ASPX page

IEfeitos1.EfeitoSeleccionado += new IEfeitos.EfeitoComandoEventHandler(this.Get_Efeito);


*and finnaly the method i called on the ASPX page - Get_Efeito

private void Get_Efeito(object sender,EfeitoComandoEventArgs e){
Response.Write(e.EfeitoID.ToString()); //just a exemple :)
}


tks for yours remarks Lerenzo, helped a lot.