How to pass DropDownList SelectedItem into the EntityDataSource Select Attribute

耗尽温柔 提交于 2020-01-16 09:35:10

问题


I am trying to pass the selected value of ddlCity dropdownlist into the EntityDataSource2 CommandText where "WHERE p.city = @city" but I get the following error:

WhereParameters cannot be specified unless AutoGenerateWhere==true or Where is specified.

<tr id="SearchDropDown">     
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=medicaldb2Entities" 
    DefaultContainerName="medicaldb2Entities" EnableFlattening="False" 
    EntitySetName="Medicals" Select="it.[medicalName], it.[city], it.[Region]">
</asp:EntityDataSource>    
<td>
<td>
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlCity_SelectedIndexChanged"
    DataSourceID="EntityDataSource1" DataTextField="city" 
    DataValueField="city">
</asp:DropDownList>
</td>
</tr>
</table>         

<asp:ListView ID="ListView1" runat="server" DataSourceID="EntityDataSource2">
<LayoutTemplate>
            <table>
                <tr>    
                    <td>
                        City
                    </td>

                </tr>
                <asp:PlaceHolder ID="ItemPlaceHolder" runat="server"></asp:PlaceHolder>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <%# Eval("city") %>
                </td>
            </tr>
        </ItemTemplate>

        <EmptyDataTemplate>
            Not Found...
        </EmptyDataTemplate>

</asp:ListView>

<asp:EntityDataSource ID="EntityDataSource2" runat="server" 
    CommandText="SELECT p.MedicalID, p.medicalName, p.city, p.Region, p.description, p.Image,     p.adress
          FROM Medicals AS p WHERE p.city = @city"
          AutoGenerateWhereClause="False"
    ConnectionString="name=medicaldb2Entities" 
    DefaultContainerName="medicaldb2Entities">

 <WhereParameters>
    <asp:ControlParameter ControlID="ddlCity" DbType="String" 
      DefaultValue="2500" Name="city" PropertyName="SelectedValue"
    ConvertEmptyStringToNull="true" />
  </WhereParameters>
 </asp:EntityDataSource>

Any help would be appreciated


回答1:


You can use

<asp:EntityDataSource
....
    <WhereParameters>
        <asp:ControlParameter ControlID="costLimit" DbType="Int32" 
          DefaultValue="2500" Name="ordercost" PropertyName="SelectedValue"
        ConvertEmptyStringToNull="true" />
      </WhereParameters>
.....
</asp:EntityDataSource>



回答2:


This is the correct version in order to pass the dropdownlist selectedvalue in the Select attribute of EntityDataSource.

    <tr id="SearchDropDown">     
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=medicaldb2Entities" 
    DefaultContainerName="medicaldb2Entities" EnableFlattening="False" 
    EntitySetName="Medicals" Select="it.[medicalName], it.[city], it.[Region]">
</asp:EntityDataSource>    
<td>
<td>
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlCity_SelectedIndexChanged"
    DataSourceID="EntityDataSource1" DataTextField="city" 
    DataValueField="city">
</asp:DropDownList>
</td>
</tr>
</table>



<asp:ListView ID="ListView1" runat="server" DataSourceID="EntityDataSource2">

<LayoutTemplate>
            <table>
                <tr>
                    <td>
                        City
                    </td>

                </tr>
                <asp:PlaceHolder ID="ItemPlaceHolder" runat="server"></asp:PlaceHolder>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <%# Eval("city") %>
                </td>
            </tr>
        </ItemTemplate>

        <EmptyDataTemplate>
            not found...
        </EmptyDataTemplate>

</asp:ListView>

 <asp:EntityDataSource ID="EntityDataSource2" runat="server" 
 ConnectionString="name=medicaldb2Entities" 
    DefaultContainerName="medicaldb2Entities" 
EntitySetName="Medicals" 
   Select= "it.[MedicalID], it.[medicalName], it.[city], it.[Region], it.[description], it.[Image], it.[adress]"
    Where = "it.[city] = @city">

    <WhereParameters> 
    <asp:ControlParameter ControlID="ddlCity" DbType="String" 
      DefaultValue="2500" Name="city" PropertyName="SelectedValue"
    ConvertEmptyStringToNull="true" />
  </WhereParameters>

</asp:EntityDataSource>


来源:https://stackoverflow.com/questions/18560761/how-to-pass-dropdownlist-selecteditem-into-the-entitydatasource-select-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!