Last post Sep 20, 2010 06:17 PM by muybn
Sep 16, 2010 02:57 PM|muybn|LINK
I reference a user control in my ListView in this manner:
<asp:Panel ID="pnlEmail" Visible="true" runat="server">
<asp:Label ID="lblEmail" Text="E-Mail:" runat="server" />
<asp:TextBox ID="Email" Text='<%#Bind("Email")%>' TabIndex="5" runat="server" />
<asp:Label ID="lblEmail_Req" Text="*" ForeColor="#FF0000" runat="server" />
The problem is that the #Bind command doesn't update the database, but it does so when I paste the code directly into the aspx page (in other words, without the user control involved). Any suggestions?
Sep 16, 2010 03:39 PM|smithspd|LINK
Sep 16, 2010 09:48 PM|muybn|LINK
Upon second glance, the article mentioned doesn't address at all what I need to do--rather, it deals with binding just regular controls and I have no problem with that.
Maybe I didn't explain clearly at first, so here goes attempt #2: What I need is a way to bind a
custom user control contained in an .ascx file, and display it back in the original .aspx page. I've created a number of these user controls because I have to re-use them in different locations throughout the site. The problem I'm having is that while
the textboxes, for example, contained in the user controls populate and display initially, they won't update per the #Bind commands as they will in the regular .aspx page.
This is an example:
Reference the user control in the .aspx page:
<Prf:email ID="Email_E" runat="server" />
The user control in the .ascx page:
<asp:TextBox ID="Email" CausesValidation="true" EnableViewState="False" Font-Bold="false"
Text='<%#Bind("Email")%>' BackColor="#FFFFFF" Enabled="true" TabIndex="50" runat="server" />
My question is, how do I get this textbox in the user control to bind user changes like it does when in the .aspx page? I can't find any satisfactory explanation on how to do this.
Sep 20, 2010 04:58 AM|Qin Dian Tang - MSFT|LINK
You need to create a property in email user control. This property is used to set and get the value from the TextBox which is needed to bind, not set the two-way binding in TextBox. So after created the property, the email user control to bind the value
<Prf:email ID="Email_E" runat="server" Email='<%# Bind("Email") %>' />
On aspx page DataBound control won't bind for the control in user control. It binds the user control directly.
Sep 20, 2010 06:17 PM|muybn|LINK
Perfect! Thank you, Tang. (Are you in Vietnam, by the way?) You relieved a lot of frustration for me. It's a bit more code-behind than I wanted, but works great and is flexible. Following is my final product (along with the textbox control in the ascx file).
This was a new experience since I hadn't worked this directly before with exposing class properties and using them.
Partial Class userControls_profile_email
Public Property _Mail() As String
Set(ByVal value As String)
Email.Text = value
Public Property _Mail_Disp() As Boolean
Set(ByVal value As Boolean)
chkEmail.Checked = value