如何修复“ddlclass”具有选定值,该值无效,因为它不存在于项目列表下拉列表中 [英] How to fix "ddlclass" has a selectedvalue which is invalid because it does not exist in the list of items dropdownlist

查看:67
本文介绍了如何修复“ddlclass”具有选定值,该值无效,因为它不存在于项目列表下拉列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have gridview class and I am trying to display student with their marks.
But when I do I am getting the error -  "ddlclass" has a selectedvalue which is invalid because it does not exist in the list of items dropdownlist.

Class.aspx
<asp:GridView ID="Gridviewclass" runat="server" >
      <Columns>
            <asp:TemplateField HeaderText="Myclass">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlclass" runat="server" DataTextField="Name" DataValueField="classID"

SelectedValue='<%# Bind("classID") %>' >
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Marks" SortExpression="Marks">
                <ItemTemplate>
                    <asp:TextBox ID="txtMarks" runat="server" Text='<%# Bind("Marks") %>' ></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>



Class.aspx.cs
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 Gridviewclass.DataSource = mainclass
            Gridviewclass.DataBind()
End Sub

推荐答案

您收到错误,因为设置选定值时未在下拉列表中设置任何列表项。因此,在下面的代码中,我在下拉控件中添加了演示ListItem(您可以根据您的要求进行更改)。

You are getting error because no listitem is not set in dropdown when a selected value is set. So in below code I added demo ListItem in dropdown control(you can change it as per your requirement).
<asp:GridView ID="Gridviewclass" runat="server" >
      <Columns>
            <asp:TemplateField HeaderText="Myclass">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlclass" runat="server" DataTextField="Name" DataValueField="classID"

SelectedValue='<%# Bind("classID") %>' >
                        <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                        <asp:ListItem>3</asp:ListItem>
                        <asp:ListItem>4</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Marks" SortExpression="Marks">
                <ItemTemplate>
                    <asp:TextBox ID="txtMarks" runat="server" Text='<%# Bind("Marks") %>' ></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>



注意:如果要将数据绑定到数据库中的下拉列表,那么您需要使用gridview的OnRowDataBound属性。请使用以下链接作为参考。



http://www.aspsnippets.com/Articles/How-to-populate-DropDownList-in-GridView-in-ASPNet.aspx [ ^ ]


我认为你必须绑定GridView rowdatabound事件中的下拉列表。



现在你只需要检查行的classid是否是作为下拉列表的值出现。似乎对于某些行,classid在下拉值列表中不匹配。
I think you must be binding the dropdownlist inside the GridView rowdatabound event.

Now you just need to check if the classid for the row is present as a value of the dropdownlist. Seems like for some row the classid is not matching in the list of drop down values.


这篇关于如何修复“ddlclass”具有选定值,该值无效,因为它不存在于项目列表下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆