Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Master/Detail GridView visible, FormView not visible on page load

Status
Not open for further replies.

capella2007

Programmer
Feb 8, 2007
21
US
Hi, 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 what I'm trying to do: 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:

[BEGIN VB CODE]
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
[END VB CODE]

This code doesn't quite work. 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. 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
 
Code:
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
I don't see where you are adding the objects to the page. Or are you adding them in the designer? If so, you have to reference them by name in your code. If they are the same names, then you are overwriting them in the code above.

Jim
 
Hi, jbenson. Thanks for answering.

You said: "I don't see where you are adding the objects to the page. Or are you adding them in the designer?"

I'm not quite sure what you mean by that question. I created everything (GridView, FormView, the datasources, etc) and got it fully functioning on the aspx side. Now I want to be able to "hide" the FormView and GridViews as described above.

It is my assumption that, since the grid- and formviews exist on the aspx, all I need to do on the code-behind is to "dim" them in the sub and set the desired visible state.

I hope that clarifies things.

Thanks again
 
It is my assumption that, since the grid- and formviews exist on the aspx, all I need to do on the code-behind is to "dim" them in the sub and set the desired visible state.
No, that;s incorrect. By declaring a New Gridview like you have done, you've created a second GridView object that you've then se to Visible="False". As this second object is never added to the page, it isn't seen anyway.

To reference the first GridView (i.e. the one you created in the aspx page), simply refer to it in code by the ID that you assigned it.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
You said, "...simply refer to it in code by the ID that you assigned it."

Okay, I'm not quite sure how to code that, but what I did was try these subs:

(The ids for the gridview and formview are simply gridview1 and formview1, respectively.)

[BEGIN VB CODE]
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 withe 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
[END VB CODE]

Obviously, neither of these work, as I'm sure you probably guessed!

Suggestions?

Thanks again.
 
I'll just paste the relevant parts of the grid and form views:

[BEGIN HTML CODE]
<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>
[END HTML CODE]

Thanks
 
I tried using your HTML you posted, and I am able to refernce the 2 objects in code by using

FormView1. and
GridView1.

There must be more to this than you are posting or explaining. There should be no problems referencing the objects.

Try creating a new page, and past the HTML you provided here, you will see it works. You may just have to create a new page and start from scratch.
 
jbenson, What, precisely, do you mean by "referencing the objects"?

I'm not saying you're wrong, I just don't understand how providing the HTML for the grid and formviews can help you figure out why I can't hide the formview on page load and then hide the gridview on a button click event using vb code in the code behind page.

Could you clarify, please?

Thanks
 
I pasted the HTML code you provied into a new page. IN the code-behind I was able to refernce the objects(get to their properties, events. etc.)

So I was able to do
Gridview1.Visible = TRUE
FormView1.Visible = FALSE (as an example)
 
Weird. So, to be clear, all you did in the code-behind is in the page_load event add the two lines:

gridview1.visible = True
formview1.visible = False

And that 'hid' the formview on page load?

Is that correct?
 
I didn't run the page becase I did not code anthing to retrieve data, but yes, that is all I did.
 
Is your gridview and formview nested inside other controls? If you are nesting them, you will have to do a control.findcontrol("gridview1") to assign them so that you can access their properties..

here is a quick and dirty example of what I mean in C#:

GridView gridview1 = (GridView)somecontrol.findcontrol("gridview1");
gridview1.Visible = true; //or false;

I've had this problem when I first started using .NET and didn't notice that nesting controls within controls caused these types of problems.

Let me know if this solves the issue.

Cheers,

G.



Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
Thanks, Gorkem. I just read your reply, so I haven't checked out your suggestion yet. But that rings a bell. The only thing the formview and gridview are nested in are their own <div> tags and "above" that, the <form> and then <body> tag.

One detail: A converter rendered your C# to the VB.NET code:

Dim gridview1 As GridView = CType(somecontrol.findcontrol("gridview1"), GridView)
gridview1.Visible = True 'or false;

I can't figure out what the "somecontrol" should be. If it's what the gridview or formview is nested in, then I'm not sure this is the solution, as, like I mentioned above, they're only nested in div,form,body tags.

I play with it and let you know what happens.

Regards.
 
If in doubt, you can turn on your debug/trace and see whats nested with what.. but if, like you say, the gridview and formview controls are not nested within other controls, you can get away with the following if all else fails:

Dim GV1 as GridView = CType(Page.findcontrol("gridview1"),GridView)

The Page "control" is the main page itself and using the findcontrol method, you should be able to reference any top level controls (non nested).

Hope this helps,

G.

Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
Gorkem,

No dice. Grrr! I just don't get it. I was almost positive it had something to do with "findcontrol", but it still didn't work. I tried a couple of variations of your suggestions in the page_load event:

Dim GV1 As GridView = Form.FindControl("gridview1")
Dim FV1 As FormView = Form.FindControl("formview1")

GV1.Visible = True
FV1.Visible = False

As you can see I tried removing the ctype to "see" what would happen. I also tried using Page.Form.Find... - all to no avail! Both controls are still being displayed on page_load regardless! So frustrating...

Like I said earlier, I KNOW it can be done.

Ooh, I lied. Might not make a difference, but you never know... the Gridview is in a table tag for page organization reasons.
 
What I find strange is the inteli-sense should automatically pick up on the gridview1 and formview1 controls and allow you to access their properties.. the fact that it isn't auto-sensing leads me to think there is something else at work inside your ASPX page..

Have you tried creating a new test page with JUST a gridview and formview control on it, then try to see if you can make them visible or not? I find, in situations where the page doesn't work, creating a test page with just the controls to troublshoot helps a lot. If it works on the test page, then you should review the original aspx page to see the differences in how they are inserted.

Can you post your full aspx code, perhaps we can figure this out quicker if I can see exactly how the page is laid out..

Cheers,

G.

Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
Oh - my - God. How simple can it be!?

I've only been coding professionally for less than a year (but I'm sure any veteran coder will agree), but almost EVERY SINGLE TIME I run into a show-stopper like this, the solution is ridiculously simple!

I went to the top of the aspx page to select all of the text to copy it here for you and saw the Page directive line. All I had in there was "<%@ Page Language="VB" ValidateRequest="false" %>" - no "CodeFile" or "Inherits". The aspx page had no idea there was a code behind page.

Problem solved.

I want to thank all of you guys for your attempts at helping me!

Best regards!

Mark
 
Excellent news!! Sometimes the easiest and simplest errors are the hardest to find!

I'm happy to know it all worked out!

Cheers,

G.



Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top