1

在过去的几天里,我一直在努力为我的问题找到解决方案,但到目前为止我还做不到。我有一个 Gridview,在编辑模式下,其中一个字段有一个 DropDownList 人员,由使用该行的其他两个字段(州和城市)的 Select 填充。我尝试了我在网上找到的所有关于它的东西,但我无法让它工作。你能帮我吗?下面的代码

<asp:GridView ID="GridView1" runat="server" AllowSorting="True"  
        DataKeyNames="id,Municipio,UF" DataSourceID="SqlDataSource5">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            ...stuff...
            <asp:TemplateField HeaderText="UF" SortExpression="UF">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("UF") %>'>
                    </asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("UF") %>'>
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Municipio" SortExpression="Municipio">
                <EditItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("Municipio") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Municipio") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="People" SortExpression="People">
                <ItemTemplate>
                    <asp:Label ID="LbPeople" runat="server" Text='<%# Bind("Cultura") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DdlPeople" runat="server" AutoPostBack="true" 
                        DataSourceID="SqlDataSourcePeopleEdit" DataTextField="People" 
                        DataValueField="People">
                    </asp:DropDownList>
                    <asp:HiddenField ID="Hidden1" Value='<%# Bind("Municipio") %>' runat="server"/>
                    <asp:HiddenField ID="Hidden2" Value='<%# Bind("UF") %>' runat="server"/>
                    <asp:SqlDataSource ID="SqlDataSourcePeopleEdit" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:dbdb %>" 
                        ProviderName="<%$ ConnectionStrings:somar.ProviderName %>"
                        SelectCommand="SELECT People FROM Table_People WHERE (Cidade = @Municipio) AND (UF = @UF)"
                        CancelSelectOnNullParameter="false">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="Hidden1" Name="Municipio" PropertyName="Value" Type="String" />
                            <asp:ControlParameter ControlID="Hidden2" Name="UF"        PropertyName="Value" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </EditItemTemplate>
            </asp:TemplateField>
       ...stuff...     
    </asp:GridView>

我究竟做错了什么?

更新 当我单击编辑时,下拉列表不加载值。如果我在 SqlDataSource 上设置属性 CancelSelectOnNullParameter="false" 它显示“必须声明标量变量@xxx”我已经检查了隐藏字段并且它们已经加载了值,所以问题是控制参数没有从中获取值

4

1 回答 1

0

您应该做的是在下拉列表之前有隐藏字段和 sqldatasource。您遇到的问题是由于加载数据的时间。

<asp:GridView ID="GridView1" runat="server" AllowSorting="True"  
    DataKeyNames="id,Municipio,UF" DataSourceID="SqlDataSource5">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        ...stuff...
        <asp:TemplateField HeaderText="UF" SortExpression="UF">
            <EditItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("UF") %>'>
                </asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("UF") %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Municipio" SortExpression="Municipio">
            <EditItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("Municipio") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Municipio") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="People" SortExpression="People">
            <ItemTemplate>
                <asp:Label ID="LbPeople" runat="server" Text='<%# Bind("Cultura") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:HiddenField ID="Hidden1" Value='<%# Bind("Municipio") %>' runat="server"/>
                <asp:HiddenField ID="Hidden2" Value='<%# Bind("UF") %>' runat="server"/>
                <asp:SqlDataSource ID="SqlDataSourcePeopleEdit" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:dbdb %>" 
                    ProviderName="<%$ ConnectionStrings:somar.ProviderName %>"
                    SelectCommand="SELECT People FROM Table_People WHERE (Cidade = @Municipio) AND (UF = @UF)"
                    CancelSelectOnNullParameter="false">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="Hidden1" Name="Municipio" PropertyName="Value" Type="String" />
                        <asp:ControlParameter ControlID="Hidden2" Name="UF"        PropertyName="Value" Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>

                <asp:DropDownList ID="DdlPeople" runat="server" AutoPostBack="true" 
                    DataSourceID="SqlDataSourcePeopleEdit" DataTextField="People" 
                    DataValueField="People">
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
   ...stuff...     
</asp:GridView>
于 2018-07-02T23:16:33.640 回答