Link to home
Create AccountLog in
Avatar of jazjef
jazjef

asked on

How do I pass a session variable value to a textbox on a formview that's defaulted to 'insert' mode?

I am using Visual Web Developer with VB.net coding.....

I have a form called page1 with TextBox1 that accepts a 9 digit number. This number as a session variable is called RegNum. Upon a button click page2 loads and I have a formview control with 6 fields----one of them is the field for the 9 digit number that is called 'RegNumTextBox'. This page2 formview1 is default set to the 'insert' view.

How can I use the page2 load event to populate the formview1 textbox associated with the 9 digit number, with the session variable named RegNum that's coming from page1? I thought this would be an easy hook-up but I'm beating my head on this one.... any help is appreciated.

here's what I've been trying...it doesn't cause an error but it does absolutely nothing...

FormView1.FindControl("RegNumTextBox").Equals(Session("RegNum"))



ASKER CERTIFIED SOLUTION
Avatar of Edwin_C
Edwin_C
Flag of Hong Kong image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of jazjef
jazjef

ASKER

Edwin_C

Thanks Edwin_C.... this does work.....but.... There's one small issue; after the page load, in order for the session value to show up in the textbox, I have to physically click a button or something that causes a postback on the page. Can I cause an auto-postback or something to occur after the page load?

I tried to call a button click of an invisible button on page load after the code, but that doesn't work....

Call Button1_Click(Nothing, Nothing)

Why can't you run

dim tb as Textbox = CType(FormView1.FindControl("RegNumTextBox"), Textbox)
tb.Text = Session("RegNum")

directly in Page_Load()?
Avatar of jazjef

ASKER

It is located directly in Page_Load()  ....  but it takes a physical postback of some type to get the session value to show up in the formview1 textbox after the page load event has completed....

When did you save value into the session variable?  If it is in page1 (after button click), then it should be available when page2 loads.
Avatar of jazjef

ASKER

Session variable is created on page1.... and it will appear on page2----but only after a physical postback of some type occurs after page2 has loaded. Prior to this manual page2 postback being initiated all fields are blank.
Can you show the code in button click event in page1? I suppose this is where you save the session variable and load page2.  If not, then show me where you did these tasks.
Avatar of jazjef

ASKER

Public RegNum As String


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click

        Session("RegNum") = TextBox1.Text

    End Sub
How did you go to page2 then?  Do you mean you have both page1 and page2 being loaded at the same time?
Avatar of jazjef

ASKER

Button1 creates the session variable. Button2 loads page2.

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

        Response.Redirect("Default2.aspx")


    End Sub
If Button1 is clicked first and then followed by Button2, then Default2.aspx should have the session variable available.  

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
                                 ^                                                                                                                          ^
Just check if this is a mistake or not?
Avatar of jazjef

ASKER

yes....that's a clerical error on my part.... my actual code is correct.......here's what it actually says:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Session("RegNum") = TextBox1.Text

    End Sub
Avatar of jazjef

ASKER

I think the issue has something to do with the Formview being a 'container' on Default2.aspx..... the page load event does work for the page, but the formview doesn't respond to the page load because its a secondary container. this may be why the postback makes it appear: perhaps the page load event makes the session variable available to page2, and the subsequent postback makes it available to the formview container.
>> the Formview being a 'container' on Default2.aspx.
>> formview doesn't respond to the page load because its a secondary container
I don't understand.  The session variable should be available when default2.aspx loads.  You can verify this in Debug mode and show the value in a Label control while Page_Load(). One possibilty is that your FormView is still empty when Page_Load runs.

Mind posting the Page_Load code of Default2.aspx?
Avatar of jazjef

ASKER

TextBox1 shows the session variable right away---I have this as a check to make sure it's being passed. If I click a button or something on the page to create a manual postback, then the session variable appears in the relevant formview1 textbox. Here's the page load:

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

        TextBox1.Text = Session("RegNum")

        Dim tb As TextBox = CType(FormView1.FindControl("RegNumTextBox"), TextBox)
        tb.Text = Session("RegNum")

    End Sub

Avatar of jazjef

ASKER

I got it to work Edwin_C.

Your solution is correct... the problem was that I had the default setting for Formview set to 'insert'----which preps it for a totally new record to be inserted. The key was to switch the default of the formview to 'edit'. After doing this the formview is in 'update' mode, the session variable populates its proper box, and then all the other record data populate their respective boxes on the formview as well based on the RegNum from the session variable.....

thanks for sticking with me on this!......

You said the formview is in insert mode.  Did you set the DefaultMode of the formview in aspx? E.g.
<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert">
            <InsertItemTemplate>
                <asp:TextBox ID="tb1" runat="server"></asp:TextBox>
            </InsertItemTemplate>
        </asp:FormView>

Missed yr last post.  Glad to know you have solved the problem
Are you setting a DataSource for that FormView at any point??, I would suggest moving that code to the Page Prerender Event Handler..