Monday, July 8, 2013

Media Player using ASP.net C#


protected void btnSave_Click(object sender, EventArgs e)
    {
        string fname = Path.GetFileName(flupUpload.PostedFile.FileName.ToString());
        string ext = Path.GetExtension(fname);
       
            if (flupUpload.HasFile)
            {
                flupUpload.SaveAs(Server.MapPath("fileCollection/" + fname));
                string ins = "insert into tbl_upload(file_caption,file_name)values('" + txtcaption.Text + "','" + fname + "')";
                obj.execute_Query(ins);
            }
       
    }
    protected void btnShow_Click(object sender, EventArgs e)
    {
        string sel = "select * from tbl_upload";
        obj.fillGrid(grdFiles, sel);
    }

    protected void grdFiles_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "play")
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());
            Response.Redirect("Player.aspx?fid=" + id + "");
        }
    }
  
}

Player Page
public partial class MediaPlayer_Player : System.Web.UI.Page
{
    public static string videoname;
    Cls_ADO obj = new Cls_ADO();
    protected void Page_Load(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request.QueryString["fid"]);
        string str = "select * from tbl_upload where file_id=" + id + "";
        DataTable dt = new DataTable();
        dt = obj.select_Query(str);
        if (dt.Rows.Count > 0)
        {
            videoname = dt.Rows[0]["file_name"].ToString();
        }
    }
}

Source Code
<table class="style1">
            <tr>
                <td>
                    <object id="mediaPlayer" classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95"
                        codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"
                        standby="Loading Microsoft Windows Media Player components..."
                        style="width: 846px; height: 416px;" type="application/x-oleobject">
        <param name="fileName" value="fileCollection/<%=videoname %>" />
                        <param name="animationatStart" value="true" />
                        <param name="transparentatStart" value="true" />
                        <param name="autoStart" value="true" />
                        <param name="showControls" value="true" />
                        <param name="loop" value="true" />
   <embed id="EMBED1" autosize="-1" autostart="true" bgcolor="darkblue"
                            designtimesp="5311" displaysize="4" height="285" loop="true" name="mediaPlayer"
                            pluginspage="http://microsoft.com/windows/mediaplayer/en/download/"
                            showcontrols="true" showdisplay="0" showstatusbar="-1" showtracker="-1"
                         
src="fileCollection/<%=videoname %>"
type="application/x-mplayer2" videoborder3d="-1" width="320"></embed>
      </EMBED>
      </object>
                </td>
            </tr>

        </table>

Thursday, June 20, 2013

Grid View Editing


design

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                        ForeColor="#333333" GridLines="None" Width="502px"
                        onrowcancelingedit="GridView1_RowCancelingEdit"
                        onrowdatabound="GridView1_RowDataBound" onrowediting="GridView1_RowEditing"
                        onrowupdating="GridView1_RowUpdating"
                        onselectedindexchanged="GridView1_SelectedIndexChanged"
                        style="text-align: center" DataKeyNames="u_id">
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:TemplateField HeaderText="Name">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtname" runat="server" Text='<%# Bind("u_name") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("u_name") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Gender">
                                <EditItemTemplate>
                                    <asp:RadioButtonList ID="rdbGender" runat="server" RepeatDirection="Horizontal">
                                        <asp:ListItem>Male</asp:ListItem>
                                        <asp:ListItem>Female</asp:ListItem>
                                    </asp:RadioButtonList>
                                    <asp:TextBox ID="txtGender" runat="server" Text='<%# Bind("u_gender") %>' Visible="false"></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("u_gender") %>'></asp:Label>
                                    <asp:RadioButtonList ID="rdbGender" runat="server" RepeatDirection="Horizontal" Visible="false">
                                        <asp:ListItem>Male</asp:ListItem>
                                        <asp:ListItem>Female</asp:ListItem>
                                    </asp:RadioButtonList>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Class">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ddlClass" runat="server" Height="16px" Width="88px"
                                        onselectedindexchanged="ddlClass_SelectedIndexChanged">
                                    </asp:DropDownList>
                                    <asp:TextBox ID="txtClass" runat="server" Text='<%# Bind("c_name") %>' Visible="false"></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("c_name") %>'></asp:Label>
                                    <asp:DropDownList ID="ddlClass" runat="server" Height="16px" Width="88px" Visible="false">
                                    </asp:DropDownList>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowEditButton="True" />
                            <%--
                            <asp:BoundField DataField="u_name" HeaderText="Name" />

                            <asp:BoundField DataField="u_gender" HeaderText="Gender" />

                            <asp:BoundField DataField="c_name" HeaderText="Class" />

                            <asp:CommandField ShowEditButton="True" />--%>
                        </Columns>
                        <EditRowStyle BackColor="#7C6F57" />
                        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#E3EAEB" />
                        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F8FAFA" />
                        <SortedAscendingHeaderStyle BackColor="#246B61" />
                        <SortedDescendingCellStyle BackColor="#D4DFE1" />
                        <SortedDescendingHeaderStyle BackColor="#15524A" />
                    </asp:GridView>


Code

 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        FillGrid();

    }
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    { if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox txtG = (TextBox)e.Row.FindControl("txtGender");
                RadioButtonList rdb = (RadioButtonList)e.Row.FindControl("rdbGender");
                if (txtG != null)
                {
                    if (txtG.Text == "Male")
                    {
                        rdb.Items[0].Selected = true;

                    }
                    else
                    {
                        rdb.Items[1].Selected = true;
                    }
                }

                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlClass");
                TextBox txtC = (TextBox)e.Row.FindControl("txtClass");
                string str = "select * from tbl_class";
                SqlDataAdapter adt = new SqlDataAdapter(str, con);
                DataTable dt = new DataTable();
                adt.Fill(dt);
                ddl.DataSource = dt;
                ddl.DataTextField = "c_name";
                ddl.DataValueField = "c_id";
                ddl.DataBind();
                if (txtC != null)
                {
                    ddl.SelectedValue = ddl.Items.FindByText(txtC.Text).Value;
                }
            }
     
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        FillGrid();
   
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        TextBox txtn = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname");
        RadioButtonList rdbg = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("rdbGender");
        DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlClass");
        string strt = ddl.SelectedValue;
        TextBox txt= (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname");
        string str = "update tbl_user set u_name='" + txtn.Text + "',u_gender='" + rdbg.SelectedItem + "',u_c_id=" +Convert.ToInt32(strt)   + "where u_id=" + id + "";
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
 
        FillGrid();
     


    }

Tuesday, June 18, 2013

Calling javascript function from CodeBehind with C#

Injecting a javascript code into a label control

This is one of the simplest method to call a javascript function from code behind. You need to do the following:
  1. Create a new website project
  2. Add a label and button control on your Default.aspx page
  3. You body part of the markup should now look something like this:
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Label ID="lblJavaScript" runat="server" Text=""></asp:Label>
            <asp:Button ID="btnShowDialogue" runat="server" Text="Show Dialogue" />
        </div>
        </form>
    </body> 
  4. Add a javascript function to show a message box as shown below:
    <head runat="server">
        <title>Calling javascript function from code behind example</title>
            <script type="text/javascript">
                function showDialogue() {
                    alert("this dialogue has been invoked through codebehind.");
                }
            </script>
    </head>
  5. Now double-click on the button and add the following code:
    lblJavaScript.Text = "<script type='text/javascript'>showDialogue();</script>";
  6. Run your project and click the Show Dialogue button
  7. A message box will be displayed as shown below:
    screen shot of client side message box

Logout or signout problem in asp.net c# using Master page and windows authentication



Login Page
protected void Page_Load(object sender, EventArgs e)
{
    Session["imp"] = "0";            
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
    Session["imp"] = "1";
    Response.Redirect("AdminHome.aspx");
}
Log Out Page
protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetNoStore();
    if (Session["imp"].ToString() == "1")
    { }
    else
    {
        Response.Redirect("HomePage.aspx");
    }  
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
    Session["imp"] = "0";
    Session.Abandon();
    Response.Clear();
    Response.Redirect("HomePage.aspx");
}