Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » Visual C# » Create your music player in c# using gridview with simple logic

Create your music player in c# using gridview with simple logic

This article gives you an idea how to create your own mp3 player in asp.net 2.0.

Page Views : 39011
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

public partial class _Default : System.Web.UI.Page
{
     public string xFile;
     public void getFileList()
     {
         
DirectoryInfo di=new DirectoryInfo("C://Temp/My");
          DataTable dt = new DataTable();
          dt.Columns.Add("songname");
          if (di.Exists == true)
          {
              foreach (FileInfo fi in di.GetFiles())
              {
                   DataRow dr = dt.NewRow();
                   dr["songname"] = fi.FullName;
                   dt.Rows.Add(dr);
              }
          }
          GridView1.DataSource = dt;
          GridView1.DataBind();
          }
protected void Page_Load(object sender, EventArgs e)
{
     if (!IsPostBack)
     {
         getFileList(); }
     }
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType != DataControlRowType.Header)
     {
          e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
          e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
          e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink((GridView
         
sender, "Select$" + e.Row.RowIndex);
     }
}
protected void imgBt_Click(object sender, ImageClickEventArgs e)
{
     xFile = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
     TextBox1.Text = xFile;
     string _Height = "50";
     string _Width = "250";
     StringBuilder sb = new StringBuilder("<OBJECT ID='" + this.ClientID + "' name='" + 
     this
.ClientID + "' " + "CLASSID='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'" +
     "VIEWASTEXT" +"height=" + _Height + " " + "width=" + _Width + ">");
    
string _fileName = xFile; //"C://myExample/008 BACHAPAN - MANHAR.MP3";
     sb.Append("<PARAM name='URL' value='" + xFile + "'>");
     sb.Append("<PARAM name='AutoStart' value='True'>");
     sb.Append("<PARAM name='playCount' value='1'>");
     sb.Append("<PARAM name='volume' value='100'>");

     //output ending object tag

     sb.Append("</OBJECT>");

     //flush everything to the output stream

     Response.Write(sb.ToString());
}

////

Write below javascript code in to your ASPX page

<script type="text/javascript">
var oldgridSelectedColor;
function setMouseOverColor(element)
{
oldgridSelectedColor = element.style.backgroundColor;
element.style.backgroundColor='#dcdcdc';
element.style.cursor='hand';

//element.style.textDecoration='underline';
}
function setMouseOutColor(element)
{
    
element.style.backgroundColor=oldgridSelectedColor;
    
element.style.textDecoration='none';
}
function playMe(xfile)
{
    
alert(xfile);
    
var mfile = xfile;
    
document.write("<EMBED SRC='"+ mfile +"' WIDTH=100 HEIGHT=100></EMBED>");
     
alert(mfile);
}
     
function checkm()
</script>

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Kaushik Patel
I am working as Sr.Developer with MNC company in Ahmedabad.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
hi by Roy On December 21, 2007
If i don't have media player installed on my machine then what will happen? Anyswayz good short smart article
Reply | Email | Modify 
c# coding by yuvraj On December 28, 2007
made it in more simple language so that i can understand it i am a c#(gui) student so it's hard to got this copde easily
Reply | Email | Modify 
C#CODiNG by yuvraj On December 28, 2007
MAKE IT IN SImPLE CODING SO CAN I UNDErSTAND I AM A c#(G U I) STUDeNT SO It'S hARd TO UNDeRStAND mAIL ME THIS CONTeNT AT yuvilikeu@c-sharpcorner.com it's a humble request pleaseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
Reply | Email | Modify 
Thanks by thiruna On October 4, 2008
This is what I wanted. I just modiifed the Grid view to be filled from a database. Thanks a lot
Reply | Email | Modify 
Update.... I had some trouble so I modified it and tossed in an Ajax update panel by Michael On June 10, 2009

I was having some trouble getting the events to work. So I changed that around some. I changed it so it recurses all directories under the path for .mp3 files. I added an update panel so that the Grid doesn't refresh when you play a song. Helps with response time if the Grid is really large. I also reduced the code required to play the songs.. I also removed some of the java script that wasn't being used.


public
string xFile;

public void getFileList()

{

DirectoryInfo di = new DirectoryInfo("Your dir ");

DataTable dt = new DataTable();

dt.Columns.Add("songname");

if (di.Exists == true)

{

foreach (FileInfo fi in di.GetFiles("*.mp3", SearchOption.AllDirectories))

//foreach (FileInfo fi in di.GetFiles())

{

DataRow dr = dt.NewRow();

dr["songname"] = fi.FullName;

dt.Rows.Add(dr);

}

}

GridView1.DataSource = dt;

GridView1.DataBind();

}

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

getFileList();

}

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";

e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";

LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];

if (_singleClickButton != null)

{

string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "Select$" + e.Row.RowIndex);

e.Row.Attributes["onclick"] = _jsSingle;

}

}

}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

{

xFile = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;

TextBox1.Text = xFile;

embed1.Attributes.Add("src", xFile);

}

}

Font page:

<script type="text/javascript">

var oldgridSelectedColor;

function setMouseOverColor(element) {

oldgridSelectedColor = element.style.backgroundColor;

element.style.backgroundColor = '#dcdcdc';

element.style.cursor = 'hand';

//element.style.textDecoration='underline';

}

function setMouseOutColor(element) {

element.style.backgroundColor = oldgridSelectedColor;

element.style.textDecoration = 'none';

}

</script>

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="scriptmanager1" runat="server">

</asp:ScriptManager>

<asp:UpdatePanel ID="upanel1" runat="server" UpdateMode="Conditional"

ChildrenAsTriggers="False" >

<Triggers>

<asp:AsyncPostBackTrigger ControlID="GridView1"

EventName="SelectedIndexChanged" />

</Triggers>

<ContentTemplate>

<embed runat="server" id="embed1" name="embed1" src=""

width="250" height="200"></embed>

<asp:TextBox ID="TextBox1" runat="server">

</asp:TextBox>

</ContentTemplate>

</asp:UpdatePanel>

<div>

<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" runat="server"

Height="154px" Width="243px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">

<Columns>

<asp:ButtonField CommandName="select" ButtonType="Link" runat="server" Text="select"

Visible="false" />

</Columns>

</asp:GridView>

</div>

</form>

</body>

</html>



 

Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.