简体   繁体   中英

How can I get a server-side control's tag?

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 /> .

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...

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++;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM