Unable to cast object of type "System.Web.UI.WebControls.HiddenField" to type Error resolution _ Practical Tips

Source: Internet
Author: User
This article will address:
1. Explain what is System.Web.UI.LiteralControl
2. Why does it happen so often? To cast an object of type "System.Web.UI.WebControls.HiddenField" to a type ... of errors
A control that looks up a template column in a 3.GridView control (explains cells and controls)

When you bind data with the GridView control, you often convert a column to a template column, such as:
We pull a control gridview in the page, name: Gvdata, convert one of the columns to a template column, where we convert the column "owning unit", and the page Template column section code is as follows:
<asp:templatefield headertext= "affiliated Company" >
<EditItemTemplate>
<asp:hiddenfield id= "HDFXZ" runat= "server" Value= ' <% #Bind ("unit")%> '/>
<asp:dropdownlist id= "DDLXZ" runat= "Server"/>
</EditItemTemplate>
<ItemTemplate>
<asp:label id= "Label1" runat= "server" text= ' <%# return2dwname ' (Eval ("unit"). ToString ())%> ' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
As you can see, the template column has two controls under the edit template, one is the hidden field hdfxz, one is the Drop-down list ddlxz, and then we remove the value of the Drop-down list ddlxz in the. cs page, part of the code:
protected void Gvdata_rowupdating (object sender, Gridviewupdateeventargs e)
{
String Dwid = ((DropDownList) this. Gvdata.rows[e.rowindex]. CELLS[2]. CONTROLS[1]). SelectedValue;

}
Note: The above is triggered when modifying data, we put it here just to illustrate the problem, not all the code, no practical significance.
Let's explain that E.rowindex is getting the index value of the currently modified data row, this. Gvdata.rows[e.rowindex] This is the current index to obtain the present row, Cells[2] is the third column under the current line (starting from 0), Controls[1] represents the second control under the third column (also from 0), so we can understand , the value of the Dwid should be the value of the Drop-down list box Ddlxz (because the Drop-down list box belongs to the second control in the edit template, as we mentioned above).
Error after run: Cannot cast object of type "System.Web.UI.LiteralControl" to Type "System.Web.UI.WebControls.DropDownList".

Perhaps everyone is very puzzled, and then go back to check again, there are no errors ah, then we will see what is the System.Web.UI.LiteralControl control?
System.Web.UI.LiteralControl: A simple explanation is any other string that does not need to be processed on the server.
This means that our edit template has "other strings that don't need to be processed on the server".
After analyzing this sentence "other strings", and then look at the template column of the source code, the suspicion is due to generate a template column generated by the space, because the space is also a string ah, so the source code changes to:
<asp:templatefield headertext= "affiliated Company" >
<edititemtemplate><asp:hiddenfield id= "HDFXZ" runat= "server" Value= ' <% #Bind ("unit")%> '/><asp :D ropdownlist id= "DDLXZ" runat= "Server"/></edititemtemplate>
<ItemTemplate>
That is to say, remove the space between the EditItemTemplate tags. At this time we run again, OK, finished Ah, oh, the original is really a space in mischief Ah!.net the space as a System.Web.UI.LiteralControl, hehe!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.