我有下面的代碼行無法將源類型「System.Web.UI.Control」爲目標類型「System.Web.UI.WebControls.TextBox」
objCustomField = FindControl(oList.ID.Replace("ddl", "txt"));
我繼承此頁面一個用visual basic .net編寫的遺留應用程序,我將它轉換爲c#。
這種特殊的行拋出這個錯誤:
Cannot convert source type 'System.Web.UI.Control' to target type '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);
}
}
}
我知道還有其他一些語法錯誤在這很好,但此錯誤對我來說是新的。有沒有人碰到這個錯誤或許可以解釋給我
這就是它感謝您的幫助和解釋太謝謝。 – MaximusPrime