Pages

Showing posts with label tricks. Show all posts
Showing posts with label tricks. Show all posts

Thursday, October 9, 2014

How To: Raising Custom Events from Custom .Net UserControls

In the last few days I've been searching books and websites to get back to "object oriented programming" I remember in my head from back time in college java and C++ courses about classes, event handling, delegates, inheritance .. etc but can't seem to get what I have in mind done in current C# development work requirements as custom electronic request forms automation, built with custom .net controls in SharePoint environment to be reused among them all when needed.

in this post I'll be showing you how to raise an event from within usercontrol's native controls events and handle it with a custom code in webpage code file hosting the usercontrol.

watching Kudvenkat's YouTube videos below helped a lot to understand the event handling technique by following the 5 steps to raise an event from the usercontrol

  1. Step 1: Create XxEventArgs class that will contain the event data.
  2. Step 2: Create XxChangedEventHandler delegate that raises this event. 
  3. Step 3: Create XxChanged event that is a variable of type delegate.
  4. Step 4: Create a protected virtual method to raise the event that enables the derived classes to do some additional work before the event can be raised. 
  5. Step 5: Finally raise the event, whenever the usercontrol's controls is changed.
Let's say, we want to raise TimeChanged event every time the usercontrol ( from the previous post ) controls values changes. i.e, when hours TextBox's Text is changed, minutes TextBox's Text is changed, or time DropDownList's Index is Changed.
  1. Step 1: Create TimeChangedEventArgs class that will contain the event data:


    public class TimeChangedEventArgs : EventArgs
    {
        private string _ucTimeValue;

        // Constructor to initialize event data
        public TimeChangedEventArgs(string ucTimeValue)
        {
            this._ucTimeValue = ucTimeValue;
        }

        // Returns ucTimeValue   
        public string UcTimeValue
        {
            get { return this._ucTimeValue; }
        }
    }


  2. Step 2: Create TimeChangedEventHandler delegate that raises this event.


    public delegate void TimeChangedEventHandler(object sender, TimeChangedEventArgs e);

  3. Step 3: Create TimeChanged event that is a variable of type delegate inside the usercontrol's class:


    public
    event TimeChangedEventHandler TimeChanged;

  4. Step 4: Create a protected virtual method to raise the event that enables the derived classes to do some additional work before the event can be raised.
    Checking if TimeChanged is null is a good practice as it will give error message in cases when you don't need to have a custom event handling in usercontrol's host. 

    protected
    virtual void OnTimeChanged(TimeChangedEventArgs e)
        {
            if (TimeChanged != null)
            {
                TimeChanged(this, e);
            }
        }

  5. Step 5: Finally raise the event, whenever the usercontrol's controls is changed:
    by assigning the following event handlers to the controls and then raising the event 
    1. When hours TextBox's Text is changed:


        protected void hourTxtBx_TextChanged(object sender, EventArgs e)
          {
               OnTimeChanged(new TimeChangedEventArgs(this.hourTxtBx.Text + ":" + this.minTxtBx.Text + " " + timeDDL.SelectedValue));

          }

    2. When minutes TextBox's Text is changed:


        protected void minTxtBx_TextChanged(object sender, EventArgs e)
          {
               OnTimeChanged(new TimeChangedEventArgs(this.hourTxtBx.Text + ":" + this.minTxtBx.Text + " " + timeDDL.SelectedValue));

          }

    3. When time DropDownList's Index is Changed:


        protected void timeDDL_OnSelectedIndexChanged(object sender, EventArgs e)
          {
              OnTimeChanged(new TimeChangedEventArgs(this.hourTxtBx.Text + ":" + this.minTxtBx.Text + " " + timeDDL.SelectedValue));

          }

Now to consume the usercontrol custom event:
  1.  Step 1: Create an event handler method. The method signature must match the signature of the "TimeChangedEventHandler" delegate.


    protected
    void fromTimeUC_OnTimeChanged(object sender, TimeChangedEventArgs e)
        {
            string errMsg = "";
             
            timeErrLbl.Text = (!checkTime(e.UcTimeValue , out errMsg)) ? errMsg : "";
            timeErrLbl.Visible = (!string.IsNullOrEmpty(errMsg))
        }

  2. Step 2: Register the created event handler method to the usercontrol OnTimeChanged event:


  <uc1:jpTimeChooser35UC OnTimeChanged="fromTimeUC_OnTimeChanged" ID="fromTimeUC"  runat="server" />




That's it ..  hope it was helpful :)

Reference:

Saturday, August 16, 2014

How To: Load SharePoint web part User Control Inside another SharePoint web part without C# Coding

simple way to Load SharePoint web part User Control Inside another  SharePoint web part without C# Coding is done by :

  1. registering the user control
    <%@ Register src="webPartUC.ascx" tagname="webPartUC" tagprefix="uc" %>

  2. loading the user control <uc:webPartUC ID="webPartUC1" runat="server" />
if you wanted to pass webpart properties.. all you have to do is to specify the values as follows:
<uc:webPartUC ID="webPartUC1" LabelText="TEXT" runat="server" />

if you wanted to pass webpart properties from within a DataList Control Items .. all you have to do is to specify the values as follows making sure to add the below line inside the DataList:

 <asp:DataList ID="usersDL" runat="server" Width="100%" >
   <ItemTemplate>
         <uc:webPartUC ID="webPartUC1"
              LabelText=<%# Eval("userName")%> runat="server" />

    </ItemTemplate>
 </asp:DataList>


Thats it :)

    

Saturday, May 17, 2014

Embed PDF in HTML Page


To Embed PDF files in HTML Page You can use below snippet of code, It works in most browser versions (Chrome, Firefox, IE,... ).

if it is not supported, it shows the message "The PDF cannot be displayed."

<div id="pdf">
<object width="900" height="1000" type="application/pdf" data="/filepath/filename.pdf?#zoom=85&scrollbar=0&toolbar=0&navpanes=0" id="pdf_content">
<p>The PDF cannot be displayed. <a href="/filepath/filename.pdf" target="_top">download</a>.</p>
</object>
</div>

Above snippet of code sets the zoom to 85%, removes scrollbars, toolbars and nav panes.

Hope it helps J

SOLVED: 'Download Unsuccessful' Error Notification in Android

Problem:
For a while, it has been an exhausting issue to fix with website users that when trying to download pdf files through the android stock browser or android chrome browser or some other browsers.. 
they get a 'Download Unsuccessful' notification.

Solution:
Now it got clear once understood that android file type association requires certain applications to open specific file types..

As long as the file type you are trying to open\download (pdf, rar, zip, word, …) is unknown or not associated with an existing application.. it won't open\download..

So all you need to do is :
·        If you want to open\download pdf file, install Acrobat Reader first through android PlayStore.
·        If you want to open\download rar, zip etc.. download and install Archidroid from android PlayStore.
·        … and so on.

Conclusion:  
At the end it's not an issue in your website you have to fix.. just file association needs to be fixed in users devices.. that's all J

Tuesday, March 25, 2014

SOLVED: SharePoint Designer Workflow updates doesn't work!!

This time we faced an issue where even after updating workflow steps conditions and actions it doesn't seem to be applied on the server...
The solution was simple as it seems that SharePoint Designer saves local copies from your servers, and although everything seems OK, this prevents it from updating the workflow ..
To solve this issue:
  1. Close SharePoint Designer
  2. Go to
    %LOCALAPPDATA%\Microsoft\WebsiteCache
    Or, on older versions of windows:
    C:\Documents and Settings\%USER%\Local Settings\Application Data\Microsoft\WebSiteCache
  3. Delete either (choose one, based on how annoyed you are by now):
    1. All folders.
    2. The folder that "looks most like your site" (can be more than one: site, site(1), etc, and should be the bottom level site, not the root site)
    3. The relevant DLL in that folder.
  4. Start SharePoint Designer, save the workflow again, it should be ok.

Saturday, December 15, 2012

Un-hide Folders in Windows via command


Once, a friend of mine had brought to me her USB flash drive infected with viruses that made most of her files disappear.. actually she haven't lost her files.. the virus had hidden all the folders in her drive, even after cleaning all the viruses with ESET Smart Security, the folders are still hidden.. 

Saturday, September 10, 2011

Tricks for Coding Pages to be Printing Friendly

One day @work, I was asked to make a printOut page version to allow the user to print a Report out of a page filled with lots of data, placed in different sections and tables.. I had to find a way to make the page as much user friendly with its search area and other query controls and to look as a properly designed and arranged report.  
Designing suchpage is actually a joyless task with lots of obstacles, but I must fight to keep my face straight with my managers..

How to Fix Lightbox Effect for Blogger

Last week, I noticed a link to the updated Blogger interface at the top of the page. To tell the truth, I didnt like it at first.. and I couldn't work with it.. I changed it back to the old interface many times.. till I finally got used to it... I also used the new template designer to update My blogger design: colors, Background, Font..  etc.

Anyways, I then started to make a new post when I suddenly noticed... LIGHTBOX EFFECT IS NOT WORKING :O

yah it's normal as I have updated theblogger template , so I must re-followed the steps to add the LightBox effect from my previous post "How to add Lightbox effect to Blogger".. but it was still not working :(

here in this post I will show you How I fixed it using the same Script and CSS files in my previous post.
LightBox Effect



Thursday, August 18, 2011

How to add Lightbox effect to Blogger


in this post i will show you the steps to add The Lightbox effect to Blogger (blogspot) blogs that is useful when your post has many images or images with large sizes, it helps people to save time browsing images in an impressive way.

LightBox effect