【问题标题】:How to Update by using dropdownlist value如何使用下拉列表值进行更新
【发布时间】:2009-10-01 16:41:20
【问题描述】:

在 asp.net vs05 上工作。我有两个表

Table A        Table B
   ID          DistrictID
   Name        DistrictName
   FName
   MName
   District

通过使用表格来填充网格视图

     <asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" DataSourceID="SQL" DataKeyNames="StudentID" EmptyDataText="No information to display">
                <Columns>
                    <asp:BoundField DataField="StudentID" HeaderText="StudentID" SortExpression="StudentID" />
                    <asp:BoundField DataField="StudentName" HeaderText="StudentName" />
                    <asp:BoundField DataField="FatherName" HeaderText="FatherName" SortExpression="FatherName" />
                    <asp:BoundField DataField="DivisionName" HeaderText="DivisionName" />
                    <asp:CommandField ShowEditButton="True" />
                </Columns>
 </asp:GridView>
        <asp:SqlDataSource ID="SQL" runat="server" ConnectionString="<%$ ConnectionStrings:JobTestConnectionString %>"
            SelectCommand="SELECT StudentID, StudentName, FatherName FROM studentInfo" UpdateCommand="UPDATE studentInfo SET StudentID = @StudentID, StudentName = @StudentName, FatherName = @FatherName WHERE (StudentID = @StudentID)">
            <UpdateParameters>
                <asp:Parameter Name="StudentID" />
                <asp:Parameter Name="StudentName" />
                <asp:Parameter Name="FatherName" />
            </UpdateParameters>
        </asp:SqlDataSource>

现在我在这个网格上设置了一个 DropDownList。我在 TempletField 上设置了这个。我想用 Table-B 填充这个 ddl,并且还希望编辑命令正常工作。

<asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" DataSourceID="SQL" EmptyDataText="No information to display">
            <Columns>
                <asp:BoundField DataField="StudentID" HeaderText="StudentID" SortExpression="StudentID" />
                <asp:BoundField DataField="StudentName" HeaderText="StudentName" SortExpression="StudentName" />
                <asp:BoundField DataField="FatherName" HeaderText="FatherName" SortExpression="FatherName" />
                <asp:TemplateField HeaderText="DivisionName" SortExpression="DivisionName">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlD" DataTextField="StudentName"
                            DataValueField="StudentName" SelectedValue='<%# Bind("DivisionName", "{0}") %>'>
                        </asp:DropDownList><asp:SqlDataSource ID="SqlD" runat="server" ConnectionString="<%$ ConnectionStrings:JobTestConnectionString %>"
                            SelectCommand="SELECT [StudentName] FROM [Division]"></asp:SqlDataSource>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("DivisionName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" />
            </Columns>
        </asp:GridView>

运行此片段浏览器显示以下错误:

'DropDownList2' 有一个无效的 SelectedValue,因为它不存在于项目列表中。 参数名称:值

现在我的问题是如何通过 Table-B 设置 ddl。填充后我想使用 gridview 的命令字段更新 Table-A。

如有必要,请正确标记

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    您的下拉列表中填充了 StudentNames,但您将所选值绑定到 DivisionName。

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 2021-01-09
      • 2013-10-30
      • 2020-06-27
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多