Using a btn control from a master page to fire a content page procedure

ckeezer

Well-known member
Joined
Jan 16, 2006
Messages
100
Programming Experience
1-3
Long title... sorry about that.

Here is what I need to do. I have a master page with several buttons on it (btn1-3). I have a content page that has three fields on it.

I need to fire a btn_click event when btn1 is clicked on the content page... how do I do that?

Here is some of my code this far:

Master Page:
HTML:
<asp:ButtonID="btn1"runat="server"Text="page1"OnClick="btn1"PostBackUrl="~/page1.aspx"/>
<asp:ButtonID="btn2"runat="server"Text="page2"OnClick="btn2"PostBackUrl="~/page2.aspx"/>
<asp:ButtonID="btn3"runat="server"Text="page3"OnClick="btn3"PostBackUrl="~/page3.aspx"/>
Content Page:
VB.NET:
ProtectedSub btn1()
Session.Add("Name", txtName.Text)
Session.Add("Major", txtMajor.Text)
Session.Add("Email", txtEmail.Text)
EndSub
ProtectedSub btn2_Click()
Session.Add("Name", txtName.Text)
Session.Add("Major", txtMajor.Text)
Session.Add("Email", txtEmail.Text)
EndSub
ProtectedSub btn3()
Session.Add("Name", txtName.Text)
Session.Add("Major", txtMajor.Text)
Session.Add("Email", txtEmail.Text)
EndSub

I keeping getting the following error:
Error 1 'btn1' is not a member of 'ASP.students_master'. D:\Documents and Settings\ckeezer\My Documents\Visual Studio 2005\WebSites\testsite\test.master 15
 
Last edited by a moderator:
Read the "Accessing Members on the Master Page" paragraph in article Working with ASP.NET Master Pages Programmatically.

First add the MasterType directive to contentpage. Then you can find the masterpage button control and add event handler from contentpage with the AddHandler statement. Here is the code in contentpage to do this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Protected [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Page_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs)[/SIZE]
[SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][SIZE=2] b [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Button = Master.FindControl([/SIZE][SIZE=2][COLOR=#800000]"Button1"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]  If [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] b [/SIZE][SIZE=2][COLOR=#0000ff]Is [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]AddHandler[/COLOR][/SIZE][SIZE=2] b.Click, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] B1_Click[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Protected [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] B1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs)[/SIZE]
[SIZE=2]  Page.Title = [/SIZE][SIZE=2][COLOR=#800000]"masterpage event handled from content page"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Where do I put the code for the session objects that I want to add?
In the method you want to use it..? Is this question related to this thread?
 
In your post you presented the following lines of code:
Protected Sub B1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Page.Title = "masterpage event handled from content page"
End Sub

Why is there page.title? Do I need that or is that an example? Do I just need to leave my B1_Click method alone, see above code?
 
I see what you were saying after trying a couple different combo's.

Here is what I have now:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim btnGridView As Button = Master.FindControl("btnGridView")
Dim btnFormView As Button = Master.FindControl("btnFormView")
Dim btnStart As Button = Master.FindControl("btnStart")
Dim btnDetailsView As Button = Master.FindControl("btnDetailsView")
If Not btnGridView Is Nothing Then AddHandler btnGridView.Click, AddressOf btnGridView_Click
If Not btnFormView Is Nothing Then AddHandler btnFormView.Click, AddressOf btnFormView_Click
If Not btnStart Is Nothing Then AddHandler btnStart.Click, AddressOf btnStart_Click
If Not btnDetailsView Is Nothing Then AddHandler btnDetailsView.Click, AddressOf btnDetailsView_Click
End Sub

Protected Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Page.Title =
"masterpage event handled from content page"
Session.Add("Name", txtName.Text)
Session.Add(
"Major", txtMajor.Text)
Session.Add(
"Email", txtEmail.Text)
End Sub


It will only work if I click on my btnStart, if I use any of the other btn controls, it does not hold the session data.
 
here is the code to my project that I am trying to work on. the zip file only has the start page and the master page.
 

Attachments

  • help.zip
    1.9 KB · Views: 24
Yes, my Page.Title is only test code.

Your Click event handlers need to be the exact signature, that mean you must add the Sender+E parameters like any other Click delegate.
 
Ok what I am really trying to do is use the session data so what I have done is removed the buttons from the master page and put them on the contentpage. This way the controls are on the same page as the methods will be, so there is no cross referencing.

Now I still cannot get the code to execute on the other buttons... even if that button is the only one with active code. See the below:

Protected Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStart.Click
'SessionEvent()
End Sub
Protected Sub btnGridView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGridView.Click
SessionEvent()
End Sub
Private Sub SessionEvent()
Session.Add(
"Name", Me.txtName.Text)
Session.Add(
"Major", Me.txtMajor.Text)
Session.Add(
"Email", Me.txtEmail.Text)
End Sub

Nothing is saved for session data when I execute this code... however if I make the first event active, vice a note, it will work on for that button.
 
I looked at the zipped files. The reason only 'btnStart' works is because that is the only button that postback 'start.aspx'. Your other buttons postback to other pages.
 
So how do I get the button to postback to the start.aspx page, and still open the appropriate form?
Example:
If I click the grid view button, it shoudl postback to the start.aspx page, but still open the grid view form.
 
Why would you want to do that? Couldn't you just add the appropriate code to the appropriate page? Your case seems simple, all your pages are linked to the same masterpage, for each page you open it looks like you want set its own set of Session.
 
What do you me by Set its own set of Session...
If you cant tell I am very new to this.

Shouldn't I be able to just have the button postback to the proper url and save the data in a session?

It seems very easy, I am just missing the final point.

What do you suggest that I do to accomplish my mission?
 
Part of the problem may be that you haven't explained properly what you are doing, you asked how "Using a btn control from a master page to fire a content page procedure" and I answered simply in first post how you do this.

Now about what you want to achieve, this way around may not do what you wish because the (master)button event is only attached after the page is first loaded. For the initial page this is fine because the button event handler is already attached first time button is clicked, the other pages takes one postback to attach the event handler and another to trigger the newly attached event handler. So for me it appears you don't need to connect this code to the button event at all, it looks to me that you only want this code to execute every time a different page is loaded. If this is the case you add your appropriate Session code to each appropriate Page_Load event instead. If I am right about my assumptions this code of yours you put in all contentpages Page_Load events:
VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
  Session.Add("Name", txtName.Text)
  Session.Add("Major", txtMajor.Text)
  Session.Add("Email", txtEmail.Text)
End Sub
Now, which ever master(button) you click to 'RedirectUrl' to various pages this code will load for this specific contentpage.
Perhaps you want to only load the Session if the specific contentpage postback or not postback, I have no idea, you haven't said anything about this.
 
Ok let me try what you suggested. I am sorry that I am not explaining well, I am not sure how to explain which is why I am sure this is becomming frustrating for you. I do thank you for your help. I will let you know what happens.
 
Back
Top