Friday, 28 June 2013

View State Variable Declaration:

Guid viewStateAttendeeId
    {
        get
        {
            if (ViewState["Attendee_Id"] == null)
                return Guid.Empty;

            return (Guid)ViewState["Attendee_Id"];
        }
        set { ViewState["Attendee_Id"] = value; }
    }

Gridview paging:

Html Coding:
<asp:GridView CellPadding="4" ID="grvAttendeeEventList" AllowPaging="true" PageSize="10"
                                OnPageIndexChanging="grvAttendeeEventList_PageIndexChanging" runat="server" AutoGenerateColumns="false"
                                Width="100%" DataKeyNames="Id" EmptyDataText="No Records Found...">
                                <Columns>
                                    <asp:TemplateField HeaderText="Event Title">
                                        <ItemTemplate>
                                            <asp:HiddenField ID="hdnId" Value='<%# Eval("Id") %>' runat="server" />
                                            <asp:LinkButton ID="lnkTitle" runat="server" Text='<%# Eval("Title") %>' OnClick="lnkTitle_Click"></asp:LinkButton>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>





c# Coding:
protected void grvAttendeeEventList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvAttendeeEventList.PageIndex = e.NewPageIndex;
        grvAttendeeEventList.DataSource = new EventInfoManager().GetAllEventInfos();
        grvAttendeeEventList.DataBind();
    }

Saturday, 22 June 2013

Grid view databinder in row data bound

protected void grvEventFeedbackDetailList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (((GridViewRow)e.Row).RowType == DataControlRowType.Header) return;
        if (((GridViewRow)e.Row).RowType == DataControlRowType.DataRow)
        {
            if (DataBinder.Eval(e.Row.DataItem, "StatusId") == null || (int)DataBinder.Eval(e.Row.DataItem, "StatusId") == (int)StatusTypes.Inactive)
                ((CheckBox)((GridViewRow)e.Row).FindControl("chkBoxApprove")).Checked = false;
            else if ((int)DataBinder.Eval(e.Row.DataItem, "StatusId") == (int)StatusTypes.Active)
                ((CheckBox)((GridViewRow)e.Row).FindControl("chkBoxApprove")).Checked = true;
        }
    }

Monday, 10 June 2013

image upload

html
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <script type="text/javascript">
        function showFileSelector() {
            var fileControl = document.getElementById("fileLogo");
            fileControl.click();
            return false;
        }

        function getFileAndPath() {

            var fileControl = document.getElementById("fileLogo");

            var lblUploadedLogo = document.getElementById("lblUploadedLogo");
            lblUploadedLogo.innerHTML = fileControl.value;

            $("#manageFile").show();
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <table width="100%" cellpadding="10px" cellspacing="0">
        <tr>

                       
                       
                        <td  valign="top">
                            <div style="width: 76px; height: 78px; background-color: #F2F2F2; border: 1px solid #CFCFCF;
                                padding: 10px;">
                                <asp:Image ID="imgLogo" ImageUrl="~/images/Default_Image.jpg" ToolTip="Click to upload / change Image."
                                    runat="server" Width="76px" Height="78px" onclick="return showFileSelector()"
                                    Style="cursor: pointer" />
                                <asp:FileUpload ClientIDMode="Static" ID="fileLogo" runat="server" onchange="getFileAndPath();"
                                    Style="display: none;" />
                                <asp:HiddenField ID="hdnLogoPath" runat="server" />
                                <asp:HiddenField ID="hdnUploadedLogo" runat="server" />
                            </div>
                            <div id="manageFile" style="display: none; float: left">
                                <asp:Label ID="lblUploadedLogo" ClientIDMode="Static" runat="server" Text="Label"></asp:Label>
                                <asp:LinkButton ID="lnkUploadLogo" runat="server" OnClick="lnkUploadLogo_Click">Upload</asp:LinkButton>
                            </div>
                        </td>
                    </tr>
    </table>
</asp:Content>


//////////
c#:
protected void lnkUploadLogo_Click(object sender, EventArgs e)
    {
        string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + "_" + Path.GetFileName(fileLogo.PostedFile.FileName);

        fileLogo.PostedFile.SaveAs(Server.MapPath(@"TempFiles/" + fileName));//TempFiles is folder , you have to create its for temporary period.

        imgLogo.ImageUrl = @"TempFiles/" + fileName;
        hdnUploadedLogo.Value = @"TempFiles/" + fileName;
    }

//upload image to a permanent folder at the time of save.
private string UploadImage(string countryId)
    {
        string galleryPath = string.Empty;
        string mappedPath = string.Empty;

        if (hdnUploadedLogo.Value != "")
        {
            FileInfo tempfile = new FileInfo(Server.MapPath(hdnUploadedLogo.Value));
            mappedPath = Server.MapPath(@"../Repository/ImageGallery") + @"\" + countryId;

            if (!Directory.Exists(mappedPath))
                Directory.CreateDirectory(mappedPath);

            if (Directory.Exists(mappedPath + @"\" + tempfile.Name))
            {
                FileInfo oldfile = new FileInfo(mappedPath + @"\" + tempfile.Name);
                oldfile.Delete();
            }

            tempfile.MoveTo(mappedPath + @"\" + tempfile.Name);

            galleryPath += "../Repository/ImageGallery/" + countryId + "/" + tempfile.Name;
        }
        else
            galleryPath = hdnLogoPath.Value;

        return galleryPath;
    }








in case of thumbnail one ex is
 ImagePath = "";
        ThumbnailPath = "";

        string imageMappedPath = "";
        string thumbnailMappedPath = "";

        if (hdnUploadedContent.Value != "")
        {
            FileInfo tempfile = new FileInfo(Server.MapPath(hdnUploadedContent.Value));
            imageMappedPath = Server.MapPath("../Repository/EventGallery/") + Id + "/Images/";
            thumbnailMappedPath = Server.MapPath("../Repository/EventGallery/") + Id + "/Thumbnails/";

            if (!Directory.Exists(imageMappedPath))
                Directory.CreateDirectory(imageMappedPath);

            if (!Directory.Exists(thumbnailMappedPath))
                Directory.CreateDirectory(thumbnailMappedPath);

            System.Drawing.Image uploadedImage = System.Drawing.Image.FromFile(Server.MapPath(hdnUploadedContent.Value));
            System.Drawing.Image image = GlobalOperations.ResizeImage(uploadedImage, new System.Drawing.Size(400, 300));
            System.Drawing.Image thumbnail = GlobalOperations.ResizeImage(uploadedImage, new System.Drawing.Size(100, 100));

            image.Save(imageMappedPath + tempfile.Name);
            thumbnail.Save(thumbnailMappedPath + tempfile.Name);

            ImagePath += "~/Repository/EventGallery/" + Id + "/Images/" + tempfile.Name;
            ThumbnailPath += "~/Repository/EventGallery/" + Id + "/Thumbnails/" + tempfile.Name;

            uploadedImage.Dispose();
            File.Delete(Server.MapPath(hdnUploadedContent.Value));
        }
        else
        {
            ImagePath = hdnContentImagePath.Value.ToString();
            ThumbnailPath = hdnThumbnailPath.Value.ToString();
        }
   

Video play in Youtube

HTML:
<tr id="linkContent" runat="server" visible="false">
                        <td>
                            Link Path
                        </td>
                        <td>
                            :
                        </td>
                        <td>
                            <asp:TextBox ID="txtLinkPath" runat="server" TextMode="MultiLine"></asp:TextBox>
                            <asp:Button ID="btnLinkPathUpload" runat="server" Text="Upload" OnClick="btnLinkPathUpload_Click" />
                        </td>
                    </tr>
 <tr>
                        <td colspan="2">
                        </td>
                        <td>
                            <div id="videoAndMusicContent" runat="server" visible="false">
                                <asp:HyperLink ID="lnkAttachment" runat="server"></asp:HyperLink>
                                <br />
                                <br />
                                <asp:Literal ID="ltrVideoAndMusic" runat="server"></asp:Literal>
                            </div>
                        </td>
                    </tr>


C# Code:

protected void btnLinkPathUpload_Click(object sender, EventArgs e)
    {
        try
        {
            videoAndMusicContent.Visible = true;
            txtLinkPath.Text = txtLinkPath.Text.Replace("watch?v=", "v/");
            ltrVideoAndMusic.Text = "<object width='300' height='300'>             <param name='movie' value='" + txtLinkPath.Text + "'>             </param><param name='allowFullScreen' value='true'>             </param><param name='allowscriptaccess' value='always'>             </param><embed src='" + txtLinkPath.Text + "' type='application/x-shockwave-flash' allowscriptaccess='always'             allowfullscreen='true' width='425' height='344'>             </embed>         </object>";
            hdnUploadedContent.Value = txtLinkPath.Text;
        }
        catch (Exception)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "Msg Box", "alert('Its not a Proper video or Media File')", true);
        }
    }

Video Upload (html, c#code)

Html Code:
 <tr id="videoAndMusicUpload" runat="server" visible="false">
                        <td>
                            Upload Video
                        </td>
                        <td>
                            :
                        </td>
                        <td>
                            <div>
                                <asp:FileUpload ID="fluVideoAndMusic" runat="server" />
                                <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
                                <%--<br />
                                <br />
                                <asp:HyperLink ID="lnkAttachments" runat="server"></asp:HyperLink>
                                <br />
                                <br />
                                <asp:Literal ID="ltrVideoAndMusics" runat="server"></asp:Literal>--%>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                        </td>
                        <td>
                            <div id="videoAndMusicContent" runat="server" visible="false">
                                <asp:HyperLink ID="lnkAttachment" runat="server"></asp:HyperLink>
                                <br />
                                <br />
                                <asp:Literal ID="ltrVideoAndMusic" runat="server"></asp:Literal>
                            </div>
                        </td>
                    </tr>


C# Code:

protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (fluVideoAndMusic.HasFile)
        {
            videoAndMusicContent.Visible = true;
            string path = Server.MapPath(@"TempFiles/");
            string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + "_" + Path.GetFileName(fluVideoAndMusic.PostedFile.FileName);
            //Session["videoPathFileName"] = path + fileName;
            fluVideoAndMusic.PostedFile.SaveAs(path + fileName);
            ltrVideoAndMusic.Text = "<object classid='clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6' id='Player1' width='300'            height='200'>            <param name='URL' value='" + path + fileName + "' />            <param name='" + path + fileName + "' />            <param name='AutoStart' value='1' />            <param name='ShowControls' value='1' />            <param name='ShowStatusBar' value='1' />            <param name='ShowDisplay' value='1' />            <param name='stretchToFit' value='1' />            <embed type='application/x-mplayer2' pluginspage='http://www.real.com/player/'                width='300' height='200' src='" + path + fileName + "' filename='" + path + fileName + "'autostart='1' showcontrols='1' showstatusbar='1' showdisplay='1'></embed>        </object>";
            hdnUploadedContent.Value = @"TempFiles/" + fileName;

            if (hdnUploadedContent.Value != "")
            {
                lnkAttachment.Text = hdnUploadedContent.Value.Substring(25);
                lnkAttachment.BackColor = System.Drawing.Color.Gray;
                lnkAttachment.NavigateUrl = hdnUploadedContent.Value;
            }
            else
            {
                lnkAttachment.Text = "No Attachment";
                lnkAttachment.NavigateUrl = "";
            }
        }
    }