Search This Blog

Wednesday, April 6, 2011

AutoEventWireUp

AutoEventWireUp attribute in asp.net is used to call the Page_Load and Page_InIt methods automatically
by setting AutoEventWireUp=true;
By default AutoEventWireup set to true in machine.config and hence at the page level it is set to false
If you set AutoEventWireUp=true at page level then Page_Load and Page_InIt will be executed twice.

Sign up for .Net Training Bangalore

User Control assignment ( Change Button color from User Control Text Box Value)

1.Create User Control with name Enquiry . Drag and drop Text box control on the Enquiry.ascx
    set text box id="tbColorName".
2.Create a property in the Enquiry.ascx.cs as shown below.
   protected void Page_Load(object sender, EventArgs e)
    {

    }
    private string _color;

    public string Color
    {
        get { return _color; }
        set { _color = value; }
    }
3.Create a Page with Name UserControlSample.aspx and do below given activities

  • Drag and drop a button on the aspx page and set id="btnSubmit" Text="Submit"
  • Drag and drop the User Control on to the surface of the aspx page. Set UserControl id="Enquiry1".
4.Write the below code in the UserControlSample.aspx.cs
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        foreach (var c in Enquiry1.Controls)
        {
            Type t=c.GetType();
            if (t.Name=="TextBox")
            {
                Enquiry1.Color = ((TextBox)c).Text;
            }
        }
        btnSubmit.BackColor = System.Drawing.Color.FromName(Enquiry1.Color);
    }
5.Execute the application by pressing F5.