Get All Web Controls of a Specific Type on a Page

后端 未结 8 1851
忘掉有多难
忘掉有多难 2020-12-01 03:58

I have been pondering how I can get all controls on a page and then perform a task on them in this related question:

How to Search Through a C# DropDownList Programm

8条回答
  •  春和景丽
    2020-12-01 04:04

    This works if you use the form components from system.web.ui however this does not work when you use them from system.web.mvc apparently so i came up with the following work around.

    for (Int32 idx = 0; idx < formCollection.Count; idx += 1)
                        {
                        String Name = formCollection.Keys[idx];
                        String value = formCollection[idx];
    
                        if (Name.Substring(0, 3).ToLower() == "chk")
    
                            {
                            Response.Write(Name + " is a checkbox 
    "); } else if (Name.Substring(0, 5).ToLower() == "txtar") { Response.Write(Name + " is a text area
    "); } else if (Name.Substring(0, 2).ToLower() == "rd") { Response.Write(Name + " is a RadioButton
    "); } }

    This works for me however i found out that radio button if not selected is null so doesnt return anything which is ok i dont have to write anything to the database if it is null

提交回复
热议问题