Reputation: 8225
I have one problem with a gridview control and maybe someone can help me.
In the gridview, there is a field Country
which comes from the database.
I have enabled editing the data in the gridview, and when someone will click the gridview edit button, the gridview will come up and offer them to select the new value for that field.
But when I am clicking Edit, I am getting this error :
'Type 'System.Web.UI.WebControls.ListItem' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.'
Here is the code I have for that field, in shorter version, excluding most of the countries from the example:
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("Country") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlCountry"
SelectedItem='<%# Bind("Country") %>'
runat="server">
<asp:ListItem Value="N/A">Please select</asp:ListItem>
<asp:ListItem Value="US">United States</asp:ListItem>
<asp:ListItem Value="AF">Afghanistan</asp:ListItem>
<asp:ListItem Value="AL">Albania</asp:ListItem>
<asp:ListItem Value="DZ">Algeria</asp:ListItem>
<asp:ListItem Value="AS">American Samoa</asp:ListItem>
<asp:ListItem Value="AD">Andorra</asp:ListItem>
<asp:ListItem Value="AO">Angola</asp:ListItem>
<asp:ListItem Value="AI">Anguilla</asp:ListItem>
<asp:ListItem Value="AQ">Antarctica</asp:ListItem>
<asp:ListItem Value="AG">Antigua And Barbuda</asp:ListItem>
<asp:ListItem Value="AR">Argentina</asp:ListItem>
<asp:ListItem Value="AM">Armenia</asp:ListItem>
<asp:ListItem Value="AW">Aruba</asp:ListItem>
<asp:ListItem Value="AU">Australia</asp:ListItem>
<asp:ListItem Value="AT">Austria</asp:ListItem>
<asp:ListItem Value="ZR">Zaire</asp:ListItem>
<asp:ListItem Value="ZM">Zambia</asp:ListItem>
<asp:ListItem Value="ZW">Zimbabwe</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
Upvotes: 2
Views: 3235
Reputation: 2975
Have you tried creating a datasource for your countries, then just putting the DropDownList into the edittemplate and assigning the datasource from code behind? This will get around explicitly creating a ListItem object into the GridView. Here is an long-winded example using a DataSource: msdn.microsoft.com/en-us/library/ms972948.aspx.
Upvotes: 2
Reputation: 5757
Use SelectedValue
instead of SelectedItem
property.
Found here.
Upvotes: 2