is there a way better way? i'm sure there is but need help

Last post 05-17-2006 2:46 AM by sophia. 7 replies.

Sort Posts:

  • is there a way better way? i'm sure there is but need help

    05-10-2006, 10:45 PM
    • Contributor
      2,317 point Contributor
    • nisarkhan
    • Member since 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,361

    Angry [:@]i have lots of control(like textbox, labels, listbox, dropdownlist etc.) and everytime i access the control i have use this set of 3 lines code:

    ContentPlaceHolder content;
    content = (
    ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");
    RadPanelbar myRadPanelbar = (RadPanelbar)content.FindControl("RadPanelbar");

    is there a way i can put that somewhere and access from any where in the project ?

    for an example, if i have to access my dropdownlist, just a dropdownlist i have to do this:

    ContentPlaceHolder content;
    content = (
    ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");
    RadPanelbar myRadPanelbar = (RadPanelbar)content.FindControl("RadPanelbar");
    DropDownList
    ddlus = (DropDownList)myRadPanelbar.FindControl("ddlus"); ///<<<<

    what is the best way to deal this?

    thanks in advance

     

     

    Its all about coding!
    --------------------------
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: is there a way better way? i'm sure there is but need help

    05-11-2006, 7:45 AM
    • Star
      8,834 point Star
    • MorningZ
    • Member since 07-22-2002, 2:39 PM
    • Fort Lauderdale, FL
    • Posts 1,815

    I dont know why you wouldn't just make a page global reference to "RadPanelBar" and then just one single time at the time of Page_Load set it

    Then anywhere else in your code you could say myRadPanelbar.FindControl("ddlus") without having to re-reference the panel

    "If you make it idiot proof, they'll build a better idiot"
  • Re: is there a way better way? i'm sure there is but need help

    05-11-2006, 9:16 AM
    • Contributor
      2,317 point Contributor
    • nisarkhan
    • Member since 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,361

    can you please give me some example of my code?

     

    thanks

    Its all about coding!
    --------------------------
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: is there a way better way? i'm sure there is but need help

    05-11-2006, 9:51 AM
    • Star
      8,834 point Star
    • MorningZ
    • Member since 07-22-2002, 2:39 PM
    • Fort Lauderdale, FL
    • Posts 1,815

    Sure

    public partial class default1 : System.Web.UI.Page
    {
        System.Web.UI.WebControls.Panel radPanel;

        protected void Page_Load(object sender, EventArgs e)
        {
            ContentPlaceHolder cont = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");
            radPanel = (Panel)cont.FindControl("RadPanelbar");
        }

        protected void SomeOtherFunction(object sender, EventArgs e)
        {
            TextBox txt = radPanel.FindControl("TextBox1");
        }
        protected void AnotherFunctionDoingSomething(object sender, EventArgs e)
        {
            DropDownList drp = radPanel.FindControl("ddlus");
        }
    }

    "If you make it idiot proof, they'll build a better idiot"
  • Re: is there a way better way? i'm sure there is but need help

    05-11-2006, 1:18 PM
    • Contributor
      2,317 point Contributor
    • nisarkhan
    • Member since 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,361
    MorningZ:

    Sure

    public partial class default1 : System.Web.UI.Page
    {
        System.Web.UI.WebControls.Panel radPanel;

        protected void Page_Load(object sender, EventArgs e)
        {
            ContentPlaceHolder cont = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");
            radPanel = (Panel)cont.FindControl("RadPanelbar");
        }

        protected void SomeOtherFunction(object sender, EventArgs e)
        {
            TextBox txt = radPanel.FindControl("TextBox1");
        }
        protected void AnotherFunctionDoingSomething(object sender, EventArgs e)
        {
            DropDownList drp = radPanel.FindControl("ddlus");
        }
    }

    i was doubt about it and i got the same error:

    it complaing of :

    protected void textName(object sender, EventArgs e)

    {

    TextBox textName = myRadPanelbar.FindControl("txtVisitName");

    }

    on page_load i have:

     

    Error 1 The name 'myRadPanelbar' does not exist in the current context 

     

     

    protected void Page_Load(object sender, EventArgs e)
    {

    ContentPlaceHolder content;
    content = (
    ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");
    RadPanelbar myRadPanelbar = (RadPanelbar)content.FindControl("RadPanelbar");

    }

    Its all about coding!
    --------------------------
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: is there a way better way? i'm sure there is but need help

    05-11-2006, 1:37 PM
    • Star
      8,834 point Star
    • MorningZ
    • Member since 07-22-2002, 2:39 PM
    • Fort Lauderdale, FL
    • Posts 1,815

    Notice in the code i posted, which you were not too close to following, the declaration for "myRadPanelbar" is outside the page load event, this makes that variable global, meaning that anywhere in the code page you can access it, manipulate it, and use it...

    Your code you posted is declaring it inside the page_load event, making it a local variable to that code, meaning you can only use it inside that block

    "If you make it idiot proof, they'll build a better idiot"
  • Re: is there a way better way? i'm sure there is but need help

    05-11-2006, 1:57 PM
    • Contributor
      2,317 point Contributor
    • nisarkhan
    • Member since 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,361

    i realized after i post, sorry for that: but i have something new come-up

    but here is i'm getting something new:

        public TextBox myName
        {
            get
            {
                return (TextBox)myRadPanelbar.FindControl("txtName");
            }
        }

    Object reference not set to an instance of an object.

    if (PreviousPage.Name != null)
    {
    if (!string.IsNullOrEmpty(PreviousPage.Name))
    {
    myName.Text = PreviousPage.Name;  
    }
    }

    i'm getting error on:

    myName.Text  (<<<<<<error is: Object reference not set to an instance of an object.)

     

    Its all about coding!
    --------------------------
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: is there a way better way? i'm sure there is but need help

    05-17-2006, 2:46 AM
    • Contributor
      2,920 point Contributor
    • sophia
    • Member since 04-05-2006, 4:32 AM
    • Posts 584

    A good article for you.

     

Page 1 of 1 (8 items)