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

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;
        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'>");

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

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

Write below javascript code in to your ASPX page.

var oldgridSelectedColor;
function setMouseOverColor(element) {
    oldgridSelectedColor = element.style.backgroundColor;
    element.style.backgroundColor = '#dcdcdc';
    element.style.cursor = 'hand';
}

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() {
}
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;
        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'>");
        sb.Append("</OBJECT>");
        Response.Write(sb.ToString());
    }
}

 


Similar Articles