Monday, 19 August 2013

How to force controls to work on postback of other controls inside GridView



1.

     <asp:TemplateField HeaderText="">
        <EditItemTemplate>
            <asp:RadioButtonList ID="rdVorI" runat="server" RepeatDirection="Horizontal" AutoPostBack="true"
                OnSelectedIndexChanged="rdVorI_SelectedIndexChanged">

                <asp:ListItem Value="Video" Text="Video" Selected="True"></asp:ListItem>
                <asp:ListItem Value="Image" Text="Image"></asp:ListItem>
            </asp:RadioButtonList>
        </EditItemTemplate>
    </asp:TemplateField>
   
    <asp:TemplateField HeaderText="Video">
        <ItemTemplate>
            <asp:ImageButton ID="vdoBtn" Height="60px" Width="60px" AlternateText="Video" BorderWidth="1px"
                BorderColor="Black" runat="server" title="Click Here" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtEvCode" runat="server" Width="160px" />
        </EditItemTemplate>
    </asp:TemplateField>
   
    <asp:TemplateField HeaderText="Images">
        <ItemTemplate>
            <asp:HiddenField ID="key" runat="server" Value='<%#Eval("EventID") %>' />   
                                          
            <asp:ImageButton ID="imgBtn" class="thumbnail" AlternateText="Image" BorderWidth="1px"
                BorderColor="Black" runat="server" />
            <asp:ImageButton ID="imgBtn2" class="thumbnail" AlternateText="Image" BorderWidth="1px"
                BorderColor="Black" runat="server" />
            <asp:ImageButton ID="imgBtn3" class="thumbnail" AlternateText="Image" BorderWidth="1px"
                BorderColor="Black" runat="server" />
        </ItemTemplate>                                
        <EditItemTemplate>
            <asp:FileUpload ID="FileUpload1" runat="server" /><br />
            <asp:FileUpload ID="FileUpload2" runat="server" /><br />
            <asp:FileUpload ID="FileUpload3" runat="server" />
        </EditItemTemplate>
    </asp:TemplateField>
   
2.
   
    protected void rdVorI_SelectedIndexChanged(object sender, EventArgs e)
    {
 
        GridViewRow gr = (GridViewRow)((DataControlFieldCell)((RadioButtonList)sender).Parent).Parent;
        RadioButtonList rdl = (RadioButtonList)gr.FindControl("rdVorI");
        FileUpload vFile1 = (FileUpload)gr.FindControl("FileUpload1");
        FileUpload vFile2 = (FileUpload)gr.FindControl("FileUpload2");
        FileUpload vFile3 = (FileUpload)gr.FindControl("FileUpload3");
        TextBox vTitle = (TextBox)gr.FindControl("txtEvCode");
        if (rdl.SelectedValue == "Video")
        {
            vFile1.Enabled = false;
            vFile2.Enabled = false;
            vFile3.Enabled = false;
            vTitle.Enabled = true;
        }
        else
        {

            vTitle.Enabled = false;
            vFile1.Enabled = true;
            vFile2.Enabled = true;
            vFile3.Enabled = true;
        }

    }


Cheers!!!

Tuesday, 13 August 2013

Dynamically generate and display Thumbnail picture by resizing the Image in ASP.Net

In this article I explain how to dynamically resize image and generate its Thumbnail and also display it on ASP.Net web page.
HTML Markup
In the form below as you can see I have 2 ASP.Net Image Controls of which one displays the original image while other displays the Thumbnail of the original image. Also there is an ASP.Net Button control which generates the Thumbnail.
 
<form id="form1" runat="server">
<asp:Image ID="Image1" runat="server" ImageUrl = "~/Jellyfish.jpg" Height = "400px" Width = "400px"/>
<br />
<asp:Button ID="btnGenerate" OnClick = "GenerateThumbnail" runat="server" Text="Generate Thumbnail" />
<hr />
<asp:Image ID="Image2" runat="server" Visible = "false"/>
</form>
 
 
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
 
Resizing Image and Generating Thumbnail
Below is the code that dynamically resizes the picture and generates the Thumbnail and also displays it in Image Control
C#
protected void GenerateThumbnail(object sender, EventArgs e)
{
    string path = Server.MapPath("~/Jellyfish.jpg");
    System.Drawing.Image image = System.Drawing.Image.FromFile(path);
    using (System.Drawing.Image thumbnail = image.GetThumbnailImage(100, 100, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            thumbnail.Save(memoryStream, ImageFormat.Png);
            Byte[] bytes = new Byte[memoryStream.Length];
            memoryStream.Position = 0;
            memoryStream.Read(bytes, 0, (int)bytes.Length);
            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
            Image2.ImageUrl = "data:image/png;base64," + base64String;
            Image2.Visible = true;
        }
    }
}
 
public bool ThumbnailCallback()
{
    return false;
}
 
 In GridView As:
 
If you want to show thumbnal in girdview than;

1.  OnRowDataBound="gvEditEvents_RowDataBound"

    <asp:TemplateField HeaderText="Images">
        <ItemTemplate>
            <asp:ImageButton ID="imgBtn" AlternateText="Image" BorderWidth="1px"
                BorderColor="Black" runat="server" />                                 
        </ItemTemplate>
    </asp:TemplateField>
   
2.
    protected void gvEditEvents_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
             if ((e.Row.RowState & DataControlRowState.Edit) == 0)
                {
                    string imgPath = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ImagePathOne"));
                    if (!string.IsNullOrEmpty(imgPath))
                    {
                        ImageButton imgBtn = (ImageButton)e.Row.FindControl("imgBtn");

                        string path = Server.MapPath(imgPath.Replace("\\RootFolder", "..\\.."));
                        System.Drawing.Image image = System.Drawing.Image.FromFile(path);
                        using (System.Drawing.Image thumbnail = image.GetThumbnailImage(60, 60, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
                        {
                            using (MemoryStream memoryStream = new MemoryStream())
                            {
                                thumbnail.Save(memoryStream, ImageFormat.Png);
                                Byte[] bytes = new Byte[memoryStream.Length];
                                memoryStream.Position = 0;
                                memoryStream.Read(bytes, 0, (int)bytes.Length);
                                string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                                imgBtn.ImageUrl = "data:image/png;base64," + base64String;
                                imgBtn.Visible = true;
                            }
                        }
                    }
                 }
            }
        }
      
3.

    public bool ThumbnailCallback()
    {
        return false;
    }
   
4.
   
    using System.IO;
    using System.Drawing;
    using System.Drawing.Imaging;
Cheers!!
   
    

Friday, 9 August 2013

Disable all events in web pages.

1.  Disable all events of a page.
<script language="javascript" type="text/javascript">
 $(document).ready(function() {
            $(document).bind("contextmenu", function(e) {
                     return false;

             }); 
}); 
 </script>
2.  Disable all events in Iframe.
 <script language="javascript" type="text/javascript">
      function disableRightClick(frameID)
      {
            var ifrm = document.getElementById(frameID);
            var innerDoc = ifrm.contentDocument ? ifrm.contentDocument : ifrm.contentWindow.document;
            $(innerDoc).bind("contextmenu", function(e) {
                return false;
            });
      }

 </script>
 and Call : onload="javascript:disableRightClick('
myframe');"

 NOTE: 
1. must add jquery file (jquery-1.4.min.js) to enable jquery on your page.
2. In iframe put all attrubute in double quote as id="myframe".


Cheers!!