User control that replaces some another control on aspx page
I have a control on 开发者_运维问答a page (let's say a button). I want to create such a user control, that will have a property TargetControlID, which takes an ID of control (button) on a page and on render will replace (hide) it with another button (a clone of a first button). Is it possible? Did anyone ever made such thing?
It's certainly possible, but I would suggest an alternate approach that will have fewer problems.
Instead of replacing the target control, just hide it by setting it's visibility to false. This way any processing that it does will still work, control ID's will remain static on rendering (automatically generated control ID's depend on order of controls), and it will have the same effect as you're looking for.
Yes, you can do what you're talking about. Create your user control in the usual way. Here's a link in case you're not familiar with that: Creating a User Control
Then create a property in the code-behind like this:
public string TargetControlID { get; set; }
Once it is a property, you can access it through the html:
<Azat:AzatButton ID="abtn1" runat="server" TargetControlID="btnOtherButton1" />
Then in the Page_Load() event handler, you instantiate a button based on this.abtn1.TargetControlID and remove abtn1 from the Page's Controls collection.
this.Controls.Remove(this.abtn1);
精彩评论