|
-
Apr 1st, 2004, 11:47 PM
#1
Usercontrol [Resolved]
Can UserControl have pulic functions and can i call them from the page where i have placed the control?
Last edited by Danial; Apr 15th, 2004 at 11:51 AM.
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
Apr 2nd, 2004, 04:45 AM
#2
-
Apr 2nd, 2004, 05:06 AM
#3
Frenzied Member
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
-
Apr 2nd, 2004, 05:15 AM
#4
Frenzied Member
You can do this by the RaiseEvent.
-
Apr 3rd, 2004, 10:52 AM
#5
Originally posted by vbud
Of course you can, the best way to see this is to try an example and test it
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
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
Apr 3rd, 2004, 01:23 PM
#6
I wonder how many charact
In your usercontrol, at the top underneath the class declaration, you put something like
In the code for your usercontrol, when you are finished getting the data, you raise the event:
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..
-
Apr 4th, 2004, 03:08 PM
#7
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
Any one?
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?
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
Apr 4th, 2004, 06:54 PM
#8
I wonder how many charact
Originally posted by Danial
Any one?
Post your class code..
-
Apr 4th, 2004, 07:00 PM
#9
Originally posted by nemaroller
Post your class code..
Here is my Code for the Class
Code:
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
}
}
This is how i am importing it
Code:
<%@ Register TagPrefix="uc1" TagName="ForumMenu" Src="Control/ForumMenu.ascx" %>
Now i would like to access the test() function of the usercontrol.
Thanks for your help.
Danial
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
Apr 4th, 2004, 10:43 PM
#10
I wonder how many charact
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();
-
Apr 5th, 2004, 09:02 PM
#11
Originally posted by nemaroller
'//C#
FormMenu formMenu1 = new FormMenu;
formMenu1.test();
[/Highlight]
Hi,
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.
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
Apr 13th, 2004, 01:27 PM
#12
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
Apr 14th, 2004, 10:44 AM
#13
Hyperactive Member
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:
PHP Code:
public class MyPage : Page
{
protected MyUserControl MyUserControlID;
}
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.
-
Apr 15th, 2004, 11:50 AM
#14
Thanks a million pvb, thats what i was looking for.
Cheers.
Danial
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|