Can UserControl have pulic functions and can i call them from the page where i have placed the control?
Printable View
Can UserControl have pulic functions and can i call them from the page where i have placed the control?
Of course you can, the best way to see this is to try an example and test it :)
A reflection... what if I have built a search page complete with a search button (all in a user control) and place this on a webpage along with a datagrid where I want the search result to be presented... how do I do that? the datadrid is outside of the control...
can this also be done, OR which is the best way to do it?
kind regards
Henrik
You can do this by the RaiseEvent.
Yes i have tried, but i am not sure how to access a property or a public function i defined in an Usercontrol.Quote:
Originally posted by vbud
Of course you can, the best way to see this is to try an example and test it :)
Any example anyone one?
Thanka
In your usercontrol, at the top underneath the class declaration, you put something like
VB Code:
Public Event DataArrived
In the code for your usercontrol, when you are finished getting the data, you raise the event:
VB Code:
RaiseEvent DataArrived
In the page that has a datagrid on it....
VB Code:
Private Sub MyUserControl_DataArrived(byval Sender as Object, e as EventArgs) Handles MyUserControl.DataArrived MyDataGrid1.DataSource = MyUserControl.Data End Sub
You will need a public property 'Data' of your UserControl that exposes the data as a datatable or something that the grid can bind to.. you could also expose the DataConnection, the SelectCommand, or whatever you need to do..
Any one?Quote:
Originally posted by Danial
Yes i have tried, but i am not sure how to access a property or a public function i defined in an Usercontrol.
Any example anyone one?
Thanka
This is what i have tried, i have created a user control called Menu
and placed a public function called Test();
Now i place this control in default.aspx and i assign this control the ID of say myMenu.
Now i should be able to accessit by myMenu.Test();
But it dont seem to work, what i am doing wrong?
Post your class code..Quote:
Originally posted by Danial
Any one?
Here is my Code for the ClassQuote:
Originally posted by nemaroller
Post your class code..
This is how i am importing itCode:namespace Forum.Net.Control
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for ForumMenu.
/// </summary>
public abstract class ForumMenu : System.Web.UI.UserControl
{
public void Test()
{
}
private void Page_Load(object sender, System.EventArgs e)
{
bool LoggedIn=false;
#region IsloggedIn
try
{
if (Request.Cookies["Username"].Value =="")
{
LoggedIn=true;
}
else
{
LoggedIn=false;
}
}
catch (Exception ex)
{
LoggedIn=false;
}
#endregion
Table tbl = new Table();
TableRow tr= new TableRow();
TableCell td= new TableCell();
if (LoggedIn==false)
{
td.Text="<a href='regiser.aspx'>Register</a> | ";
tr.Controls.Add (td);
}
td= new TableCell();
td.Text="<a href=search.aspx>Search</a> | ";
tr.Controls.Add (td);
td= new TableCell();
td.Text="<a href=GetAllUser.aspx>Members</a> | ";
tr.Controls.Add (td);
td= new TableCell();
td.Text="<a href=faq.aspx>FAQ</a> | ";
tr.Controls.Add (td);
td= new TableCell();
td.Text="<a href=cp.aspx>Control Panel</a> | ";
tr.Controls.Add (td);
td= new TableCell();
if (LoggedIn==false)
{
td.Text="<a href=login.aspx>Login</a>";
}
else
{
td.Text="<a href=logout.aspx>Logout</a>";
}
tr.Controls.Add (td);
tbl.Controls.Add(tr);
this.Controls.Add (tbl);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Now i would like to access the test() function of the usercontrol.Code:<%@ Register TagPrefix="uc1" TagName="ForumMenu" Src="Control/ForumMenu.ascx" %>
Thanks for your help.
Danial
Well, i don't declare references in the aspx side, but in the code behind you would simply drop the control on the page, and the designer writes the code akin to:
(Now im not sure what C# writes on this part.)
VB Code:
Protected WithEvents FormMenu1 As FormMenu
Then its simply a matter of :
VB Code:
FormMenu1 = New FormMenu FormMEnu1.test() '//C# FormMenu formMenu1 = new FormMenu; formMenu1.test();
Hi,Quote:
Originally posted by nemaroller
'//C#
FormMenu formMenu1 = new FormMenu;
formMenu1.test();
[/Highlight]
Thanks for yoru response again. This bit seems to work, meaning i can access the function if i declare the usercontrol through code. But if i place it visually i cant seem to access the properties/function of this control.
How can i do so? Is it available from Code Behind page (preferably at Page_Load)
Thanks for your help.
Anyone ?
Think of a user control as a regular server control, like an asp:TextBox. Until you declare that somewhere in the codebehind, you can't use any properties with it, right? same goes for a UserControl. It needs an ID. If you're using code behind, then that control needs to be declared somewhere in the code behind just like a TextBox server control would need.
So drop your usercontrol on a webform, make sure it has the Runat="server" and ID="MyUserControlID" attributes, then in the code behind make sure to declare that usercontrol:
The name of the variable you pick to represent your usercontrol in code behind must match the value of the ID attribute for that usercontrol in the aspx page for this to work.PHP Code:public class MyPage : Page
{
protected MyUserControl MyUserControlID;
}
Thanks a million pvb, thats what i was looking for.
Cheers.
Danial