Problem with textbox

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Hey

asp.net 2.0

I have a UserControl which contain many TextBoxes. When I from the webpage
(the webpage containing this UserControl) click on a button to start save
the text in the textBoxes, then nothing is saved....

I've debugged and discovered that the TextBox.Text = "", but I typed in a
value in that TextBox...

Here is some of the source of my UserControl:
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell Width="130px" CssClass="fieldname">First
name:</asp:TableCell>
<asp:TableCell Width="300px">
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>

This is the method I use to save the values in TextyBoxes:
It's in this method the error occur... txtFirstName.Text = "", but it should
for example be "helloworld"
public void SaveInfo(ProfileCommon profile)
{
profile.LastName = txtLastName.Text;
profile.FirstName = txtFirstName.Text;
profile.Website = txtWebsite.Text;
profile.Address.Street = txtStreet.Text;
profile.BirthDate = Convert.ToDateTime(txtBirthDate.Text);
}

The event handler of the button in the web page:
protected void btnSave_Click(object sender, EventArgs e)
{
this.Settings1.SaveInfo(Profile);
}

Any suggestions?

Jeff
 
Are you exposing the "Profile" object ........ as a Property of the
UserControl?

code of hte usercontrol

private Profile m_profileInfo;

public Profile TheProfile()
get
{
this.PopulateProfile();
return this.m_profileInfo;
}

private void PopulateProfile()
{
m_profileInfo = new Profile();
m_profileInfo.FirstName = this.txtFirstName.Text;
m_profileInfo.LastName = this.txtLastName.Text;
}


.....................

Now, there is a web page using the userControl.


button1_click()

Profile profOne /* using antoher variable name to avoid ambiguity*/ =
myUserControl.Profile;
Response.Write (profOne.Firstname)



...............


Always remember that a UserControl is nothing but an object at its core. And
you exposed properties/methods on it like any other object.
 
No, as far as I know I'm not exposing the "Profile" object as a property in
the UserControl. The "Profile" object is in the webpage, and I'm just
sending the "Profile" object to the UserControl.

The error is because txtFirstName.Text has the value "", but it should got a
value.. I'm not sure this has something to do with the "Propfile" object...
but I'm a newbie, and I've been wrong before.... I have the same problem if
don't pass the "profile" object from the webpage to the UserControl.:

string fname = txtFirstName.Text.. txtFirstName.Text = ""... but I'm sure I
typed "helloworld"

Any suggestions?

Jeff
 
More info:

These are 2 event handlers from the web page containing the UserControl:
public partial class Network_Profile_Settings : AH.MyNetwork.UI.BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
this.Settings1.SetInfo(Profile);
}

protected void btnSave_Click(object sender, EventArgs e)
{
this.Settings1.SaveInfo();
}
}

These are the 2 methods in the UserControl (they are referenced from the web
page, see above):
public void SetInfo(ProfileCommon profile)
{
txtLastName.Text = profile.LastName;
txtWebsite.Text = profile.Website;
txtStreet.Text = profile.Address.Street;
txtBirthDate.Text = profile.BirthDate.ToString();
}

public void SaveInfo()
{
string fname = txtFirstName.Text;
}

This is the markup of the button I use to save the text entered
<asp:Button runat="server" Text="Save" OnClick="btnSave_Click"></asp:Button>

But the strange is that when clicking on this button, the SetInfo method is
executed before SaveInfo....

Why are SetInfo executed before SaveInfo when I click the button?
 
Solved, I've forgotten to test if it's postback

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostback) {
this.Settings1.SetInfo(Profile);
}
}
 
Back
Top