473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

aspx and ascx

rom
I have an user control inside me aspx page and I want a
button in the user control to call a sub in the aspx page.
is it possible?
I tried to several things like:
1. just call the className.SubName but I get error "Object
reference not set to an instance of an object...."
2. I tried:
Dim cls As myClassName = New myClassName
cls.SubName
this way I can reach the sub, but when I'm inside the sub,
if try to write to a label control on that page (the
sub+label are in the aspx page) then I get the same error
message...Object reference not set......

Any Ideas?
Nov 18 '05 #1
3 1820
hi
try to import this aspx page to your aspx page
import yourproject.yourpage
and then try to get your procedure
"rom" wrote:
I have an user control inside me aspx page and I want a
button in the user control to call a sub in the aspx page.
is it possible?
I tried to several things like:
1. just call the className.SubName but I get error "Object
reference not set to an instance of an object...."
2. I tried:
Dim cls As myClassName = New myClassName
cls.SubName
this way I can reach the sub, but when I'm inside the sub,
if try to write to a label control on that page (the
sub+label are in the aspx page) then I get the same error
message...Object reference not set......

Any Ideas?

Nov 18 '05 #2
1. The Html Script (DatePicker.ascx):

<%@ Control Language="c#" AutoEventWireup="true"
Codebehind="DatePicker.ascx.cs" Inherits="dotnet.DatePicker"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<FONT id="FONT1" face="??" runat="server">
<DIV style="WIDTH: 153px; POSITION: relative; HEIGHT: 20px"
ms_positioning="GridLayout"
runat="server">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px" runat="server"
Width="128px" Height="24px"></asp:TextBox>
<asp:Calendar id="Calendar1" style="Z-INDEX: 102; LEFT: 0px; POSITION:
absolute; TOP: 24px" runat="server"
Width="152px" Height="48px" BorderWidth="1px" BackColor="#FFFFCC"
DayNameFormat="FirstLetter"
ForeColor="#663399" Font-Size="8pt" Font-Names="Verdana"
BorderColor="#FFCC66" ShowGridLines="True"
Visible="False">
<TodayDayStyle ForeColor="White" BackColor="#FFCC66"></TodayDayStyle>
<SelectorStyle BackColor="#FFCC66"></SelectorStyle>
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC"></NextPrevStyle>
<DayHeaderStyle Height="1px" BackColor="#FFCC66"></DayHeaderStyle>
<SelectedDayStyle Font-Bold="True"
BackColor="#CCCCFF"></SelectedDayStyle>
<TitleStyle Font-Size="9pt" Font-Bold="True" ForeColor="#FFFFCC"
BackColor="#990000"></TitleStyle>
<OtherMonthDayStyle ForeColor="#CC9966"></OtherMonthDayStyle>
</asp:Calendar>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 128px; POSITION:
absolute; TOP: 0px" runat="server"
Width="24px" Text="?" Height="24px"
Font-Size="XX-Small"></asp:Button></DIV>
</FONT>

2. DatePicker.ascx.cs :

using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace dotnet
{
public class DatePicker:System.Web.UI.UserControl
,System.Web.UI.INamingContainer
{
protected System.Web.UI.WebControls.TextBox TextBox1;
public System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlGenericControl FONT1;
public System.Web.UI.WebControls.Calendar Calendar1;
public string ShortDate
{
get {return this.TextBox1.Text;}
set {this.TextBox1.Text = value;}
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Calendar1.SelectionChanged += new
System.EventHandler(this.Calendar1_SelectionChange d);
this.Button1.Click += new System.EventHandler(this.Button1_Click);

}

private void Button1_Click(object sender, System.EventArgs e)
{
if(this.Calendar1.Visible == false)
{
this.Calendar1.Visible = true;
this.Button1.Text = "?";
}
else
{
this.Calendar1.Visible = false;
this.Button1.Text = "?";
this.TextBox1.Text = this.Calendar1.SelectedDate .ToShortDateString();
}
}

private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
this.TextBox1.Text = this.Calendar1.SelectedDate .ToShortDateString();
this.Calendar1.Visible = false ;
this.Button1.Text = "?";
}
}
}
3. The sub what you want:
private void Button1_Click(object sender, System.EventArgs e)
{
String date1;
.......
date1 =
(((System.Web.UI.WebControls.TextBox)(this.Page.Co ntrols[0].FindControl("Dat
epicker1").FindControl("TextBox1"))).Text);
.......
}
Nov 18 '05 #3
May be you could try this way.

1. By pressing the button in the user control, you call a javascript function.
2. Then, in the javascript, you could call the functions in the script.

If you need to pass in some of the parameters, set the parameters into
ViewState parameter in user control and retrieve it in the function in aspx
page

If you need to return something from the aspx page, e.g. dataset, datatable,
string etc. Use back the same mechanism, the script and viewstate.

or a better suggestion, make your function in a class, in your aspx, call
the function from class, and also you can call it from your user control.

Let me know whether it works. Thanks

"rom" wrote:
I have an user control inside me aspx page and I want a
button in the user control to call a sub in the aspx page.
is it possible?
I tried to several things like:
1. just call the className.SubName but I get error "Object
reference not set to an instance of an object...."
2. I tried:
Dim cls As myClassName = New myClassName
cls.SubName
this way I can reach the sub, but when I'm inside the sub,
if try to write to a label control on that page (the
sub+label are in the aspx page) then I get the same error
message...Object reference not set......

Any Ideas?

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: dh | last post by:
As a simple example, I can't seem to be able to reference a label called "lblOutput" that exists in my test.aspx page from within a User control, TestControl.ascx. I am stumped. Any help is...
2
by: Luboą ©lapák | last post by:
Hi, I have the main page "default.aspx". I have also UserControls "content1.ascx" and "content2.ascx". How can I write in "default.aspx" this: if true then show content1.ascx else show...
4
by: BH | last post by:
I'm looking at the source code of the ASP.NET forum sample application. It has the "code-behind" classes compiled into a separate DLL, totally separated from the aspx/ascx files. Adding the class...
3
by: Tom | last post by:
Hi, I have an index.aspx page which includes top.aspx, left.aspx, main.aspx and bottom.aspx. In the left.aspx, there is a login web control - login.ascx. It keeps session of username and role...
4
by: Nedu N | last post by:
Hi, I want to put a HTML table on aspx webform so that i can place my header, menu, footer user controls appropriately. I got 3 user controls header.ascx menu.ascx footer.ascx I trying to...
2
by: Peter Jackson | last post by:
I'm using v2 of the UIP App. Block. I've created all my .aspx pages, all of which contain .ascx user controls. The .ascx user controls provide the standard server controls (i.e., LinkButtons, etc.)...
2
by: InHyuk, Song | last post by:
I want to include ascx file, it displays some contents. therefore I drag the ascx file from the solution explorer into the design window of the aspx file. However, in case that ascx file(not...
2
by: Ric | last post by:
im new to asp.net. from what i understand, you have the aspx file (presentation), user-control(ascx file), code-behind(vb file) and components(compiled vb and dll files). the aspx file contains a...
2
by: mharness | last post by:
Hello, I've tried a number of examples showing how to read the properties of a user control from an aspx file where the code is on the html view of the form but I can't figure out how to read...
6
by: Martin Eyles | last post by:
Hi, I have a .aspx page which has a .ascx file included through the lines <%@ Register TagPrefix="aspcustom" TagName="menu" Src="Menu.ascx" %> and <aspcustom:menu id="Menu1"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.