在ASP.NET Gridview控件中实现级联下拉列表

时间:2016-03-21 13:47:15

标签: c# sql asp.net gridview

我正在使用SQLDataSource进行ASP.NET Gridview控件。用户需要能够编辑一行,但两个字段将具有下拉列表。一个下拉列表中的信息取决于在第一个下拉列表中选择的内容。如何才能通过仅使用SQLDataSource来实现?如果没有,有没有一个很好的资源,没有SQLDataSource吗?这是one resource I found并尝试使用,但只有在ItemTemplateField中才有效。 这是我的代码:

<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" Width="1100px" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="CustomerID,StateID" DataSourceID="SqlDSCustomers" GridLines="Vertical">
  <AlternatingRowStyle BackColor="#DCDCDC" />
  <Columns>
    <asp:BoundField DataField="CustomerID" Visible="false" HeaderText="CustomerID" InsertVisible="False" ReadOnly="True" SortExpression="CustomerID" />
    <asp:BoundField DataField="Customer" HeaderText="Customer Name" SortExpression="Customer" />
    <asp:TemplateField HeaderText="Country" SortExpression="Country">
      <EditItemTemplate>
        <asp:DropDownList ID="ddlCountryEdit" runat="server" AutoPostBack="True" DataSourceID="SqlDSCountries" SelectedValue='<%# Bind("Country_ID") %>' DataTextField="Country" DataValueField="Country_ID"></asp:DropDownList>
        <asp:SqlDataSource ID="SqlDSCountries" runat="server" ConnectionString="<%$ ConnectionStrings:cnnMain %>" SelectCommand="SELECT [Country_ID], [Country] FROM [Countries]"></asp:SqlDataSource>
      </EditItemTemplate>
      <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="State" SortExpression="State">
      <EditItemTemplate>
        <asp:DropDownList ID="ddlStateEdit" runat="server" DataSourceID="SqlDSStates" SelectedValue='<%# Bind("StateID") %>' DataTextField="State" DataValueField="StateID"></asp:DropDownList>
        <asp:SqlDataSource ID="SqlDSStates" runat="server" ConnectionString="<%$ ConnectionStrings:cnnMain %>" SelectCommand="SELECT [StateID], [State] FROM [States] WHERE ([Country_ID] = @Country_ID)">
          <SelectParameters>
            <asp:ControlParameter ControlID="ddlCountryEdit" Name="Country_ID" PropertyName="SelectedValue" Type="Int32" />
          </SelectParameters>
        </asp:SqlDataSource>
      </EditItemTemplate>
      <ItemTemplate>
        <asp:Label ID="Label2" runat="server" Text='<%# Bind("State") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes">
    </asp:BoundField>
    <asp:CommandField ShowEditButton="True">
    </asp:CommandField>
    <asp:TemplateField ShowHeader="False">
      <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" ForeColor="Black" OnClientClick="return confirm('Are you sure you want to delete this row?')" Text="Delete"></asp:LinkButton>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
  <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
  <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
  <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
  <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
  <SortedAscendingCellStyle BackColor="#F1F1F1" />
  <SortedAscendingHeaderStyle BackColor="#0000A9" />
  <SortedDescendingCellStyle BackColor="#CAC9C9" />
  <SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>

这个当前代码一直有效,直到我改变国家。因此,所有字段都显示正确的信息,国家/地区下拉列表显示我们使用的所有国家/地区。状态下拉列表显示预选国家/地区的正确状态,但是,一旦我更改国家/地区,我就会收到此错误:

  

数据绑定方法,如Eval(),XPath()和Bind()只能是   在数据绑定控件的上下文中使用。

这是堆栈跟踪:

  

[InvalidOperationException:数据绑定方法,如Eval(),   XPath()和Bind()只能在数据绑定的上下文中使用   控制。] System.Web.UI.Page.GetDataItem()+ 3210970
  System.Web.UI.TemplateControl.Eval(String expression)+34
  ASP.editcorporate_aspx .__ DataBinding__control106(对象发送者,   EventArgs e)在c:\ Users \ user \ Documents \ Visual Studio中   2013 \网站已\ MyWebApp \ EditCustomer.aspx:357个
  System.Web.UI.Control.OnDataBinding(EventArgs e)+92
  System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)+14
  System.Web.UI.WebControls.ListControl.PerformSelect()+34
  System.Web.UI.WebControls.BaseDataBoundControl.DataBind()+30
  System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()+ 105   System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e)+23
  System.Web.UI.Control.PreRenderRecursiveInternal()+83
  System.Web.UI.Control.PreRenderRecursiveInternal()+155
  System.Web.UI.Control.PreRenderRecursiveInternal()+155
  System.Web.UI.Control.PreRenderRecursiveInternal()+155
  System.Web.UI.Control.PreRenderRecursiveInternal()+155
  System.Web.UI.Control.PreRenderRecursiveInternal()+155
  System.Web.UI.Control.PreRenderRecursiveInternal()+155
  System.Web.UI.Control.PreRenderRecursiveInternal()+155
  System.Web.UI.Control.PreRenderRecursiveInternal()+155
  System.Web.UI.Control.PreRenderRecursiveInternal()+155
  System.Web.UI.Control.PreRenderRecursiveInternal()+155
  System.Web.UI.Page.ProcessRequestMain(布尔   includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+974

如果还有其他信息,请告诉我。非常感谢任何帮助。

0 个答案:

没有答案