C# 在ASP.Net Formview中使用DropDownList作为分页函数

C# 在ASP.Net Formview中使用DropDownList作为分页函数,c#,asp.net,sql,drop-down-menu,formview,C#,Asp.net,Sql,Drop Down Menu,Formview,我有一个Formview,当选择下拉列表时,它会填充SQL数据,但当我单击“编辑”时,它希望编辑下拉列表中的第一项,因为从技术上讲,它仍然认为它位于第一页。我希望分页链接到下拉列表,但我不确定如何实现这一点。我有很多字段,所以我不会发布完整的Formview,但这里是关键部分 Formview项目模板: <ItemTemplate> <table id="FormTable"> <tr><th>

我有一个Formview,当选择下拉列表时,它会填充SQL数据,但当我单击“编辑”时,它希望编辑下拉列表中的第一项,因为从技术上讲,它仍然认为它位于第一页。我希望分页链接到下拉列表,但我不确定如何实现这一点。我有很多字段,所以我不会发布完整的Formview,但这里是关键部分

Formview项目模板:

<ItemTemplate>
          <table id="FormTable">
          <tr><th>
        <asp:DropDownList ID="ProjectNameDropDown" runat="server" AutoPostBack="true"  
        DataSourceID="SqlDataSource1" 
        DataValueField="Project_Name" name="Text" OnSelectedIndexChanged="ProjectSelect" AppendDataBoundItems="true">              
        <asp:ListItem Text="Select a Project" />
        </asp:DropDownList>
        <asp:Panel ID="Panel1" runat="server" Visible="false">             
          </th>
          <th>
            <asp:Button ID="EditButton" runat="server" CausesValidation="False" 
                CommandName="Edit" Text="Edit" />
            &nbsp;<asp:Button ID="DeleteButton" runat="server" CausesValidation="False" 
                CommandName="Delete" Text="Delete" />
            &nbsp;<asp:Button ID="NewButton" runat="server" CausesValidation="False" 
                CommandName="New" Text="New" />                 
          </th></tr>            
           <tr><th>                
            Business Category:
          </th><td>
            <asp:Label ID="BusinessCategoryLabel" runat="server" 
                Text='<%# Bind("Business_Category") %>'
                 />
            </td></tr>
          <tr><th>
            Project Description:
          </th><td>
            <asp:TextBox ID="ProjectDescriptionLabel" runat="server" ReadOnly="true"
                Text='<%# Bind("Project_Description") %>' TextMode="MultiLine" Rows="5" />
            </td></tr>
          <tr><th>
            Operations Owner:
          </th><td>
            <asp:Label ID="OwnerLabel" runat="server" 
                Text='<%# Bind("Operations_Owner") %>' />
            </td></tr>
我想象着在从下拉列表中选择时,会告诉Formview要切换到哪个页面,但我还没有弄清楚该页面的编码。谢谢你的帮助

一种方法(我过去的做法)是将数据绑定
DropDownList
移动到
FormView
之外。然后,将您的
FormView
绑定到另一个
SQLDataSource
,它依赖于
DropDownList
s SelectedValue。因此,您将拥有一个包含所有项目名称的DDL:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    DataSourceID="SqlDataSource1" DataTextField="Project_Name" 
    DataValueField="Project_Name">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="Your Connection String" 
    ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT DISTINCT [Project_Name] FROM [ProjectTable]">
</asp:SqlDataSource>
这样,当您在
窗体视图中单击“编辑”按钮时,您正在编辑您想要编辑的记录


顺便说一句,您的代码落后使您非常容易受到SQL注入的影响。我会小心的。您不应该使用字符串连接来生成SELECT查询,而应该使用参数化查询

实际上,除了在此处作为注释之外,您还需要告诉原始海报哪些其他选项?效果很好。我甚至不需要代码来填充字段。再次感谢你的帮助!我正计划让MIS修复我所有的安全漏洞…=)@克里斯哈哈,太棒了。很高兴我能帮忙!
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    DataSourceID="SqlDataSource1" DataTextField="Project_Name" 
    DataValueField="Project_Name">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="Your Connection String" 
    ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT DISTINCT [Project_Name] FROM [ProjectTable]">
</asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" AllowPaging="True" 
    DataKeyNames="Project_Name" DataSourceID="SqlDataSource2">
...
</asp:FormView>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="Your Connection String" 
    ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT * FROM [ProjectTable] WHERE Project_Name=@Project_Name">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" Name="Project_Name" 
            PropertyName="SelectedValue" />
    </SelectParameters>
</asp:SqlDataSource>