John_Mc wrote: » I'm confused as to why you're putting the Gridview in the LoginView? Would it not make more sense to redirect the user to a secure page after successfully signing in, and to have the Gridview located there? This is how it usually works. The login control is particularly clunky and will only give you headaches.
kingambrose wrote: » I'd agree with John_Mc, however I'm sure you could use FindControl? I'm a bit rusty but in vb it'd be something like... Dim gvw As GridView gvw = CType(Me.LoginView1.FindControl("GridView1"), GridView)
Collumbo wrote: » by any chance, is GridView1 sitting inside a MasterPage? if so, when you are running it and the page loads, view the source html. is the object called gridview1? (I think it gets rendered as a table, and the Id will be GridView1). With a masterpage, has the name in your server side code will be changed to something like "ctl00$ContentPlaceHolder1$GridView1". I can't remember the exact naming convention used by asp.net but I'm pretty close. If you debug through your code, look at all of the child objects of the page. It will appear under some other control I'm sure... The other option as suggested above is to do this: GridView yourGridVariableName = this.Master.FindControl("GridView1") as GridView; aside from why you're putting it inside the LoginView (which is something i would weird to maintain in future, let alone explain to someone else who might have to look after the website), i actually don't like using GridView. You have way more control over defining a Repeater Control and I've always found them nicer to work with. Or, if we are talking about a very small piece of data, you can always write your own html output with the correct link.
<%@ Page language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e) { // If multiple buttons are used in a GridView control, use the // CommandName property to determine which button was clicked. if(e.CommandName=="Add") { // Convert the row index stored in the CommandArgument // property to an Integer. int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button clicked // by the user from the Rows collection. GridViewRow row = ContactsGridView.Rows[index]; // Create a new ListItem object for the contact in the row. ListItem item = new ListItem(); item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " + Server.HtmlDecode(row.Cells[3].Text); // If the contact is not already in the ListBox, add the ListItem // object to the Items collection of the ListBox control. if (!ContactsListBox.Items.Contains(item)) { ContactsListBox.Items.Add(item); } } } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>GridView RowCommand Example</title> </head> <body> <form id="form1" runat="server"> <h3>GridView RowCommand Example</h3> <table width="100%"> <tr> <td style="width:50%"> <asp:gridview id="ContactsGridView" datasourceid="ContactsSource" allowpaging="true" autogeneratecolumns="false" onrowcommand="ContactsGridView_RowCommand" runat="server"> <columns> <asp:buttonfield buttontype="Link" commandname="Add" text="Add"/> <asp:boundfield datafield="ContactID" headertext="Contact ID"/> <asp:boundfield datafield="FirstName" headertext="First Name"/> <asp:boundfield datafield="LastName" headertext="Last Name"/> </columns> </asp:gridview> </td> <td style="vertical-align:top; width:50%"> Contacts: <br/> <asp:listbox id="ContactsListBox" runat="server" Height="200px" Width="200px"/> </td> </tr> </table> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the AdventureWorks sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="ContactsSource" selectcommand="Select [ContactID], [FirstName], [LastName] From Person.Contact" connectionstring="<%$ ConnectionStrings:AdventureWorks_DataConnectionString%>" runat="server"/> </form> </body> </html>
MrDarcy wrote: » Just an update, I've taken the LoginView out of my page and I'm dealing with user states programatically and all is fine now, thanks to everyone above for advice with this... What I'm doing though is opening up a child window on:public void GridView1_SelectedIndexChanged(object sender, EventArgs e) { string strScript = "<script>"; strScript += "var newWindow = window.open('newpage.aspx', '_blank','height=450, center:yes, width=450, status=no, resizable= yes, menubar=no, toolbar=no, location=yes, scrollbars=no, status=no');"; strScript += "</script>"; ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", strScript); string PassID = GridView1.SelectedValue.ToString(); Label1.Text = GridView1.SelectedValue.ToSTring(); } This code opens up a new child window when a new row in my GridView is selected and sets the value of string PassID to the ID of the respective row in my mssql DB. I've assigned the value of GridView1.SelectedValue.ToSTring();, to my label1 control to check that the right ID is being returned and this is the case... All I need to ask now is how do I pass this value from the parent page with the GridView, to the Child page with more detailed info for the same mssql ID value that has been passed to it via my passID string on the parent page???
void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e) { // If multiple buttons are used in a GridView control, use the // CommandName property to determine which button was clicked. if(e.CommandName=="Add") { // Convert the row index stored in the CommandArgument // property to an Integer. int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button clicked // by the user from the Rows collection. GridViewRow row = ContactsGridView.Rows[index]; // Create a new ListItem object for the contact in the row. ListItem item = new ListItem(); item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " + Server.HtmlDecode(row.Cells[3].Text); // If the contact is not already in the ListBox, add the ListItem // object to the Items collection of the ListBox control. if (!ContactsListBox.Items.Contains(item)) { ContactsListBox.Items.Add(item); } } }
John_Mc wrote: » string pageUrl = "Newpage.aspx?PageId=" + PassId; Or you could use the string.format function: string pageUrl=string.format("Newpage.aspx?PageId={0}",PassId); Personally, I think you should avoid using a popup window as these are blocked by most modern browsers. Why not have a user control situated underneath the Grid which is invisible unless something is selected?
MrDarcy wrote: » Thanks for that John. Don't I have to put a QueryString or or something like that into the child page to basically "grab" the value being passed and put it into a local variable or something like that???