Hi,
It is possible to do it in DataGrid. I have implemented it, in my sample
code. I have pasted my sample code below:
Table Script
~~~~~~~~~
CREATE TABLE ModelExam (QuestionID IDENTITY INT,Question VARCHAR(50),Answer1
VARCHAR(50),Answer2 VARCHAR(50),Answer3 VARCHAR(50))
Set QuestionID -> Primary Key
Web.config configuration
~~~~~~~~~~~~~~~~
<appSettings>
<add key="ConnectionString" value="Data Source=VIJAY\NETSDK;Integrated
Security=SSPI;Initial Catalog=Northwind;"></add>
</appSettings>
Add the above mentioned key in web.config inside the <configuration> tag
ModelExam.aspx file
~~~~~~~~~~~~~
<%@ Page language="c#" Codebehind="ModelExam.aspx.cs"
AutoEventWireup="false" Inherits="testCompositeControl.ModelExam" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="
http://schemas.microsoft.com/intellisense/ie5">
<script>
function ToggleAnswers(objDiv,objButton)
{
if (objDiv && objButton)
{
if (objDiv.style.display == 'none')
{
objDiv.style.display = '';
objButton.value = "-";
}
else if (objDiv.style.display == '')
{
objDiv.style.display = 'none';
objButton.value = "+";
}
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="frmModelExam" method="post" runat="server">
<asp
ataGrid id="dgExamInfo" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn ItemStyle-VerticalAlign=Top>
<ItemTemplate>
<input type=button value="+" id="btnQuestionID" runat=server
onclick=<%# "ToggleAnswers(div" +
DataBinder.Eval(Container.DataItem,"QuestionID") + ",this);"%> >
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label ID="lblQuestion" Runat="server">
<%# DataBinder.Eval(Container.DataItem,"Question") %>
</asp:Label>
<asp
ataList ID="dlstModelExam" Runat=server DataSource=<%#
GetAnswers(DataBinder.Eval(Container.DataItem,"QuestionID"))%> >
<ItemTemplate>
<div style="display:none" id=<%# "div" +
DataBinder.Eval(Container.DataItem,"QuestionID")%>>
<table>
<tr><td><%# DataBinder.Eval(Container.DataItem,"Answer1")
%></td></tr>
<tr><td><%# DataBinder.Eval(Container.DataItem,"Answer2")
%></td></tr>
<tr><td><%# DataBinder.Eval(Container.DataItem,"Answer3")
%></td></tr>
</table>
</div>
</ItemTemplate>
</asp
ataList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp
ataGrid>
</form>
</body>
</HTML>
ModelExam.aspx.cs file
~~~~~~~~~~~~~~~
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;
using System.Configuration;
using System.Data.SqlClient;
namespace testCompositeControl
{
/// <summary>
/// Summary description for ModelExam.
/// </summary>
public class ModelExam : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgExamInfo;
protected static string ConnectionString;
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sqlDataAdp = new SqlDataAdapter(@"SELECT
QuestionID,Question,ISNULL(Answer1,'') AS Answer1,ISNULL(Answer2,'') AS
Answer2,ISNULL(Answer3,'') AS Answer3
FROM ModelExam",new SqlConnection(ConnectionString));
DataSet dsExamInfo = new DataSet();
sqlDataAdp.Fill(dsExamInfo,"ExamInfo");
Cache["ModelExam"] = dsExamInfo;
DataView dView = new DataView(dsExamInfo.Tables[0]);
dgExamInfo.DataSource = dView;
dgExamInfo.DataBind();
}
}
/// <summary>
/// This method is triggered for each QuestionID
/// </summary>
/// <param name="questionID"></param>
/// <returns></returns>
public DataSet GetAnswers(object questionID)
{
DataSet dsResult = (DataSet)Cache["ModelExam"];
DataRow[] drRowList = dsResult.Tables[0].Select("QuestionID = " +
questionID.ToString());
DataSet retResult = new DataSet();
retResult = dsResult.Clone();
foreach(DataRow drRow in drRowList)
{
retResult.Tables[0].ImportRow(drRow);
}
return retResult;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
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
}
}