如何使用下拉列表值进行更新

时间:2009-10-01 16:41:20

标签: asp.net

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

Table A        Table B
   ID          DistrictID
   Name        DistrictName
   FName
   MName
   District

通过使用表格,我填写gridview

     <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>

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

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

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

如有必要,请输入正确的标签

1 个答案:

答案 0 :(得分:2)

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