I am looking for an example that shows how to select and highlight
multiple rows in a DataGrid.
My DataGrid is part of a Web User Control which is contained in an ASPX
page.
I haven't been able to find any examples which demonstrate how to do
this.
My Code:
The ASPX Page
<%@ Page language="c#" Codebehind="Web Form1.aspx.cs"
AutoEventWireup ="false" Inherits="TestM ultiSelect.WebF orm1" %>
<%@ Register TagPrefix="uc1" TagName="WebUse rControl1"
Src="WebUserCon trol1.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1 </title>
<meta name="GENERATOR " Content="Micros oft Visual
Studio .NET 7.1">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript"
content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server">
<uc1:WebUserCon trol1 id="WebUserCont rol11"
runat="server"> </uc1:WebUserCont rol1>
</form>
</body>
</HTML>
Code Behind for ASPX page
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
namespace TestMultiSelect
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
private void Page_Load(objec t sender,
System.EventArg s e)
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web
// Form
// Designer.
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}
}
Web User Control
<%@ Control Language="c#" AutoEventWireup ="false"
Codebehind="Web UserControl1.as cx.cs"
Inherits="TestM ultiSelect.WebU serControl1"
TargetSchema="h ttp://schemas.microso ft.com/intellisense/ie5"%>
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300"
border="1">
<TR>
<TD colSpan="3">
<div style="OVERFLOW : auto; WIDTH: 100%; HEIGHT:
200px"><asp:dat agrid id="DataGrid1" style="Z-
INDEX: 102;
LEFT: 24px; POSITION: absolute; TOP: 32px"
Height="120px"
runat="server"> </asp:datagrid></div>
</TD>
</TR>
<TR>
<TD></TD>
<TD align="center">
<asp:Button ID="CloseButton " runat="server"
Width="99px"
Text="Close"></asp:Button>
</TD>
<TD></TD>
</TR>
</TABLE>
Web User Control - Code Behind
namespace TestMultiSelect
{
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Data.Sql Client;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
/// <summary>
/// Summary description for WebUserControl1 .
/// </summary>
public class WebUserControl1 : System.Web.UI.U serControl
{
protected System.Web.UI.W ebControls.Data Grid
DataGrid1;
protected System.Web.UI.W ebControls.Butt on
CloseButton;
SqlConnection myConnection;
private void Page_Load(objec t sender,
System.EventArg s e)
{
myConnection = new SqlConnection
("Server=localh ost;uid=sa;pwd= preview;databas e=pubs");
if(!IsPostBack)
BindGrid();
}
public void BindGrid()
{
SqlDataAdapter dAdapter = new SqlDataAdapter
("SELECT au_id,
au_fname, au_lname FROM authors ",
myConnection);
DataSet dSet= new DataSet();
dAdapter.Fill(d Set, "authors");
DataGrid1.DataS ource = dSet.Tables
["authors"].DefaultView;
DataGrid1.DataB ind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET
// Web Form
// Designer.
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not
/// modify
/// the contents of this method with the code
/// editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler
(this.Page_Load );
}
#endregion
}
}
I want to select the au_fname and au_lname fields (which I am going to
concatenate). The Web User Control has a Button which, when clicked,
should pass the selections to the calling page (a simple HTML page).
Clicking the button should also close the .ASPX page, and refresh the
calling page.
Is what I am trying to do reasonable, or is there a better approach? I
am a bit out of my depth here, and would really appreciate any kind of
help, especially code snippets.
Thanks!
*** Sent via Developersdex http://www.developersdex.com ***