Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problem getting value from textbox from one page to another with findcontrol

Posted on 2012-04-03
13
371 Views
Last Modified: 2012-04-19
Hi

I have a MasterPage with a Textbox and an imageButton, one this MasterPage im using a web.Sitemap and global.asax for navigation, its works fine.

My problen is that when i type ind a value in the textbox on the masterpage and click the imageButton its then PostBackUrl="~/getmail.aspx" that the page cant find /SLETTEST/FrontPage, so i cant get the value from the textbox to the getmail.aspx site with findcontrol when im using routing/global.asax

If i use this code the findcontrol is working fine.
MasterPage.Master
<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        
        </asp:ContentPlaceHolder>
    </div>
<asp:TextBox ID="GetMailInfo" runat="server" Text="Tilmeld dig til vores Nyhedsbrev"></asp:TextBox>
<asp:ImageButton runat="server" ID="btnSubscribe" ImageUrl="~/add/image/btntilmeld.png" PostBackUrl="~/getmail.aspx"></asp:ImageButton>
    </form>
</body>
</html>

Open in new window


Default.aspx
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
Content of Default.aspx
</asp:Content>

Open in new window


getmail.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="getmail.aspx.vb" Inherits="getmail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

Open in new window


getmail.aspx.vb
Partial Class getmail
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim mpTextBox As TextBox = CType(Me.PreviousPage.Master.FindControl("GetMailInfo"), TextBox)
        Label1.Text = mpTextBox.text

    End Sub
End Class

Open in new window


Then i can get the value with no problems, but if i add this code so the routings are working, i cant get it to work, bc. it cant find SLETTEST/FrontPage (SLETTEST is the name of the foldet that this website test is running from.)

Default.aspx.vb
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Request.Url.AbsolutePath = "/TESTSLET/Default.aspx" Then
            Response.Redirect(Request.Url.AbsoluteUri.Replace("/Default.aspx", "/FrontPage"))
        End If

    End Sub
End Class

Open in new window


Global.asax
<%@ Application Language="VB" %>
<%@ Import Namespace="System.Web.Routing" %> 

<script runat="server">

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
        
        '///// Denne linje er den der kalder Sub'en RegisterRoutes ved opstart, er meget vigtig. /////
        RegisterRoutes(RouteTable.Routes)
    End Sub
    
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application shutdown
    End Sub
        
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a new session is started
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a session ends. 
        ' Note: The Session_End event is raised only when the sessionstate mode
        ' is set to InProc in the Web.config file. If session mode is set to StateServer 
        ' or SQLServer, the event is not raised.
    End Sub
    
    '///// Dette Sub er vigtig i Routes, sammen med import namespace System.Web.Routing. /////
    Sub RegisterRoutes(ByVal routes As RouteCollection)
        'for de forskellige link adresses/ruter skal der laven en 
        routes.MapPageRoute("", _
                            "FrontPage", _
                            "~/Default.aspx")
        ' adresse nr. 2
        routes.MapPageRoute("", _
                            "GettingTheMail", _
                            "~/getmail.aspx")
   
    End Sub
       
</script>

Open in new window


I hope someone can help me.
0
Comment
Question by:tjgrindsted
  • 7
  • 6
13 Comments
 

Author Comment

by:tjgrindsted
ID: 37809788
No one that can help !?
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37810675
It's a lot to take in ;-)

I can't try it out myself right now, but can you put a breakpoint on line 6 in Default.aspx.vb (If Request.Url.AbsolutePath = "/TESTSLET/Default.aspx" Then) and check what the actual value is because:
1) it's inconsistent with the text above that ("SLETTEST" <> "TESTSLET")
2) possibly you need to check for part of the path and not the whole path, something like:

If Request.Url.AbsolutePath.Contains("/TESTSLET/Default.aspx") Then

Open in new window


also, check if AbsolutePath doesn't contain backslashes instead of slashes.
0
 

Author Comment

by:tjgrindsted
ID: 37811885
Hi Rebert_Schutt

If i put this code under the if request.Url... in the Page_Load on the default.aspx.vb
        Response.Write(Request.Url.AbsolutePath)
When i get this
/TESTSLET/FrontPage

and sry i have write TESTSLET and SLETTEST, its TESTSLET.

If i delete my If statment and only have the response.write
Then i get this. (But then my routing is not working.)
/TESTSLET/Default.aspx

so i need it to work with the /TESTSLET/FrontPage  but its almost as the PreviousPage.Master.FindControl cant see this, but only se /TESTSLET/Default.aspx how can i get it to find the control in the routing /TESTSLET/FrontPage !?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37812245
Ok, but isn't it logical that once you do a redirect, that Me.PreviousPage is not correct anymore? I must admit I haven't used that before. Can't you use the posted form data and store that? I'm going to try and re-create your project here but it might take me some time.
0
 

Author Comment

by:tjgrindsted
ID: 37812602
NP if u can get the fincontrol to work with the routing that will be great and maybe help alot of other that need to do this, when using routing.

If not then i must use Sessions or QueryString or HttpPost.

If i add another page and use the masterpage and add it to the routing, then call the page with maybe /TESTSLET/Page2 then i get the same error about that it cant find the page, and this new page is not using redirect, i use this on the front page, bc. when going on the page it will show /TESTSLET/Default.aspx it will not using the routing on the frontpage when calling /TESTSLET/ it will use the Default.aspx page or Index.aspx if i use that name. so first time when visit the domain i use the redirect on the Default.aspx so the routing is in use, and after that i can use links as /FrontPage or /GettingTheMail if i want to visit TESTSLET/getmail.aspx here it will ofc. show /TESTSLET/GettingTheMail and not /TESTSLET/getmail.aspx
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37812730
I've got it partly up and running, the PreviousPage does seem unavailable but have a look at the picture with a 'watch' on Request.Form.AllKeys:

debug form
And Request.Form(5) does contain the email adres filled in, but there's gotta be a nicer way to get to it. At the moment I don't know unfortunately, as the master page construction is something I haven't done a lot with...
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37813327
Possibly the reason you see a lot of people asking for it and no answers could of course be that it cannot be done.

There's a lot of useful information on this (old!) page: http://odetocode.com/Articles/450.aspx 

I think you didn't want to use Sessions but this seems to be until now the easiest solution I found:

- in Page_Load in Default.aspx.vb add:
        Session("Save_GetMailInfo") = Me.GetMailInfo.UniqueID

Open in new window


- in Page_Load in getmail.aspx.vb replace your existing 2 lines with this line:
        Label1.Text = Request.Form(Session("Save_GetMailInfo"))

Open in new window


But I have a gut feeling that it can be done much easier, it's just that Redirect that's in the way... :-(
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 37813725
Ah, I found an easy way to avoid having to store the UniqueID, so you don't need sessions.

If you give the Master page an ID, then the generated element name for the Textbox is 'predictable': MasterID$MainContent$GetMailInfo

Just put this in your MasterPage.master.vb:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.ID = "MasterID"
    End Sub

Open in new window


And then get rid of the previous code which stored the UniqueID in a Session variable.

In getmail.aspx.vb you can now use this id directly:
        Label1.Text = Request.Form("MasterID$MainContent$GetMailInfo")

Open in new window

0
 

Author Comment

by:tjgrindsted
ID: 37814142
I get a blank page.

Do i do anything else on that masterpage adding a button or !?
If i add the code to my master and have the global.asax added too then i just get a blank page I dont get any value from the GetMainInfo.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37815105
Can you use "View Source" on your browser to verify that it looks like this: (before you press the button)

<input name="MasterID$MainContent$GetMailInfo" type="text" value="Tilmeld dig til vores Nyhedsbrev" id="GetMailInfo" />

Open in new window


Also, use some breakpoints to make sure that the (new) code is being executed correctly.

Did you try the Session solution first or just the last one?
0
 

Author Comment

by:tjgrindsted
ID: 37815311
i get this

<input name="MasterID$GetMailInfo" type="text" value="Tilmeld dig til vores Nyhedsbrev" id="GetMailInfo" />
<input type="image" name="MasterID$btnSubscribe" id="btnSubscribe" src="add/image/btntilmeld.png" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;MasterID$btnSubscribe&quot;, &quot;&quot;, false, &quot;&quot;, &quot;getmail.aspx&quot;, false, false))" />

so i miss MainContent, how do i add that !?
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37815320
Looks like you're missing the ID in your Master file:

<asp:ContentPlaceHolder ID="MainContent" runat="server"/>

Open in new window


Or maybe you have changed the ClientIDMode on your Textbox?

Then, alternatively you can use the ID from the Textbox as you see it in your source.
0
 

Author Closing Comment

by:tjgrindsted
ID: 37865371
thx for the help
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question