I am working on a very simple server control that I want to databind to an objectdatasource. I am not sure exactly on how to go about doing it. I was wondering if anyone can help here. My code is below. The server control will bind to MyObject (Business
object) which returns a single object. I want to bind the MyInfo property of MyObject to my server control.
[ToolboxData("<{0}:Displayinfo runat=server></{0}:Displayinfo>")]
[System.ComponentModel.Description("Displayinfo")]
public class Displayinfo : CompositeControl, INamingContainer
{
private string content;
protected override void OnPreRender(EventArgs e)
{
// Render Control Text
base.OnPreRender(e);
}
}
// MyObject.cs public class MyObject { private int _myObjectId; public int MyObjectId { set { _myObjectId = value; } get { return _myObjectId; } }
private string _myInfo; public string MyInfo { set { _myInfo = value; } get { return _myInfo; } } }
Please be sure to click "Mark as Answer" on the post that helped you.
From your description, it seems that you want to build a data-bound custom server control which shares datasourceID property and databind method, right?
Generally, we can call these controls as Custom data-bound control, you should make you control inherit from your CompositeDataBoundControl, which is the base class that extends the DataBoundControl class. It implements the typical code required by composite
controls, including the code that restores the control's child control hierarchy from view state after a postback is made. It binds to an IEnumerable data source and enumerates the data to build a control tree and serves as the basis for data-bound controls,
such as GridView and DetailsView.
For more information and samples, please refer:
My question is similar to the original but I'm not quite sure if your answer applies to my situation or not. I currently have a custom control which inherits from the CompositeControl. It contains a couple textbox and some a couple of labels, etc. I have
a property exposing the Text property of the textbox. The binding works fine one-way but not two-ways. Any guidance on this would be very helpful.
Public Class MyTextArea
Inherits System.Web.UI.WebControls.CompositeControl
Private m_textarea As System.Web.UI.WebControls.TextBox
<System.ComponentModel.Bindable(True, System.ComponentModel.BindingDirection.TwoWay)> _
<System.ComponentModel.Description("The textarea text.")> _
Public Property Text() As String
Get
EnsureChildControls()
Return m_textarea.Text
End Get
Set(ByVal value As String)
EnsureChildControls()
m_textarea.Text = value
End Set
End Property
Protected Overrides Sub CreateChildControls()
' code to add controls here
End Sub
End Class
End Class
Member
34 Points
177 Posts
DataBound Custom Server Control
Dec 11, 2007 08:03 PM|officialboss|LINK
I am working on a very simple server control that I want to databind to an objectdatasource. I am not sure exactly on how to go about doing it. I was wondering if anyone can help here. My code is below. The server control will bind to MyObject (Business object) which returns a single object. I want to bind the MyInfo property of MyObject to my server control.
All-Star
35458 Points
3430 Posts
Re: DataBound Custom Server Control
Dec 12, 2007 10:23 PM|Nai-Dong Jin - MSFT|LINK
Hi,
From your description, it seems that you want to build a data-bound custom server control which shares datasourceID property and databind method, right?
Generally, we can call these controls as Custom data-bound control, you should make you control inherit from your CompositeDataBoundControl, which is the base class that extends the DataBoundControl class. It implements the typical code required by composite controls, including the code that restores the control's child control hierarchy from view state after a postback is made. It binds to an IEnumerable data source and enumerates the data to build a control tree and serves as the basis for data-bound controls, such as GridView and DetailsView.
For more information and samples, please refer:
http://msdn.microsoft.com/en-us/library/ms366539(VS.80).aspx
Thanks.
Member
52 Points
148 Posts
Re: DataBound Custom Server Control
Dec 14, 2007 03:07 PM|dsa1971|LINK
Michael,
My question is similar to the original but I'm not quite sure if your answer applies to my situation or not. I currently have a custom control which inherits from the CompositeControl. It contains a couple textbox and some a couple of labels, etc. I have a property exposing the Text property of the textbox. The binding works fine one-way but not two-ways. Any guidance on this would be very helpful.
custom control Data Bound Control Composite Control Controls