Opening a new window from aspx page.

G

Guest

I have a datagrid with several rows of data. One of the
columns is a button that opens an edit window. When I
click the button, I need to open a new edit window for the
record. That means the new window need to know the record
of the row that was clicked in the datagrid. Being new to
asp.net, how do you get a datagrid to do this?

Thanks.

Leon Chuck Gosslin
 
C

Chris Gastin

Leon:

Here is some sample code to get you started. For simplicity of the
example I used a IList as the DataSource to bind to the datagrid. I
assume that you data source will come from the database. As you can
see I used javascript to pop open the window, and I put the primary
key in the querystring of the popup window. IF you have this value in
the querystring then you should be able to re query the database for
this record to display in your edit window. You probalby will want to
make the column displaying the primary key (PK) for the record hidden
by setting the visible to false. This is not a complete solution, but
it should get you started.

Hope this helps you.

Chris Gastin



///////////////////////
// ASPX PAGE //
///////////////////////
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="NewsGroup.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 32px;
POSITION: absolute; TOP: 24px" runat="server"
Width="704px">
<Columns>
<asp:HyperLinkColumn Text="Edit" DataNavigateUrlField="PK"
DataNavigateUrlFormatString="javascript:window.open('popup.aspx?id={0}')"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>


///////////////////////
// Codebehind //
///////////////////////
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace NewsGroup
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
IList list = new ArrayList();
list.Add(new MyObject(111,"item1-field1","item1-field2","item1-field3"));
list.Add(new MyObject(222,"item2-field1","item2-field2","item2-field3"));
list.Add(new MyObject(333,"item3-field1","item3-field2","item3-field3"));
list.Add(new MyObject(444,"item4-field1","item4-field2","item4-field3"));
DataGrid1.DataSource = list;
DataGrid1.DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion


}
}

///////////////////////
// MyObject //
///////////////////////
using System;

namespace NewsGroup
{
/// <summary>
/// Summary description for MyObject.
/// </summary>
public class MyObject
{
private int pk;
private string field1;
private string field2;
private string field3;

public MyObject(int pk,string field1,string field2,string field3)
{
this.pk = pk;
this.field1 = field1;
this.field2 = field2;
this.field3 = field3;

}

public int PK
{
get{return pk;}
set{pk = value;}
}

public string Field1
{
get{return field1;}
set{field1 = value;}
}

public string Field2
{
get{return field2;}
set{field2 = value;}
}

public string Field3
{
get{return field3;}
set{field3 = value;}
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,361
Latest member
eitamoro

Latest Threads

Top