26 June 2012

Invoke custom method in Crystal report default print button click event


We have requirement is when i click on default crystal report print button, it will open printoptions window, when i select/click the Ok button, on button click event i want to insert data into the database(Call custom method). we tried so many way at last we got the solution for that as shown in bellow link
My Link: http://dotnetgoutham.blogspot.in/2012/06/get-control-name-in-pageload-event.html
Step 1:
 public string GetPostBackControlId(Page page)
    {
        if (!page.IsPostBack)
            return string.Empty;


        Control control = null;
        // first we will check the "__EVENTTARGET" because if post back made by the controls
        // which used "_doPostBack" function also available in Request.Form collection.
        string controlName = page.Request.Params["__EVENTTARGET"];
        if (!String.IsNullOrEmpty(controlName))
        {
            control = page.FindControl(controlName);
        }
        else
        {
            // if __EVENTTARGET is null, the control is a button type and we need to
            // iterate over the form collection to find it


            // ReSharper disable TooWideLocalVariableScope
            string controlId;
            Control foundControl;
            // ReSharper restore TooWideLocalVariableScope


            foreach (string ctl in page.Request.Form)
            {
                // handle ImageButton they having an additional "quasi-property" 
                // in their Id which identifies mouse x and y coordinates
                if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                {
                    controlId = ctl.Substring(0, ctl.Length - 2);
                    foundControl = page.FindControl(controlId);
                }
                else
                {
                    foundControl = page.FindControl(ctl);
                }


                if (!(foundControl is Button || foundControl is ImageButton)) continue;


                control = foundControl;
                //hdPrintValue.Value = control.ID;
                Session["HiddenValue"] = ctl.Substring(ctl.Length - 7, 5);
                break;
            }
        }
        return control == null ? String.Empty : control.ID;
    }
Step 2:
        string str = GetPostBackControlId(this.Page);
        if (Session["HiddenValue"] != null)
        {
            if (Session["HiddenValue"].ToString() == "ctl01" && str == "CrystalReportViewer1")
            {
                //ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ShowMessage", "alert('Updated Successfully');", true);
                try
                {
                    //Call custom Method() here....
                }
                catch (Exception ex)
                {
                   
                }
            }
        }

Get control name in Page_Load event which make the post back



Need to know which specific control posted back to your page? (Original article at http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx)
/// <summary>
/// Gets the ID of the post back control.
/// </summary>
/// <param name = "page">The page.</param>
/// <returns></returns>
public static string GetPostBackControlId(this Page page)
{
    if (!page.IsPostBack)
        return string.Empty;
    Control control = null;
    // first we will check the "__EVENTTARGET" because if post back made by the controls
    // which used "_doPostBack" function also available in Request.Form collection.
    string controlName = page.Request.Params["__EVENTTARGET"];
    if (!String.IsNullOrEmpty(controlName))
    {
        control = page.FindControl(controlName);
    }
    else
    {
        // if __EVENTTARGET is null, the control is a button type and we need to
        // iterate over the form collection to find it
        // ReSharper disable TooWideLocalVariableScope
        string controlId;
        Control foundControl;
        // ReSharper restore TooWideLocalVariableScope
        foreach (string ctl in page.Request.Form)
        {
            // handle ImageButton they having an additional "quasi-property" 
            // in their Id which identifies mouse x and y coordinates
            if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
            {
                controlId = ctl.Substring(0, ctl.Length - 2);
                foundControl = page.FindControl(controlId);
            }
            else
            {
                foundControl = page.FindControl(ctl);
            }


            if (!(foundControl is Button || foundControl is ImageButton)) continue;


            control = foundControl;
            break;
        }
    }
    return control == null ? String.Empty : control.ID;
}


If you are using master pages, simply add this to the master.cs file and where you need to call the method use the following code:
MasterPage masterPage = (MasterPage)Page.Master; // where MasterPage is the class name
 Control rep = masterPage.getPostBackControlName();
 if (rep.Parent.Parent.ID == "rpRegion")// only if postback is the regions control
 GetSelectedRegion();

08 June 2012

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). 
Use <%# this.txtCompany.ClientID %> to replace <%= this.txtCompany.ClientID %> to fix the problem.

Once those changes are done then run the application if your not getting the values of controls(getting balnk ("") value instead of Client id) then follow the below step.

Re-place (<%# ... %>) with (<%= ...%>) and put your script from "head" tag to "body" tag.


Ref Link:- 
http://www.west-wind.com/weblog/posts/2006/May/27/The-Controls-collection-cannot-be-modified-because-the-control-contains-code-blocks-ie-
http://www.dotnet-tips.com/2006/09/controls-collection-cannot-be-modified.html