Reputation: 1
How can I get a server-side control's tag? I'm guessing it's something like below:
TextBox textBox=new TextBox();
GetTag(TextBox textBox)
{
...
}
And the result is something like <asp:TextBox />
(the control's design time tag).
When I change the control to CheckBox
then the result should be something like <asp:CheckBox />
.
Upvotes: 0
Views: 237
Reputation: 5909
You can use RenderControl
method
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
using (HtmlTextWriter textWriter = new HtmlTextWriter(sw))
{
textBox.RenderControl(textWriter);
}
}
sb
will have textBox
's html content.
Edit:
You can get aspx content and find element tag. like this:
String path = Server.MapPath("~/[aspx path]");
string content = File.ReadAllText(path);
string controlID = "textBox";
int startIndex = content.IndexOf("<asp:TextBox ID=\"" + controlID + "\"");
bool foundEndTag = false;
string controlTag = "";
int i = startIndex;
while (!foundEndTag)
{
if (content[i] == '>')
foundEndTag = true;
controlTag += content[i];
i++;
}
Upvotes: 0
Reputation: 63970
There are no methods that can return these tags for you but constructing a dictionary your self should be easy enough.
Dictionary<Type, string> d = new Dictionary<Type, string>();
d.Add(typeof(TextBox), @"<\asp:TextBox />");
TextBox t = new TextBox();
string tag= (d[t.GetType()]);
And so on...
Upvotes: 2