Wednesday 18 July 2012

Finding ASP.Net control in User Control when using Master Page

Finding a control from User Control while using Master page can cause some confusion.

The code when NOT using Master page:

ucl_slareport WebUserControl1 = (ucl_slareport)FindControl(reportview);
Repeater ulRepeater1 = (Repeater)WebUserControl1.FindControl("Repeater1");
ulRepeater1.DataSource = reader;
ulRepeater1.DataBind();

Here ucl_slareport is my user control. The control that we are searching is a Repeater control. "reportview" is a new name of the control that we passing from a different function.

When we have the Master Page and trying to find the Repeater control, we first do a FindControl of the ContentPlaceHolder - "MainContent", and then we do a find control of the repeater.

ContentPlaceHolder mpContentPlaceHolder = (ContentPlaceHolder)this.Master.FindControl("MainContent");
ucl_slareport WebUserControl1 = (ucl_slareport)mpContentPlaceHolder.FindControl(reportview);
Repeater ulRepeater1 = (Repeater)WebUserControl1.FindControl("Repeater1");
ulRepeater1.DataSource = reader;
ulRepeater1.DataBind();

So that solves the problem of finding the control from the user control.

Calling an executable file from Website and passing arguments


I had a requirement where I needed to call a exe file from my web application. Also I needed to pass arguments to the exe.

Am calling the exe from my website through a button click. The below code explains:


Webpage.aspx

protected void Button1_Click(object sender, EventArgs e)
    {
        ProcessStartInfo prodRun = new ProcessStartInfo(ConfigurationManager.AppSettings["exepath"] + "ConClientServer.exe", ConfigurationManager.AppSettings["paths"]);       
        Process exProducts = new Process();
        prodRun.WorkingDirectory = @"C:\my work\EduProj\ConClientServer\bin\Debug\";
        prodRun.Arguments = "C:\\Development\\Temp\\NewFilesTemp\\cab.txt";
        exProducts = Process.Start(prodRun);
    }

The exe file name is mentioned in ProcessStartInfo - "ConClientServer.exe".
WorkingDirectory is where my exe resides.
The 4th line, I passed a single argument. We can however pass multiple argument, separated by comma.

 The exe file is called from the location mentioned in the working directory, it accepts the argument from the website as below:

 ConClientServer.exe
    class Program
    {
        static void Main(string[] args)
        {
            Client cl = new Client();
            if (args.Length > 0)
            {
                string path = args[0];               
                cl.FetchFileFromServer(path);
            }
        }
    }

    public class Client
    {       
        public void FetchFileFromServer(string storepath)
        {
              string downloadpath  = storepath;
         }
     }