Updating a Textbox control in MasterPage from BasePage

Last post 11-25-2009 11:24 AM by Ahmed Moosa. 4 replies.

Sort Posts:

  • Updating a Textbox control in MasterPage from BasePage

    11-25-2009, 6:52 AM
    • Member
      15 point Member
    • alaa_tm
    • Member since 07-14-2004, 9:21 AM
    • Posts 6

    I have a MasterPage.master file and a BasePage class. The BasePage has a method called SetStatus where it uses the Master property's FindControl to get a reference to a TextBox and set the Text property. This method is called from child pages to update the status of some operation.

    The problem I am having is that the text box is not being updated. It seems like the Text property keeps getting reset back to empty string somehow. I have stepped through the code and everything seems to be going well.

    Any tips?

    Thanks

  • Re: Updating a Textbox control in MasterPage from BasePage

    11-25-2009, 9:33 AM
    Answer

    Hi i am alkesh kashyap here the code which is working with myself properly If your problem solved from it then marked as

    answer.

    TextBox txtTemp = (TextBox)this.Master.FindControl("txtBoxID");
            //This line is use to check the text
            string text = txtTemp.Text;
            txtTemp.Text = "This is New Text";

  • Re: Updating a Textbox control in MasterPage from BasePage

    11-25-2009, 9:37 AM
    Answer
    • Contributor
      2,738 point Contributor
    • thirumaran007
    • Member since 03-14-2007, 5:39 AM
    • India
    • Posts 568

    Hi Friend,

    The following code shows that , how to access master page controls and update them

    void Page_Load ( ) {
       // gets a reference to a TextBox control inside a ContentPlaceHolder
       TextBox mpTextBox;
       ContentPlaceHolder mpContentPlaceHolder = 
          ( ContentPlaceHolder ) Master.FindControl ( "ContentPlaceHolder1" );
       if ( mpContentPlaceHolder != null ) {
          mpTextBox = ( TextBox ) mpContentPlaceHolder.FindControl ( "TextBox1" );
          if ( mpTextBox != null ) {
             mpTextBox.Text = "TextBox found!";
          }
       }
       // gets a reference to a Label control that not in a ContentPlaceHolder
       Label mpLabel = ( Label ) Master.FindControl ( "masterPageLabel" );
       if ( mpLabel != null ) {
          Label1.Text = "Master page label = " + mpLabel.Text;
       }
    }




    Kindly revert me is there any clarification.

    With Friendly,
    Thirumaran

    Please remember to click "Mark as Answer" on this post if it helped you
  • Re: Updating a Textbox control in MasterPage from BasePage

    11-25-2009, 11:14 AM
    • Member
      15 point Member
    • alaa_tm
    • Member since 07-14-2004, 9:21 AM
    • Posts 6

    Thanks for the responses. However, my problem is not how to update the text box which I have it coded just fine. The problem is the text goes back to empty.

    Here is what I have so far:

    protected void SetStatus(string message) {
      Label lblStatus = (Label)Master.FindControl("lblStatus");
    
      lblStatus.Visible = true;
      lblStatus.Text = message;
    }
    

    The above method is in my base page class and it's being called from child pages. The code executes but for some reason the text is not getting updated. I am not sure exactly why as I have seen sometimes the Page_Load is getting fired twice when I post back and maybe this is causing the problem. I tried tracing but it wasn't very helpfull. Is there any kind of tool that let's you see when a certain control is being updated and by who?

    Thanks
     

  • Re: Updating a Textbox control in MasterPage from BasePage

    11-25-2009, 11:24 AM
    Answer
    • Member
      572 point Member
    • Ahmed Moosa
    • Member since 10-01-2009, 6:58 PM
    • Egypt
    • Posts 102

    alaa_tm:

    1. protected void SetStatus(string message) {  
    2.   Label lblStatus = ((Label)Master.FindControl("lblStatus"));  
    3.   
    4.   lblStatus.Visible = true;  
    5.   lblStatus.Text = message;  
    6. }  


    Hi Alaa

    Try it Like this

    ((Label)(Page.Master.FindControl("lblstatus"))).Text = Message;


    Hope this Help

    My Blog

    الحمد لله رب العالمين
Page 1 of 1 (5 items)