Click to See Complete Forum and Search --> : Hiding formview on page_load and showing on linkbutton click event


capella2007
02-08-2007, 03:38 PM
First, so you know what environment I'm working in, I have VS 2005 Pro, and I'm using VB code-behind pages.

The subject essentially describes my problem: I have a GridView and a FormView on a page and I want only the GridView to display on page load and the FormView to only show up when the user clicks a linkbutton, at which time the GridView "disappears".

I've tried messing with the visible properties of both the Grid- and FormViews, while at the same time changing the visible properties on the page load and button click events in the code-behind page:

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

Dim formview1 As New FormView
Dim gridview1 As New GridView

gridview1.Visible = True
formview1.Visible = False
End Sub

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim gridview1 As New GridView
Dim linkbutton1 As New LinkButton
Dim formview1 As New FormView

GridView1.Visible = False
linkbutton1.Visible = False
formview1.Visible = True
End Sub


Here's the main, relevant parts of the Grid- and FormViews:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="2px" CellPadding="3"> </asp:GridView>

<asp:FormView ID="FormView1" runat="server" AllowPaging="True" DefaultMode="Insert" DataSourceID="SqlDataSource1" DataKeyNames="id" Width="800px" BackColor="#DAE6E6">
</asp:FormView>


Here's some "testing" subs I've done:

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

'Here I removed the "dim" statements altogether
gridview1.Visible = True
formview1.Visible = False

'Results in blue "squiggly" lines under the gridview1 and formview1 with the message: "Name gridview1 is not declared."

End Sub
'******************************
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'Here I just removed the "new" keyword
Dim formview1 As FormView
Dim gridview1 As GridView

gridview1.Visible = True
formview1.Visible = False

'Results in green "squiggly" message under gridview1 & formview1 in second set of lines: "Variable gridview1 has been used before it has been assigned a value. A null reference exception could result at run time."

End Sub


Obviously, I've been working on this for a while now. The only way I've been able to "hide" and "unhide" the Grid and Formviews is with the Visible="true/false" on the aspx page.

The closest I've gotten is loading the page with the FormView not visible and the Gridview visible, but clicking on the linkbutton doesn't "hide" the GridView and "display" the FormView.

I suspect I'm missing something in the VB code, but can't quite figure it out. Someone in another forum suggested the "New" keyword in my vb code was "overriding" the visible= state I set on the aspx side. I know it can be done - I've done it myself before. I just can't find the code I used! Arrrgh!!

Any help would be greatly appreciated!

Thanks

capella2007
02-09-2007, 10:30 AM
Wow - 15 views and no replies?!?

C'mon, someone's got to have a suggestion! :)

I need help - this is for a project at work! :eek:

Ribeyed
02-09-2007, 05:24 PM
Hi,
try putting them in div tags and show/hide the div from your code behind page.

Ribeyed
02-09-2007, 05:30 PM
Hi,
you can also try using findcontol to make sure you are finding the control.

Dim GridView 1 As Bew GridView
GridView1 = FindControl("GridView1")

GridView1.Visible = True

In VS add a breakpoint, run your code, walk through line by line and add a watch to GridView1 just to check it is finding the control.

lmf232s
02-09-2007, 06:18 PM
If clicking on the links is performaing a post back then what i would try it placing each control in a Panel. Set the visible property of the Formview = False and set the gridview = True (gridview dosent really matter because it will display by default).

There is no need to place code in the page_Load sub and you dont need to declare your controls like Dim formview as new formview. You can reference your controls as such Me.fvMyControl.Visible = True or False.

If i had to guess this bit of code would of helped you with the way your trying to do it. Your problem was that on each post back it was running the page load code again. If Not Page.IsPostBack will check to see if its a post back and will not execute the code again.

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

If Not Page.IsPostBack Then
'Drops in on page load only
Me.gridview1.Visible = True
Me.formview1.Visible = False
End If
End Sub

capella2007
02-09-2007, 06:20 PM
Thanks for the suggestions, guys. I did get it resolved, though. Someone in another forum (tek-net, I think) suggested I just copy only the gridview and formview to a new page and try to get those to do what I want and then add from there. I did get it working the way I wanted finally using that method. Problem is, I really don't know what I was missing/doing wrong in the first place!

Ah well, the boss is happy that I got it working.

Regards.