繁体   English   中英

无法将源类型“ System.Web.UI.Control”转换为目标类型“ System.Web.UI.WebControls.TextBox”

[英]Cannot convert source type 'System.Web.UI.Control' to target type 'System.Web.UI.WebControls.TextBox'

我有以下代码行

objCustomField = FindControl(oList.ID.Replace("ddl", "txt"));

我已经从用Visual Basic .net编写的旧应用程序继承了此页面,并将其转换为c#。

此特定行抛出此错误:

无法将源类型“ System.Web.UI.Control”转换为目标类型“ System.Web.UI.WebControls.TextBox”

这是它包含的整个例程:

private void PopulateDecodeDropDown(ref DropDownList oList, string vListItems, int vValueIndex, int vTextIndex, string vSelectedValue, string vCustomText , ref TextBox oCustomField, bool vIncludeBlank = true, string vBlankText = "", string vBlankValue = "")
    {


        TextBox objCustomField = default(TextBox);
        ListItem objItem = default(ListItem);
        string[] arItems = vListItems.Split(Convert.ToChar("|"));  //added syntax to convert from string to Char 10/21/15 Max //


        oList.Items.Clear();


        if (vIncludeBlank == true)
        {
            objItem = new ListItem();
            var _with7 = objItem;
            objItem.Value = vBlankValue;
            objItem.Text = vBlankText;
            oList.Items.Add(objItem);
        }

        if (!string.IsNullOrEmpty(vCustomText))
        {
            objItem = new ListItem();
            var _with8 = objItem;
            objItem.Value = "-1";
            objItem.Text = vCustomText;
            oList.Items.Add(objItem);
        }

        for (n = 0; n <= arItems.Count() - 1; n++)
        {
            objItem = new ListItem();
            var _with9 = objItem;
            objItem.Value = mobjFormat.StripObjectToString(arItems[n]);  //added square brackets to arItems to facilitate array 10/21/15 Max //
            objItem.Text = mobjFormat.StripObjectToString(arItems[n]);  //added square brackets to arItems to facilitate array 10/21/15 Max //
            oList.Items.Add(objItem);
        }

        try
        {
            //set the value
            oList.SelectedValue = vSelectedValue;

            //if for some reason the value selected is different then
            //we need to show custom something when wrong
            if (oList.SelectedValue != vSelectedValue)
            {

                if (!string.IsNullOrEmpty(vCustomText))
                {
                    objCustomField = FindControl(oList.ID.Replace("ddl", "txt"));
                    //DropDownList objCustomField = (DropDownList) FindControl(oList.ID.Replace("ddl", "txt"));
                    oList.SelectedValue = "-1";  //added double quotes to facilitate conversion to string 10/21/15 Max //
                    oCustomField.Text = vSelectedValue;
                    oCustomField.Style.Add("display", "");  //changed to c# syntax 10/21/15 Max //
                }
                else
                {
                    objItem = new ListItem();
                    var _with10 = objItem;
                    objItem.Value = Strings.Trim(arItems(vSelectedValue));
                    objItem.Text = Strings.Trim(arItems(vSelectedValue));
                    oList.Items.Add(objItem);
                }


            }

        }
        catch (Exception ex)
        {

            if (!string.IsNullOrEmpty(vCustomText))
            {
                objCustomField = FindControl(oList.ID.Replace("ddl", "txt"));
                oList.SelectedValue = -1;
                oCustomField.Text = vSelectedValue;
                oCustomField.Style.Item("display") = "";
            }
            else
            {
                objItem = new ListItem();
                var _with11 = objItem;
                objItem.Value = Strings.Trim(arItems(vSelectedValue));
                objItem.Text = Strings.Trim(arItems(vSelectedValue));
                oList.Items.Add(objItem);
            }

        }

    }

我知道在这方面还有其他语法错误,但是这个错误对我来说是一个新错误。 有没有人遇到这个错误,或者可以向我解释

你需要这样投

objCustomField = (TextBox)FindControl(oList.ID.Replace("ddl", "txt"));

FindControl()返回Control类型的引用。 控件是TextBox的基类,因此您可以轻松使用类型转换:

objCustomField = (TextBox) FindControl(oList.ID.Replace("ddl", "txt"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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