从控件ASP.net获取文本C# [英] Getting text from a control ASP.net c#

查看:116
本文介绍了从控件ASP.net获取文本C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我对ASP.net相当陌生.我在具有公共功能的页面中创建了一个公共类,该公共类将在页面中找到一个控件,一旦找到该控件,它将将该控件转换为下拉列表并获取文本值.我使用了Page.FindControl(),但是它总是返回一个空值.这是我的代码:

Hi,

I am quite new with ASP.net. I created a public class within a page that has a public function that will find a control in the page, once it finds that control it converts the control to a dropdownlist and gets the text value. i used the Page.FindControl() but it always returns a null value. Here is my code:

public class Dashboard : System.Web.UI.Page
    {

        public String getTxt(string xID,String xType)
        {
            String ObjText;

            switch (xType.ToUpper())
            {
                case "DROPDOWNLIST":
                    //foreach (Control c in Page.Controls)
                    //{
                    //    if (c.ID == xID)
                    //    {

                    //    }
                    //}

                    DropDownList dlist = (DropDownList) Page.FindControl(xID);

                    if (dlist != null)
                        ObjText = dlist.Text;
                    else
                        ObjText = "";
                    break;
                default:
                    ObjText = "";
                    break;
            }


            return ObjText;
        }
    }



任何帮助将不胜感激.

谢谢,



Any help is greatly appreciated.

Thanks,

推荐答案

FindControl的搜索仅深入1级,因此,如果您要查找的控件不在页面级的正下方,它将返回null .递归的FindControl方法(在线有很多方法,这里是一个:
FindControl only goes 1 level deep in its search, so if the control you are looking for isn''t directly below page level, it will return null. A recursive FindControl method (there are many online, here is one: http://weblogs.asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspx[^]) will help you if this is the route you want to go.


这篇关于从控件ASP.net获取文本C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆