Is it possible to convert input control to label?
I am making a form that contains a lot of User Controls, each User Control is part of the form (contains TextBox, ComboBox etc).
User will use the form to update their information. At end of submission, I need to display the original data and the data that the user have entered.
I wonder if is possible that I can replace the input control (TextBox etc) to Label? So I can just simply use the same user control, then convert each of the input control to label to display the data... (I just don't really want to use readonly or disable)
Note: I used different dataset to map each of the User Control data....
What I was thinking to do is like to get the input control from Page.Controls:
aInputControl = new Label();
or...
Page.Controls.Remove(aInputControl);
Then somehow add new Label in same position in the page... But I have no idea how...can't think of anything except add another div to surround each of the control...
I just wonder if it this is possible...
Thanks in advance.
================
Edit: Seems like making new user control is not a good way开发者_Go百科 for me....I will just try to somehow map each original data and new data into a new User control, and write them into page...but anyway, thanks for the idea guys.
You can make a custom user control that contains a TextBox and a Label, and displays one or the other depending on whether or not it has a value.
I would create a user control or a web control to encapsulate that functionality. Add a Property to change the display mode and some logic in the control to determine which control to show.
Here is a sample of code to give you an idea, I can expand on this if you would like.
public class ReadOnlyControl<T> : WebControl where T : Control, ITextControl {
protected T inputControl;
protected Label lblLabel;
public bool IsReadOnly { get; set; }
public string Text { get; set; }
protected override void Render(HtmlTextWriter writer) {
Control control = IsReadOnly ? lblLabel : (Control)inputControl;
((ITextControl)control).Text = Text;
control.RenderControl(writer);
}
}
You may want to rethink your design. I recommend you have another view/page that displays the data summary after submit. Additionally, you will have more control of the formatting this way. I don't want to sound condescending, but it just sounds like you are being a little lazy.
精彩评论