passing parameter to user control

yaniv

Centurion
Joined
Apr 15, 2002
Messages
167
Location
israel
how do i pass a parameter to user control? i know i should use something like this-
<uc1:topmenu id="Topmenu1" source="menu" runat="server"></uc1:topmenu>

where "source" is the parameter name and menu is the content. but how do i use it?

i want "source" to be the name of a folder for images so i tried this:

ImageUrl=source + "link1.gif"

but it didn't recognize it.

how do i do it?
 
yaniv said:
how do i pass a parameter to user control?
So, source is a string property of your topmenu class?

1. add a HTMLInputHidden to your class def.
2. Make a property to set its value
3. Define an event for it to load its value into view state on render (not sure if this is the best way to do this, I couldnt find any documentation or examples but this should work. . . I forget why I did this and not just set view state in the property, but there was a reason! I gotta practice documentation!!!)
4. Redefine your constructor to set the hidden control's event and add the hidden control to the Wrapping class' Controls Property
5. override the oninit to define a uniques ID for the hidden control. Do this in on init as you are using the wrappers Unique ID as a base and it isnt set until after init is called
6. Override the wrapper's render method to render the Hidden input. . . add a parameter runat=server before rendering


PHP:
 public class TopMenu : System.Web.UI.WebControls.WebControl
  
 {
  private System.Web.UI.HtmlControls.HtmlInputHidden source = new System.Web.UI.HtmlControls.HtmlInputHidden();

  protected void Source_PreRender(object sender, EventArgs e)
  {
   ViewState[((System.Web.UI.HtmlControls.HtmlInputHidden)(sender)).ID] = ((System.Web.UI.HtmlControls.HtmlInputHidden)(sender)).Value;
   source.Text = ((System.Web.UI.HtmlControls.HtmlInputHidden)(sender)).Value;
  }

  public String Source
  {
   get
   {
	return  source.Value; 
   }
   set
   {
	source.Value = Value;
   }
  }
 
  public TopMenu(): base()
  {
   source.PreRender += new EventHandler(Source_PreRender);
   Controls.Add(source);
  }
 
  protected override void OnInit(EventArgs e)
  {
   base.OnInit(e);
   source.ID  =  this.UniqueID + "Source";
  }  
  
  protected override void Render(HtmlTextWriter output)
  {
   source.Attributes.Add("runat","server");
   source.RenderControl(output);
   base.RenderControl(output);
  } 
 }

This code is hacked from something I just wrote for a job. . . might not work as written here, but I think you will get the idea. The numbered items at the top need to be addressed for the class to work.

I have been preparing a tutorial on developing a WebControl for rendering an <OBJECT> element. If anyone has any input as to working with the ViewState of the wrapped element - please reply!!!
 
Fixed. . . no event -

CLEANER. . . NO EVENT

1. add a HTMLInputHidden to your class def.
2. Make a property to set its value and the viewstate
3. Redefine your constructor to add the hidden control to the Wrapping class' Controls Property
4. override the oninit to define a uniques ID for the hidden control. Do this in on init as you are using the wrappers Unique ID as a base and it isnt set until after init is called
5. Override the wrapper's render method to render the Hidden input. . . add a parameter runat=server before rendering


PHP:
public class TopMenu : System.Web.UI.WebControls.WebControl
 
{
private System.Web.UI.HtmlControls.HtmlInputHidden source = new System.Web.UI.HtmlControls.HtmlInputHidden();
 
public String Source
{
get
{
	return source.Value; 
}
set
{
	ViewState[source.UniqueID] = value 
	source.Value = Value;
}
}
 
public TopMenu(): base()
{
Controls.Add(source);
}
 
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
source.ID = this.UniqueID + "Source";
} 
 
protected override void Render(HtmlTextWriter output)
{
source.Attributes.Add("runat","server");
source.RenderControl(output);
base.RenderControl(output);
} 
}
 
Back
Top