本文介绍了无法将类型为"System.Web.UI.HtmlControls.HtmlInputHidden"的对象转换为类型为"System.IConvertible"的对象.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!!

我写了这段代码,但出现错误

Hello.!!

I write this code but i get Error



有人帮我吗?



anybody help me please?

protected void dgcategory_ItemDataBound(object sender, DataGridItemEventArgs e)
   {
       if (e.Item.ItemType == ListItemType.Header)
       {
           CheckBox chk = new CheckBox();
           chk = (CheckBox)e.Item.FindControl("chkHeader");
           chk.Attributes.Add("onclick", "javascript:return checkheader(''chkHeader'');");

           ImageButton imgtitle = new ImageButton();
           imgtitle = (ImageButton)e.Item.FindControl("imgasc");
           if (queryval.Value == "asc")
           {
               imgtitle.ImageUrl = "images/asc.png";
               imgtitle.ToolTip = "Ascending";
               imgtitle.AlternateText = "Ascending";
           }
           else
           {
               imgtitle.ImageUrl = "images/desc.png";
               imgtitle.ToolTip = "Descending";
               imgtitle.AlternateText = "Descending";
           }

           ImageButton imgtitle1 = new ImageButton();
           imgtitle1 = (ImageButton)e.Item.FindControl("imgasc1");
           if (queryval1.Value == "asc")
           {
               imgtitle1.ImageUrl = "images/asc.png";
               imgtitle1.ToolTip = "Ascending";
               imgtitle1.AlternateText = "Ascending";
           }
           else
           {
               imgtitle1.ImageUrl = "images/desc.png";
               imgtitle1.ToolTip = "Descending";
               imgtitle1.AlternateText = "Descending";
           }
       }
       if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
       {
           CheckBox chk = new CheckBox();
           chk = (CheckBox)e.Item.FindControl("chkDelete");
           chk.Attributes.Add("onclick", "javascript:return checkother(''chkHeader'');");

           if (Convert.ToBoolean((HtmlInputHidden)e.Item.FindControl("hiddisstatus")) == true)
           {
               ((LinkButton)e.Item.FindControl("disstatus")).Text = "Activate";
           }
           else
           {
               ((LinkButton)e.Item.FindControl("disstatus")).Text = "Deactivate";
           }
       }
   }



谢谢.



Thank you.

推荐答案

narendrarathod写道:
narendrarathod wrote:

if(Convert.ToBoolean(( HtmlInputHidden)e.Item.FindControl("hiddisstatus"))== true)

if (Convert.ToBoolean((HtmlInputHidden)e.Item.FindControl("hiddisstatus")) == true)



您要在这里做什么?
您正在将一个隐藏变量转换为布尔值,这根本不可能,因此就是错误.
获取隐藏变量的值,然后尝试



What are you trying to do here?
You are converting a hidden variable to boolean, which is not at all possible so thats the error.
Get the value of the hidden variable and try


narendrarathod写道:
narendrarathod wrote:

if(Convert.ToBoolean(( HtmlInputHidden)e.Item.FindControl("hiddisstatus"))== true)

if (Convert.ToBoolean((HtmlInputHidden)e.Item.FindControl("hiddisstatus")) == true)



试试:



Try:

if (Convert.ToBoolean(((HtmlInputHidden)e.Item.FindControl("hiddisstatus")).Value))


e.Item.FindControl(NameOfControl)

返回Control. Control不能转换为Boolean,因为它们没有像ToString()方法那样的隐式转换.您实际上需要访问存储在HtmlInputHidden对象中的值并将其转换为布尔值...假设您将布尔值存储在某个位置.

因此,其外观应为:

returns a Control. A Control cannot be converted to a Boolean because they do not have an implicit conversion like the ToString() method. You need to actually access the value stored within the HtmlInputHidden object and convert that to a Boolean...assuming that you stored a Boolean value in it somewhere.

So, it should look like:

if (Convert.ToBoolean(((HtmlInputHidden)e.Item.FindControl("hiddisstatus")).Value) == true)


这篇关于无法将类型为"System.Web.UI.HtmlControls.HtmlInputHidden"的对象转换为类型为"System.IConvertible"的对象.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 11:36