An error occurred while forcibly converting the object of the type "System. Web. UI. WebControls. HiddenField" to the type.

Source: Internet
Author: User

This article will solve the following problems:
1. What is System. Web. UI. LiteralControl?
2. Why does it happen to forcibly convert an object of the type "System. Web. UI. WebControls. HiddenField" to a type ...... Error
3. The control for finding the template column in The GridView control (interpreting Cells and Controls)

When you bind data with the GridView control, you often need to convert a column to a template column, for example:
On the page, we pull a control GridView named GVData to convert a column into a template column. Here we convert the column to the "unit". The code of the template column on the page is as follows:
<Asp: TemplateField HeaderText = "">
<EditItemTemplate>
<Asp: HiddenField ID = "HDFxz" runat = "server" Value = '<% # Bind ("") %>'/>
<Asp: DropDownList ID = "DDLxz" runat = "server"/>
</EditItemTemplate>
<ItemTemplate>
<Asp: Label ID = "Label1" runat = "server" Text = '<% # return2dwNAME (Eval (" "). toString () %> '> </asp: Label>
</ItemTemplate>
</Asp: TemplateField>
As you can see, there are two controls under the editing template of this template column, one is the hidden domain HDFxz, the other is the drop-down list DDLxz, and then we are in. take the value of DDLxz from the drop-down list on the cs page. Some codes:
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 data is modified. We put it here to illustrate the problem, not all code, and it has no practical significance.
Let's explain, e. rowIndex is used to obtain the index value of the currently modified data row. this. GVData. rows [e. rowIndex] The current row is obtained by the current index. Cells [2] indicates the third column under the current row (starting from 0 ), controls [1] indicates the second control in the third column (also counted from 0). After this explanation, we can understand it, the dwid value should be the value of DDLxz in the drop-down list box (because the drop-down list box belongs to the second control in the template editing, which we mentioned above ).
An error occurs after running: the object of the type "System. Web. UI. LiteralControl" cannot be forcibly converted to the type "System. Web. UI. WebControls. DropDownList ".

Maybe everyone is wondering. Let's go back and check again. There is no error in the number. Now let's see what the System. Web. UI. LiteralControl control is?
System. Web. UI. LiteralControl: simply interpreted as any other string that does not need to be processed on the server.
That is to say, the template we edited above has "other strings that do not need to be processed on the server". What is that about.
After analyzing this sentence "other strings" and then looking at the source code of the template column, we suspect that the template column is generated with spaces, because spaces are strings, so modify the source code:
<Asp: TemplateField HeaderText = "">
<EditItemTemplate> <asp: HiddenField ID = "HDFxz" runat = "server" Value = '<% # Bind ("") %>'/> <asp: dropDownList ID = "DDLxz" runat = "server"/> </EditItemTemplate>
<ItemTemplate>
That is to say, remove spaces between EditItemTemplate labels. Now let's run it again. OK, it's all done. Haha, it turns out that it's really a space !. Net regards space as System. Web. UI. LiteralControl!

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.