C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Assemblies
.NET Core
.NET Standard
Active Directory
ADO.NET
Agile Development
AJAX
Alexa Skills
Algorithms in C#
Android
Angular
Architecture
ArcObject
Artificial Intelligence
ASP.NET
ASP.NET Core
Augmented Reality
Aurelia
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blockchain
Bootstrap
Bot Framework
Business
C#
C# Corner
C, C++, MFC
Career Advice
Chapters
CIO
Cloud
COBOL.NET
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Cortana Development
Cryptocurrency
Cryptography
Crystal Reports
Current Affairs
Custom Controls
Cyber Security
Data Mining
Databases & DBA
Design Patterns & Practices
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
Expression Studio
F#
Files, Directory, IO
Games Programming
GDI+
General
Generative Engine Optimization (GEO)
Google Cloud
Google Development
Graphics Design
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Internet & Web
Internet of Things
Ionic
iOS
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
kotlin
Leadership
Learn .NET
LightSwitch
LINQ
Machine Learning
Microsoft 365
Microsoft Office
Microsoft Phone
Mobile Development
Multithreading
Nano Banana
NetBeans
Networking
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Power BI
Printing in C#
Products
Progressive Web Apps
Project Management
Python
Q#
QlikView
R
React
Reports using C#
Robotics & Hardware
Ruby on Rails
Salesforce
Security
Servers
SharePoint
SignalR
Silverlight
Smart Devices
Software Testing
SQL Language
SQL Server
Startups
String in C#
Swift
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
WCF
Wearables
Web Development
Web Services
Web3
Windows 10
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML Standard
XML
XNA
XSharp
Register
Login
4
Answers
Gridview and Detail view
Murugavel Sadagopan
14y
7k
0
1
DLL/ActiveX
Reply
Hi,
I have two table
Table 1: job_details
Job_id
Project Name
Table 2: chap_details
chap_id
Job_id
chap_no
ms_pages
In the aspx page, I have grid view and details view.
My problem is
In the gridview, template fields called job_id, chapter_no
and view button
whenever i select the chapter of particular job and click the view button.
It displays the ms pages of the particular job and chapter.
Note: A job can have multiple chapters
But in the following program works when i select the first value value in the dropdown chapter list box.
Plz help me.
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="width: 605px">
<asp:GridView ID="grvTask" CssClass="gvHeader" runat="server" AutoGenerateColumns="False"
RowStyle-HorizontalAlign="Center"
GridLines="None" PagerStyle-Width="5%" HeaderStyle-BackColor="#72a1c1" HeaderStyle-ForeColor="white"
PagerStyle-Font-Bold="true" PagerStyle-HorizontalAlign="Center" PagerStyle-BackColor="linen" PageSize="5" OnSelectedIndexChanged="grvTask_SelectedIndexChanged" CellPadding="4" ForeColor="#333333" >
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="2%" HorizontalAlign="Center"/>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Job ID">
<ItemTemplate>
<asp:Label ID="lblJob_no" runat="server" Text='<%#Eval("Job_id") %>' />
<asp:HiddenField ID="hdnJobNo" runat="server" Value='<%#Eval("Job_id") %>' />
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Chapters">
<ItemTemplate>
<asp:DropDownList ID="ddChapter" runat="server" AutoPostBack="false" Width="120px" ></asp:DropDownList>
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="5%"/>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnView" runat="server" Text="View" OnClick="btnView_Click" />
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="15%" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" Width="5%" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
</table>
</div>
<div id="secondDiv" runat="server">
<table>
<tr>
<td style="width: 283px">
<asp:GridView ID="grvDetails" CssClass="gvHeader" runat="server" AutoGenerateColumns="false"
RowStyle-HorizontalAlign="Center"
GridLines="Horizontal" PagerStyle-Width="5%" HeaderStyle-BackColor="#72a1c1" HeaderStyle-ForeColor="white"
PagerStyle-Font-Bold="true" PagerStyle-HorizontalAlign="Center" PagerStyle-BackColor="linen"
AllowPaging="false" PageSize="5" Width="611px">
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="2%" HorizontalAlign="Center"/>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Job ID">
<ItemTemplate>
<asp:Label ID="lblJob_no" runat="server" Text='<%#Eval("Job_id") %>' />
<asp:HiddenField ID="hdnJobNo" runat="server" Value='<%#Eval("Job_id") %>' />
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Chapters">
<ItemTemplate>
<asp:Label ID="lblChapter" runat="server" Text='<%#Eval("chapter_no") %>' />
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="MS Pages">
<ItemTemplate>
<asp:Label ID="lblReworkCount" runat="server" Text='<%#Eval("ms_pages") %>' />
<%--<asp:Button ID="btnView" runat="server" Text="View" OnClick="btnView_Click" />--%>
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="15%" />
</asp:TemplateField>
</Columns>
<RowStyle HorizontalAlign="Center" />
<PagerStyle BackColor="Linen" Font-Bold="True" HorizontalAlign="Center" Width="5%" />
<HeaderStyle BackColor="#72A1C1" ForeColor="White" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
C#:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection sqlCon = new SqlConnection();
SqlCommand sqlCmd = new SqlCommand();
string connectionString;
protected void Page_Load(object sender, EventArgs e)
{
OpenConnection();
if (!IsPostBack)
{
fnFillGrid();
fillDdlChapter();
secondDiv.Visible = false;
}
}
public void OpenConnection()
{
connectionString = System.Configuration.ConfigurationSettings.AppSettings["myconn"];
sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
}
public void fnFillGrid()
{
DataSet ds = new DataSet();
sqlCmd = new SqlCommand("select distinct(job_id) from job_details order by job_id", sqlCon);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sqlCmd;
da.Fill(ds);
grvTask.DataSource = ds;
grvTask.DataBind();
}
public void fillDdlChapter()
{
for (int i = 0; i < grvTask.Rows.Count; i++)
{
Label lblJob_no = ((Label)(grvTask.Rows[i].Cells[2].Controls[1]).FindControl("lblJob_no"));
DropDownList ddlchapter = ((DropDownList)(grvTask.Rows[i].Cells[2].Controls[1]).FindControl("ddChapter"));
DataSet ds = new DataSet();
sqlCmd = new SqlCommand("select distinct(chapter_no),job_id from chap_details where job_id ='" + lblJob_no.Text + "' ", sqlCon);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sqlCmd;
da.Fill(ds);
ddlchapter.DataSource = ds;
ddlchapter.DataValueField = "job_id";
ddlchapter.DataTextField = "chapter_no";
ddlchapter.DataBind();
}
}
protected void btnView_Click(object sender, EventArgs e)
{
string jobno = Convert.ToString(((HiddenField)(((Button)sender).Parent.FindControl("hdnJobNo"))).Value);
string chapter = Convert.ToString(((DropDownList)(((Button)sender).Parent.FindControl("ddChapter"))).SelectedItem);
secondDiv.Visible = true;
DataSet ds = new DataSet();
sqlCmd = new SqlCommand("select job_id,chapter_no,ms_pages from chap_details where job_id='" + jobno + "' and chapter_no='" + chapter + "'", sqlCon);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sqlCmd;
da.Fill(ds);
grvDetails.DataSource = ds;
grvDetails.DataBind();
}
}
Problem is: when i am selecting the second item in the dropdown box of a particular job means it shoes the first item value in the details view.
bye
Murugavel S
Delete Row
Delete Column
Insert Link
×
Insert
Cancel
Embed YouTube Video
×
Width (%)
Height (%)
Insert
Cancel
Table Options
×
Rows
Columns
First row as header
Create Table
Insert Image
×
Selected file:
Alignment
Left
Center
Right
Select an image from your device to upload
Upload to Server
Cancel
Post
Reset
Cancel
Answers (
4
)
Related Discussion