aspx and ascx

R

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?
 
G

Guest

hi
try to import this aspx page to your aspx page
import yourproject.yourpage
and then try to get your procedure
 
C

curllion

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_SelectionChanged);
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.Controls[0].FindControl("Dat
epicker1").FindControl("TextBox1"))).Text);
.......
}
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top