By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,172 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,172 IT Pros & Developers. It's quick & easy.

Textbox in a User Control

P: n/a
I have created a user control, MyControl.ascx, that has a text box and
a button. The button updates a table in SQL with the text in the
textbox. Now, I include that control in a page Default.aspx. From
Default.aspx, on page load, I populate the textbox through the public
property TextBoxBody of MyControl.ascx.

When I load Default.aspx and change what's in the text box and click
the button to update the database, the update statement includes the
original textbox text (loaded through TextBoxBody on page load) rather
than the changes I have made.

What am I missing?

Thanks!
Jason

May 8 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
I am sure it is just a simple coding error (I would check the order that the
statements are called in), but I can't say much more than that without
seeing the code. If you post the code I will be happy to look at it and tell
you anything I can find. Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Jason" <ja*************@hotmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
I have created a user control, MyControl.ascx, that has a text box and
a button. The button updates a table in SQL with the text in the
textbox. Now, I include that control in a page Default.aspx. From
Default.aspx, on page load, I populate the textbox through the public
property TextBoxBody of MyControl.ascx.

When I load Default.aspx and change what's in the text box and click
the button to update the database, the update statement includes the
original textbox text (loaded through TextBoxBody on page load) rather
than the changes I have made.

What am I missing?

Thanks!
Jason

May 10 '06 #2

P: n/a
Nathan, thank you for the response...

There are four code segments here:
ctrlEditBlogEntry.ascx
ctrlEditBlogEntry.ascx.cs
edit_blog_entry.aspx
edit_blog_entry.aspx.cs

The second page, edit_blog_entry.aspx, calls the ctrlEditBlogEntry.ascx
user control. After it has included this control, it loads some other
user controls I have created. I have this same situation with another
user control, but it is working properly. The only difference is that
the working user control is included last in the aspx page, where
ctrlEditBlogEntry.ascx is loaded first.

ctrlEditBlogEntry.ascx:
<table border="0" width="901px" cellpadding="0" cellspacing="0">
<tr>
<td class="tableHeader">
<asp:Label ID="lblTitle" Text="Edit Blog Entry"
runat="server" />
</td>
</tr>
<tr>
<td class="tableBody">
<asp:TextBox ID="txtTitle" runat="server" Width="901px"
Font-Names="Segoe UI" Font-Size="16px" />
</td>
</tr>
<tr>
<td class="tableBody">
<asp:TextBox ID="txtBody" runat="server" Rows="8"
TextMode="MultiLine" Width="901px" Font-Names="Segoe UI"
Font-Size="16px" />
</td>
</tr>
<tr>
<td align="right">
<table border="0" cellpadding="10" cellspacing="0">
<tr>
<td>
<asp:ImageButton ID="imgSave"
ImageUrl="/blog/images/save.gif" AlternateText="Save changes"
runat="server" OnClick="imgSave_Click" /></td>
</tr>
</table>
</td>
</tr>
</table>

ctrlEditBlogEntry.ascx.cs:
public partial class controls_ctrlEditBlogEntry :
System.Web.UI.UserControl
{
private int giBlogEntryID, giBlogID;

protected void Page_Load(object sender, EventArgs e)
{

}

public string BlogTitle
{
set
{
txtTitle.Text = value;
}
}

public string BlogBody
{
set
{
txtBody.Text = value;
}
}

public int BlogEntryID
{
set
{
giBlogEntryID = value;
}
}

public int BlogID
{
set
{
giBlogID = value;
}
}

protected void imgSave_Click(object sender, ImageClickEventArgs e)
{
SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlCommand loCommand = new SqlCommand("UPDATE tbblog_entry SET
blog_entry_title = '" + txtTitle.Text.Trim() + "', blog_entry_body = '"
+ txtBody.Text.Trim() + "' WHERE blog_entry_id = " +
giBlogEntryID.ToString());
loCommand.Connection = loConnection;
loConnection.Open();
loCommand.ExecuteNonQuery();
loConnection.Close();
Response.Redirect("/blog/blogs/view_blog_entries.aspx?entry_id=" +
giBlogEntryID.ToString() + "&blog_id=" + giBlogID.ToString());
}
}

edit_blog_entry.aspx:
<%@ Register TagPrefix="phdr" TagName="ctrlPageHeader"
Src="/blog/controls/ctrlPageHeader.ascx" %>
<%@ Register TagPrefix="pftr" TagName="ctrlPageFooter"
Src="/blog/controls/ctrlPageFooter.ascx" %>
<%@ Register TagPrefix="ebe" TagName="ctrlEditBlogEntry"
Src="/blog/controls/ctrlEditBlogEntry.ascx" %>
<%@ Reference Control="/blog/controls/ctrlEditComment.ascx" %>
<html>
<body>
<form id="form1" runat="server">
<phdr:ctrlPageHeader id="ctrlPageHeader" runat="server" />
<ebe:ctrlEditBlogEntry id="ctrlEBE" runat="server" />
<asp:PlaceHolder ID="phComments" runat="server" />
<pftr:ctrlPageFooter id="ctrlPageFooter" runat="server" />
</form>
</body>
</html>

edit_blog_entry.aspx.cs:
public partial class blogs_edit_blog_entry : System.Web.UI.Page
{
private int giBlogID, giBlogEntryID;

protected void Page_Load(object sender, EventArgs e)
{
giBlogID =
System.Int32.Parse(Request.QueryString["blog_id"].ToString());
giBlogEntryID =
System.Int32.Parse(Request.QueryString["entry_id"].ToString());

SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlCommand loCommand = new SqlCommand("SELECT * FROM
tbblog_entry WHERE blog_entry_id = " + giBlogEntryID);
loCommand.Connection = loConnection;
loConnection.Open();
SqlDataReader loReader = loCommand.ExecuteReader();
loReader.Read();
ctrlEBE.BlogEntryID = giBlogEntryID;
ctrlEBE.BlogID = giBlogID;
ctrlEBE.BlogBody = loReader["blog_entry_body"].ToString();
ctrlEBE.BlogTitle = loReader["blog_entry_title"].ToString();
loReader.Close();
loConnection.Close();
LoadComments();
}

private void LoadComments()
{
SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlCommand loCommand = new SqlCommand("SELECT * FROM
vwblog_comment_user WHERE blog_entry_id = " + giBlogEntryID + " AND
blog_comment_deleted = 0 ORDER BY blog_comment_date");
loCommand.Connection = loConnection;
loConnection.Open();
SqlDataReader loReader = loCommand.ExecuteReader();

while (loReader.Read())
{
controls_ctrlEditComment loComment =
LoadControl("/blog/controls/ctrlEditComment.ascx") as
controls_ctrlEditComment;
loComment.CommentBody =
loReader["blog_comment_body"].ToString().Trim();
loComment.CommentTitle =
loReader["blog_comment_title"].ToString().Trim();
loComment.CommentDate =
System.DateTime.Parse(loReader["blog_comment_date"].ToString());
loComment.Author =
loReader["sec_user_username"].ToString().Trim();
loComment.CommentID =
System.Int32.Parse(loReader["blog_comment_id"].ToString());
phComments.Controls.Add(loComment);
}

loReader.Close();
loConnection.Close();
}
}

May 10 '06 #3

P: n/a
You mention that the only difference is the order the controls are loaded
in. Did you try changing the order they are loaded in? If you remember from
my first response I suggested checking the order the stuff is done in, it
sounds like that could be it (you could also try reversing them in the
working page and see if that causes the same problem there). I am a VB.NET
person, so I have never tried actually using C#, but does Visual Studio have
a step-by-step debugger for that? If it does, I would try using it to see
what order it does everything in, knowing that info might help you figure
out what needs to be changed. Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Jason" <ja*************@hotmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Nathan, thank you for the response...

There are four code segments here:
ctrlEditBlogEntry.ascx
ctrlEditBlogEntry.ascx.cs
edit_blog_entry.aspx
edit_blog_entry.aspx.cs

The second page, edit_blog_entry.aspx, calls the ctrlEditBlogEntry.ascx
user control. After it has included this control, it loads some other
user controls I have created. I have this same situation with another
user control, but it is working properly. The only difference is that
the working user control is included last in the aspx page, where
ctrlEditBlogEntry.ascx is loaded first.

ctrlEditBlogEntry.ascx:
<table border="0" width="901px" cellpadding="0" cellspacing="0">
<tr>
<td class="tableHeader">
<asp:Label ID="lblTitle" Text="Edit Blog Entry"
runat="server" />
</td>
</tr>
<tr>
<td class="tableBody">
<asp:TextBox ID="txtTitle" runat="server" Width="901px"
Font-Names="Segoe UI" Font-Size="16px" />
</td>
</tr>
<tr>
<td class="tableBody">
<asp:TextBox ID="txtBody" runat="server" Rows="8"
TextMode="MultiLine" Width="901px" Font-Names="Segoe UI"
Font-Size="16px" />
</td>
</tr>
<tr>
<td align="right">
<table border="0" cellpadding="10" cellspacing="0">
<tr>
<td>
<asp:ImageButton ID="imgSave"
ImageUrl="/blog/images/save.gif" AlternateText="Save changes"
runat="server" OnClick="imgSave_Click" /></td>
</tr>
</table>
</td>
</tr>
</table>

ctrlEditBlogEntry.ascx.cs:
public partial class controls_ctrlEditBlogEntry :
System.Web.UI.UserControl
{
private int giBlogEntryID, giBlogID;

protected void Page_Load(object sender, EventArgs e)
{

}

public string BlogTitle
{
set
{
txtTitle.Text = value;
}
}

public string BlogBody
{
set
{
txtBody.Text = value;
}
}

public int BlogEntryID
{
set
{
giBlogEntryID = value;
}
}

public int BlogID
{
set
{
giBlogID = value;
}
}

protected void imgSave_Click(object sender, ImageClickEventArgs e)
{
SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlCommand loCommand = new SqlCommand("UPDATE tbblog_entry SET
blog_entry_title = '" + txtTitle.Text.Trim() + "', blog_entry_body = '"
+ txtBody.Text.Trim() + "' WHERE blog_entry_id = " +
giBlogEntryID.ToString());
loCommand.Connection = loConnection;
loConnection.Open();
loCommand.ExecuteNonQuery();
loConnection.Close();
Response.Redirect("/blog/blogs/view_blog_entries.aspx?entry_id=" +
giBlogEntryID.ToString() + "&blog_id=" + giBlogID.ToString());
}
}

edit_blog_entry.aspx:
<%@ Register TagPrefix="phdr" TagName="ctrlPageHeader"
Src="/blog/controls/ctrlPageHeader.ascx" %>
<%@ Register TagPrefix="pftr" TagName="ctrlPageFooter"
Src="/blog/controls/ctrlPageFooter.ascx" %>
<%@ Register TagPrefix="ebe" TagName="ctrlEditBlogEntry"
Src="/blog/controls/ctrlEditBlogEntry.ascx" %>
<%@ Reference Control="/blog/controls/ctrlEditComment.ascx" %>
<html>
<body>
<form id="form1" runat="server">
<phdr:ctrlPageHeader id="ctrlPageHeader" runat="server" />
<ebe:ctrlEditBlogEntry id="ctrlEBE" runat="server" />
<asp:PlaceHolder ID="phComments" runat="server" />
<pftr:ctrlPageFooter id="ctrlPageFooter" runat="server" />
</form>
</body>
</html>

edit_blog_entry.aspx.cs:
public partial class blogs_edit_blog_entry : System.Web.UI.Page
{
private int giBlogID, giBlogEntryID;

protected void Page_Load(object sender, EventArgs e)
{
giBlogID =
System.Int32.Parse(Request.QueryString["blog_id"].ToString());
giBlogEntryID =
System.Int32.Parse(Request.QueryString["entry_id"].ToString());

SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlCommand loCommand = new SqlCommand("SELECT * FROM
tbblog_entry WHERE blog_entry_id = " + giBlogEntryID);
loCommand.Connection = loConnection;
loConnection.Open();
SqlDataReader loReader = loCommand.ExecuteReader();
loReader.Read();
ctrlEBE.BlogEntryID = giBlogEntryID;
ctrlEBE.BlogID = giBlogID;
ctrlEBE.BlogBody = loReader["blog_entry_body"].ToString();
ctrlEBE.BlogTitle = loReader["blog_entry_title"].ToString();
loReader.Close();
loConnection.Close();
LoadComments();
}

private void LoadComments()
{
SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlCommand loCommand = new SqlCommand("SELECT * FROM
vwblog_comment_user WHERE blog_entry_id = " + giBlogEntryID + " AND
blog_comment_deleted = 0 ORDER BY blog_comment_date");
loCommand.Connection = loConnection;
loConnection.Open();
SqlDataReader loReader = loCommand.ExecuteReader();

while (loReader.Read())
{
controls_ctrlEditComment loComment =
LoadControl("/blog/controls/ctrlEditComment.ascx") as
controls_ctrlEditComment;
loComment.CommentBody =
loReader["blog_comment_body"].ToString().Trim();
loComment.CommentTitle =
loReader["blog_comment_title"].ToString().Trim();
loComment.CommentDate =
System.DateTime.Parse(loReader["blog_comment_date"].ToString());
loComment.Author =
loReader["sec_user_username"].ToString().Trim();
loComment.CommentID =
System.Int32.Parse(loReader["blog_comment_id"].ToString());
phComments.Controls.Add(loComment);
}

loReader.Close();
loConnection.Close();
}
}

May 10 '06 #4

P: n/a
No, I haven't tried moving the control to the last thing loaded.
However, I'm starting to have the same trouble, but with just a simple
ASPX page.

I have an ASPX page with text boxes that are populated with data on
page_load. When you click the "save" icon, it takes the contents of
the text boxes and updates the database - only it's grabbing the
original values, not the text in the text boxes at the time the save
icon is clicked! Is there some new property of the asp:textbox in 2.0
that I am missing?

Here is some code:

public partial class control_panel_security_edit_user :
System.Web.UI.Page
{
private int giUserID, giSecurity;
private bool gbHidden, gbDeleted;

protected void Page_Load(object sender, EventArgs e)
{
giUserID =
System.Int32.Parse(Request.QueryString["user_id"].ToString());

SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlCommand loCommand = new SqlCommand("SELECT * FROM tbsec_user
WHERE sec_user_id = " + giUserID.ToString(), loConnection);
loConnection.Open();
SqlDataReader loReader = loCommand.ExecuteReader();
loReader.Read();
lblUserID.Text = loReader["sec_user_id"].ToString();
txtFirstName.Text =
loReader["sec_user_firstname"].ToString().Trim();
txtLastName.Text =
loReader["sec_user_lastname"].ToString().Trim();
txtEmail.Text = loReader["sec_user_email"].ToString().Trim();
txtUsername.Text =
loReader["sec_user_username"].ToString().Trim();
txtPassword.Text =
loReader["sec_user_password"].ToString().Trim();
giSecurity =
System.Int32.Parse(loReader["sec_user_security"].ToString());
gbHidden =
System.Boolean.Parse(loReader["sec_user_hidden"].ToString());
gbDeleted =
System.Boolean.Parse(loReader["sec_user_deleted"].ToString());
loReader.Close();
loConnection.Close();
}

protected void imgSave_Click(object sender, ImageClickEventArgs e)
{
SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlCommand loCommand = new SqlCommand("prsec_user_u");
try
{
loCommand.CommandType = CommandType.StoredProcedure;
loCommand.Connection = loConnection;

SqlParameter sec_user_id =
loCommand.Parameters.Add("@sec_user_id", SqlDbType.Int);
sec_user_id.Value = giUserID;

SqlParameter sec_user_username =
loCommand.Parameters.Add("@sec_user_username", SqlDbType.VarChar, 20);
sec_user_username.Value = txtUsername.Text.Trim();

SqlParameter sec_user_password =
loCommand.Parameters.Add("@sec_user_password", SqlDbType.VarChar, 20);
sec_user_password.Value = txtPassword.Text.Trim();

SqlParameter sec_user_email =
loCommand.Parameters.Add("@sec_user_email", SqlDbType.VarChar, 75);
sec_user_email.Value = txtEmail.Text.Trim();

SqlParameter sec_user_firstname =
loCommand.Parameters.Add("@sec_user_firstname", SqlDbType.VarChar, 50);
sec_user_firstname.Value = txtFirstName.Text.Trim();

SqlParameter sec_user_lastname =
loCommand.Parameters.Add("@sec_user_lastname", SqlDbType.VarChar, 50);
sec_user_lastname.Value = txtLastName.Text.Trim();

SqlParameter sec_user_security =
loCommand.Parameters.Add("@sec_user_security", SqlDbType.Int);
sec_user_security.Value = giSecurity;

SqlParameter sec_user_hidden =
loCommand.Parameters.Add("@sec_user_hidden", SqlDbType.Bit);
sec_user_hidden.Value = gbHidden;

SqlParameter sec_user_deleted =
loCommand.Parameters.Add("@sec_user_deleted", SqlDbType.Bit);
sec_user_deleted.Value = gbDeleted;

loConnection.Open();
loCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
pnlError.Visible = true;
}
finally
{
loConnection.Close();
}

Response.Redirect("/blog/control_panel/security/default.aspx");
}
}

May 10 '06 #5

P: n/a
Try putting the code in the page_load that initially populates the TextBox
inside a conditional such as the following (you will need to change the code
from VB.NET to C#, but it will use the same condition):
If Not IsPostBack() Then mytextbox.Text="ORIGINAL TEXT"
This will cause the initial populating to occur only the first time. Because
the page_load is called before the click event, without this condition the
Text property will always be reset between the time you click the control
and the time you save to the database. Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Jason" <ja*************@hotmail.com> wrote in message
news:11**********************@j73g2000cwa.googlegr oups.com...
No, I haven't tried moving the control to the last thing loaded.
However, I'm starting to have the same trouble, but with just a simple
ASPX page.

I have an ASPX page with text boxes that are populated with data on
page_load. When you click the "save" icon, it takes the contents of
the text boxes and updates the database - only it's grabbing the
original values, not the text in the text boxes at the time the save
icon is clicked! Is there some new property of the asp:textbox in 2.0
that I am missing?

Here is some code:

public partial class control_panel_security_edit_user :
System.Web.UI.Page
{
private int giUserID, giSecurity;
private bool gbHidden, gbDeleted;

protected void Page_Load(object sender, EventArgs e)
{
giUserID =
System.Int32.Parse(Request.QueryString["user_id"].ToString());

SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlCommand loCommand = new SqlCommand("SELECT * FROM tbsec_user
WHERE sec_user_id = " + giUserID.ToString(), loConnection);
loConnection.Open();
SqlDataReader loReader = loCommand.ExecuteReader();
loReader.Read();
lblUserID.Text = loReader["sec_user_id"].ToString();
txtFirstName.Text =
loReader["sec_user_firstname"].ToString().Trim();
txtLastName.Text =
loReader["sec_user_lastname"].ToString().Trim();
txtEmail.Text = loReader["sec_user_email"].ToString().Trim();
txtUsername.Text =
loReader["sec_user_username"].ToString().Trim();
txtPassword.Text =
loReader["sec_user_password"].ToString().Trim();
giSecurity =
System.Int32.Parse(loReader["sec_user_security"].ToString());
gbHidden =
System.Boolean.Parse(loReader["sec_user_hidden"].ToString());
gbDeleted =
System.Boolean.Parse(loReader["sec_user_deleted"].ToString());
loReader.Close();
loConnection.Close();
}

protected void imgSave_Click(object sender, ImageClickEventArgs e)
{
SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlCommand loCommand = new SqlCommand("prsec_user_u");
try
{
loCommand.CommandType = CommandType.StoredProcedure;
loCommand.Connection = loConnection;

SqlParameter sec_user_id =
loCommand.Parameters.Add("@sec_user_id", SqlDbType.Int);
sec_user_id.Value = giUserID;

SqlParameter sec_user_username =
loCommand.Parameters.Add("@sec_user_username", SqlDbType.VarChar, 20);
sec_user_username.Value = txtUsername.Text.Trim();

SqlParameter sec_user_password =
loCommand.Parameters.Add("@sec_user_password", SqlDbType.VarChar, 20);
sec_user_password.Value = txtPassword.Text.Trim();

SqlParameter sec_user_email =
loCommand.Parameters.Add("@sec_user_email", SqlDbType.VarChar, 75);
sec_user_email.Value = txtEmail.Text.Trim();

SqlParameter sec_user_firstname =
loCommand.Parameters.Add("@sec_user_firstname", SqlDbType.VarChar, 50);
sec_user_firstname.Value = txtFirstName.Text.Trim();

SqlParameter sec_user_lastname =
loCommand.Parameters.Add("@sec_user_lastname", SqlDbType.VarChar, 50);
sec_user_lastname.Value = txtLastName.Text.Trim();

SqlParameter sec_user_security =
loCommand.Parameters.Add("@sec_user_security", SqlDbType.Int);
sec_user_security.Value = giSecurity;

SqlParameter sec_user_hidden =
loCommand.Parameters.Add("@sec_user_hidden", SqlDbType.Bit);
sec_user_hidden.Value = gbHidden;

SqlParameter sec_user_deleted =
loCommand.Parameters.Add("@sec_user_deleted", SqlDbType.Bit);
sec_user_deleted.Value = gbDeleted;

loConnection.Open();
loCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
pnlError.Visible = true;
}
finally
{
loConnection.Close();
}

Response.Redirect("/blog/control_panel/security/default.aspx");
}
}

May 10 '06 #6

P: n/a
Nathan,

Thanks for all of your help!

I actually found this solution a few hours ago. if (!IsPostBack)
{.......}

It took me a while of just staring at it and trying to rule out other
possible causes and I found a common thread - this was only happening
when I was "editing" or "pre-populating" the textboxes. It wouldn't
happen when I had empty textboxes.

Check back in a few days (I should be launching the site within the
next few days), but you can visit blogs.active-ant.com to see the
results!

Thanks again for your help!
Jason

May 10 '06 #7

P: n/a
....or blogs.jasonwilkerson.com.

May 10 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.