Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie question - how to get text field value when embedded

Status
Not open for further replies.

Technokrat

Programmer
Jul 20, 2001
92
US
I'm new to web development, I'm using aspx, c#

I'm trying to use this.txtHomePhone.text to get the value. This is working fine on simple forms, but it doesn't seem to work when the text field is in a container object, such as a table. It's like it can't resolve the "txtHomePhone" reference.

Is there something special that needs to be done to reference the text field when it is embedded in another control?
 
Yes, you have to use the findcontro() method of the object to get a reference to it.

something like:
tablename.findcontrol("name of your textbox")
 
Thank you. However, I am still getting a null result. Does the fact that this web form has a master page make a difference?

if it helps, here is what I am working with:
Code:
        <table style="width:100%;" id="tblFormData">
            <tr>
                <td class="style12" colspan="3">
                    <strong>Profile:</strong></td>
            </tr>
            <tr>
                <td class="style22">
                    <span class="style18">Home Phone:</span><span class="style6"><br 
                        class="style18" />
                    <span class="style18">
        <asp:TextBox ID="txtHomePhone" runat="server" Width="200px"></asp:TextBox>
                    </span>
                    </span>
                </td>
</tr>

And the code to get to it:
Code:
Table tbl = (Table)this.FindControl("tblFormData");
TextBox t = (TextBox)tbl.FindControl("txtHomePhone");
 
I can take the ID out of the table, I'm not doing anything dynamic. However, that doesn't get me any closer to resolving the findcontrol of the textbox. I still end up with a NULL reference.
 
The fact that it is on a Master page does make a difference.
Are you just trying to set the text value of a textbox on a Master page from a Content page?

If so, then just create a public property on your master page that sets the value of the textbox. Then you can access the property from any content page.
 
The table is NOT in the master page. I have it in the form and I'm using it for formatting. The textbox is in one of the table cells. I'm loading and populating the field from the database, but when I click the submit button I am unable to retrieve the updated fields. When I reference the textboxes, they have the original values I loaded in them, and not the updates the user entered.
 
That's an entirely different issue then, and at a guess I'd say your probably noy checking for a postback on the load of your page when you are populating the values hence why they get overwritten each time.

If you supply the code for your page then we can give a more exact answer.

Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
thanks Mark, here is the code as you requested...

Extracting the values from the fields in btnSubmit_Click is where I need guidance.

Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class ProfileEditor : System.Web.UI.Page
    {
        #region Private Fields

        private string _personId;
        private string _homephoneId;
        private string _cellphoneId;
        private string _emergencyPhoneId;
        private string _emergencyPersonId;

        private GUID _guid = new GUID();

        #endregion

        #region Public Properties

        public string HomePhoneId
        {
            get { return _homephoneId; }
            set
            {
                if (_homephoneId != value)
                {
                    _homephoneId = value;                    
                }
                if (_homephoneId.Length == 0)
                {
                    _homephoneId = _guid.GetNewId(Session["ConnectionString"].ToString());
                }

            }
        }

        public string CellPhoneId
        {
            get { return _cellphoneId; }
            set
            {
                if (_cellphoneId != value)
                {
                    _cellphoneId = value;
                }
                if (_cellphoneId.Length == 0)
                {
                    _cellphoneId = _guid.GetNewId(Session["ConnectionString"].ToString());
                }

            }
        }

        public string EmergencyPhoneId
        {
            get { return _emergencyPhoneId; }
            set
            {
                if (_emergencyPhoneId != value)
                {
                    _emergencyPhoneId = value;
                }
                if (_emergencyPhoneId.Length == 0)
                {
                    _emergencyPhoneId = _guid.GetNewId(Session["ConnectionString"].ToString());
                }

            }
        }

        public string EmergencyPersonId
        {
            get { return _emergencyPersonId; }
            set
            {
                if (_emergencyPersonId != value)
                {
                    _emergencyPersonId = value;
                }
                if (_emergencyPersonId.Length == 0)
                {
                    _emergencyPersonId = _guid.GetNewId(Session["ConnectionString"].ToString());
                }

            }
        }


        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["EmployeeId"] == null)
                this.Response.Redirect("Default.aspx");
            else
            {
                
                try
                {
                    bool rv = false;

                    string connectionString = Session["ConnectionString"].ToString();
                    string employeeid = Session["EmployeeId"].ToString();

                    using (SqlConnection con = new SqlConnection(connectionString))
                    {
                        using (SqlCommand cmd = new SqlCommand())
                        {
                            con.Open();
                            cmd.Connection = con;
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "[SEC].[xspGetEmployeeInfoByID]";
                            cmd.Parameters.Add("@EmployeeId", SqlDbType.VarChar, 120).Value = employeeid;
                            
                            //EmployeeId = Convert.ToString(
                            SqlDataReader reader = cmd.ExecuteReader();
                            try
                            {
                                while (reader.Read())
                                {
                                    //0 - EmployeeId                           
                                    _personId = reader[1].ToString(); //1 - PersonId                             
                                    txtFirstName.Text = reader[2].ToString();//2 - FirstName            
                                    //3 - MiddleName           
                                    txtLastName.Text = reader[4].ToString();//4 - LastName             
                                    //5 - BusinessSiteId                       
                                    //6 - BusinessSiteName          
                                    txtAddress1.Text = reader[7].ToString();//7 - AddressLine1                                                                                                                                           
                                    txtAddress2.Text = reader[8].ToString();//8 - AddressLine2                                                                                                                                           
                                    txtCity.Text = reader[9].ToString();//9 - City                                               
                                    txtPostalCode.Text = reader[10].ToString();//10 - PostalCode                                         
                                    _homephoneId = reader[11].ToString(); //11 - HomePhoneId                          
                                    txtHomePhone.Text = reader[12].ToString();//12 - HomePhone                                          
                                    _cellphoneId = reader[13].ToString();//13 CellPhoneId                          
                                    txtCellPhone.Text = reader[14].ToString();//14CellPhone                                          
                                    EmergencyPhoneId = reader[15].ToString();//15EmergencyPhoneId                     
                                    txtEmPhone.Text = reader[16].ToString();//16EmergencyPhone                                     
                                    txtEmFirstName.Text = reader[17].ToString();//17FirstName            
                                    txtEmLastName.Text = reader[18].ToString();//18LastName
                                    EmergencyPersonId = reader[19].ToString();
                                }
                            }
                            finally
                            {
                                reader.Close();
                            }
                            
                            con.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Log errors
                    throw;
                }
            }


        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {            
            //TextBox a = (TextBox)Form.FindControl("txtHomePhone");
            //Table tbl = (Table)this.FindControl("tblFormData");
            //TextBox t = (TextBox)this.FindControl("txtHomePhone");
            SavePhone(_personId, HomePhoneId, 10000, txtHomePhone.Text);
            SavePhone(_personId, CellPhoneId, 11000, this.txtCellPhone.Text);
            SavePhone(EmergencyPersonId, EmergencyPhoneId, 10000, this.txtEmPhone.Text);
                
            //SaveAddress
        }

        protected void SavePhone(string PersonId, string PhoneId, int PhoneType, string PhoneNumber)
        {
            if (PhoneNumber.Length == 0)
                return;

            try
            {
                string connectionString = Session["ConnectionString"].ToString();
                string audituser = _guid.GetAuditUserId(connectionString, Session["EmployeeId"].ToString()); 

                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        con.Open();
                        cmd.Connection = con;
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "[CMN].[xspSavePersonPhone]";
                        cmd.Parameters.Add("@PersonId", SqlDbType.VarChar, 120).Value = PersonId;
                        cmd.Parameters.Add("@PhoneId", SqlDbType.VarChar, 120).Value = PhoneId;
                        cmd.Parameters.Add("@PhoneTypeConstantValue", SqlDbType.Int).Value = PhoneType;
                        cmd.Parameters.Add("@PhoneNumber", SqlDbType.VarChar, 50).Value = PhoneNumber;
                        cmd.Parameters.Add("@IsDeleted", SqlDbType.Bit).Value = 0;
                        cmd.Parameters.Add("@AuditUserId", SqlDbType.VarChar, 120).Value = audituser;

                        //EmployeeId = Convert.ToString(
                        cmd.ExecuteNonQuery();

                        con.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                // Log errors
                throw;
            }

        }

        protected void SaveAddress()
        {
        }
    }
}
 
Thanks for the help, that worked. However, now on the postback my values for PersonId and PhoneId are null. Do I need to add these as hidden fields for them to be available on postback, or is there another way to do this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top