Reputation: 1545
I have a user control with a text box and a button. When I press the button, I try to read the contents using the text box's ID (from the user control's code behind) but I always get a blank result. What is the proper way to reference a control in a user control from the same user control.
I read elsewhere that I should simply use the control ID, but somehow it is not working. I must be doing something wrong and I am hoping this is a common mistake and that someone can she some light on this.
Edit:
Markup:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ForumPostControl.ascx.cs" Inherits="ForumPostControl" debug="true" %>
<div id="PostDiv" runat="server">
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="border:1px solid white; border-radius:5px; margin-bottom:5px;">
<tr>
<td valign="top" width="52" style="text-align:center; padding:10px 0px 10px 0px; border-bottom:1px dashed #FFF;">
<asp:Image ID="imgReplyPoster" runat="server" />
</td>
<td valign="middle" style="text-align:left; padding:10px 0px 10px 0px; border-bottom:1px dashed #FFF;">
<asp:HyperLink ID="lnkReplyPoster" runat="server">lnkReplyPoster</asp:HyperLink><br />
<asp:Label ID="lblCreated" runat="server" Text="Label"></asp:Label>
</td>
<td style="text-align:right; padding:10px 20px 10px 0px; border-bottom:1px dashed #FFF;">
<asp:LinkButton ID="btnReply" runat="server" onclick="btnReply_Click">Reply</asp:LinkButton>
<asp:LinkButton ID="btnDelete" runat="server" OnClick="btnDelete_Click" OnClientClick="return ConfirmClick('Do you really want to delete this post?');"><img src="images/icons/trash16.png" alt="Delete" border="0" /></asp:LinkButton>
</td>
</tr>
<tr>
<td valign="top" align="justify" style="padding:10px;" colspan="3">
<asp:Literal ID="ltrlPostBody" runat="server"></asp:Literal>
</td>
</tr>
</table>
<asp:PlaceHolder ID="phReply" runat="server" Visible="False">
<div align="center" style="padding:5px 10px 10px 10px;">
<asp:HiddenField ID="hdnParentID" runat="server" />
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<asp:TextBox ID="txtReply" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right" style="padding-top:20px;">
<asp:LinkButton ID="lnkPostReply" runat="server" OnClick="lnkPostReply_Click">Post reply</asp:LinkButton>
</td>
</tr>
</table>
</div>
</asp:PlaceHolder>
</div>
Code behind for saving text to database:
protected void lnkPostReply_Click(Object sender, EventArgs e)
{
String connectionString = (String)System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
SqlConnection cnn = new SqlConnection(connectionString);
SqlCommand cmd;
String strSQL;
cnn.Open();
strSQL = "INSERT INTO ForumThreads (ForumID, ParentID, PostBody, Created, CreatedBy, Modified, ModifiedBy) VALUES " +
"(@ForumID, @ParentID, @PostBody, GETDATE(), @CreatedBy, GETDATE(), @CreatedBy);";
cmd = new SqlCommand(strSQL, cnn);
cmd.Parameters.AddWithValue("@ForumID", prvForumID);
cmd.Parameters.AddWithValue("@ParentID", prvForumThreadID);
cmd.Parameters.AddWithValue("@PostBody", txtReply.Text);
cmd.Parameters.AddWithValue("@CreatedBy", CurrentMember.MemberID);
cmd.ExecuteNonQuery();
cmd.Dispose();
cnn.Close();
cnn.Dispose();
Response.Redirect("forum_topic.aspx?TID=" + prvEncryptedTID);
}
The problem is with reading the txtReply control.
Edit #2
I am still struggling with this and experimenting stuff. Here are some finding, I hope they can help solve the mystery. I noticed that I can read properties from the text box control such as width and ID but I cannot read the text that is entered by the user. It always returns blank. If I pre set the text programmatically to, say, "Initial Text", then when I try to read the .text property, I still get "Initial Text", no matter what the user enters. It is as if the text typed the user is lost on post back.
Upvotes: 1
Views: 782
Reputation: 7943
As you mentioned that you are using the usercontrol inside a repeater, I would check the page's Page_Load event method to see I am not rebinding the repeater at postback:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Repeater1.DataSource = GetData();
Repeater1.DataBind();
}
}
And my Repeater1_ItemDataBound may look something like:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
ForumPostControl myControl = (ForumPostControl)e.Item.FindControl("ForumPostControl1");
myControl.MyProperty = 20;//My custom property of user control
}
}
Now, each postback may wipe out my usercontrol's custom property. I need to make sure it persist on postback. In my ForumPostControl.ascx.cs I would store the value of this property in visestate and retrieve when needed. I should have this code in my usercontrol:
public int MyProperty
{
get
{
int myProperty = 0;
if (ViewState["MyProperty"] != null)
{
int.TryParse(ViewState["MyProperty"].ToString(), out myProperty);
}
return myProperty;
}
set
{
ViewState["MyProperty"] = value;
}
}
Upvotes: 1