2014-02-09 138 views
0

我想建立一个管理页面,以便让用户创建编辑更新特定表的记录。 “区”表具有以下属性:区域ID,区域名称,说明,区域图像,CityFK。由于CityFK只是表示城市表的CityID的数字,并不代表用户的任何内容,所以我已经使用了下拉菜单,并且它将城市名称显示为“DataTextField”,CityIDs显示为表“城市”中的“DataValueField” ”。但我的目标是将这个“DataValueField”作为CityFK插入到区域表中。但是,每当我输入一个新的区记录并从下拉列表中选择一个城市并按列表视图的“插入”按钮时,关于记录的CityFK就是“0”。ASP.net listview下拉列表项获取从另一个表的值

我粘贴了整个列表视图,但是如果你看看InsertItemTemplate或EditItemTemplate的下拉列表,你会明白我的意思;

<asp:ListView ID="DistrictList" runat="server" DataKeyNames="DistrictID" InsertItemPosition="LastItem" DataSourceID="DistrictEntityDataSource" > 
    <AlternatingItemTemplate> 
     <tr style="background-color:#FFF8DC;"> 
      <td> 
       <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" /> 
       <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" /> 
      </td> 
      <td> 
       <asp:Label ID="DistrictIDLabel" runat="server" Text='<%# Eval("DistrictID") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="DistrictNameLabel" runat="server" Text='<%# Eval("DistrictName") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' /> 
      </td> 
      <td> 
       <asp:Image ID="DistrictImageLabel" runat="server" Height="100px" ImageUrl='<%# "~/Handlers/ImageHandler.ashx?ID="+Eval("DistrictID")+"&Entity=District"%>'/> 
      </td> 
      <td> 
       <asp:Label ID="CityFKLabel" runat="server" Text='<%# Eval("CityFK") %>' /> 
      </td> 
     </tr> 
    </AlternatingItemTemplate> 
    <EditItemTemplate> 
     <tr style="background-color:#008A8C;color: #FFFFFF;"> 
      <td> 
       <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" /> 
       <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" /> 
      </td> 
      <td> 
       <asp:Label ID="DistrictIDLabel1" runat="server" Text='<%# Eval("DistrictID") %>' /> 
      </td> 
      <td> 
       <asp:TextBox ID="DistrictNameTextBox" runat="server" Text='<%# Bind("DistrictName") %>' /> 
      </td> 
      <td> 
       <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' /> 
      </td> 
      <td> 
       <asp:FileUpload ID="FileUploadDistrict2" runat="server" /> 

      </td> 
      <td> 
       <asp:DropDownList ID="CityFKDropDownList" runat="server" AutoPostBack="true" DataSourceID="ddlCityFK_DataSource" ViewStateMode="Enabled" 
        DataTextField="CityName" DataValueField="CityID" 
        AppendDataBoundItems="true"> 
        <asp:ListItem Text="-Stadt Wählen-" Value="0" ></asp:ListItem> 
       </asp:DropDownList> 

       <asp:EntityDataSource ID="ddlCityFK_DataSource" runat="server" 
        ConnectionString="name=MedicalEntities" DefaultContainerName="MedicalEntities" 
        EntitySetName="Cities" > 
       </asp:EntityDataSource> 
      </td> 

     </tr> 
    </EditItemTemplate> 
    <EmptyDataTemplate> 
     <table runat="server" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;"> 
      <tr> 
       <td>No data was returned.</td> 
      </tr> 
     </table> 
    </EmptyDataTemplate> 
    <InsertItemTemplate> 
     <tr style=""> 
      <td> 
       <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" /> 
       <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" /> 
      </td> 
      <td> 
       <asp:TextBox ID="DistrictIDTextBox" runat="server" Text='<%# Bind("DistrictID") %>' /> 
      </td> 
      <td> 
       <asp:TextBox ID="DistrictNameTextBox" runat="server" Text='<%# Bind("DistrictName") %>' /> 
      </td> 
      <td> 
       <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' /> 
      </td> 
      <td> 
       <asp:FileUpload ID="FileUploadDistrict" runat="server"/> 

      </td> 
      <td> 
       <asp:DropDownList ID="CityFKDropDownList" runat="server" AutoPostBack="true" DataSourceID="ddlCityFK_DataSource" ViewStateMode="Enabled" 
        DataTextField="CityName" DataValueField="CityID" 
        AppendDataBoundItems="true"> 
        <asp:ListItem Text="-Stadt Wählen-" Value="0" ></asp:ListItem> 
       </asp:DropDownList> 

       <asp:EntityDataSource ID="ddlCityFK_DataSource" runat="server" 
        ConnectionString="name=MedicalEntities" DefaultContainerName="MedicalEntities" 
        EntitySetName="Cities"> 
       </asp:EntityDataSource> 
      </td> 
     </tr> 
    </InsertItemTemplate> 
    <ItemTemplate> 
     <tr style="background-color:#DCDCDC;color: #000000;"> 
      <td> 
       <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" /> 
       <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" /> 
      </td> 
      <td> 
       <asp:Label ID="DistrictIDLabel" runat="server" Text='<%# Eval("DistrictID") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="DistrictNameLabel" runat="server" Text='<%# Eval("DistrictName") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' /> 
      </td> 
      <td> 
       <asp:Image ID="DistrictImageLabel" runat="server" Height="100px" ImageUrl='<%# "~/Handlers/ImageHandler.ashx?ID="+Eval("DistrictID")+"&Entity=District"%>'/> 
      </td> 
      <td> 
       <asp:Label ID="CityFKLabel" runat="server" Text='<%# Eval("CityFK") %>' /> 
      </td> 
     </tr> 
    </ItemTemplate> 
    <LayoutTemplate> 
     <table runat="server"> 
      <tr runat="server"> 
       <td runat="server"> 
        <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;"> 
         <tr runat="server" style="background-color:#DCDCDC;color: #000000;"> 
          <th runat="server"></th> 
          <th runat="server">DistrictID</th> 
          <th runat="server">DistrictName</th> 
          <th runat="server">Description</th> 
          <th runat="server">DistrictImage</th> 
          <th runat="server">CityFK</th> 
         </tr> 
         <tr id="itemPlaceholder" runat="server"> 
         </tr> 
        </table> 
       </td> 
      </tr> 
      <tr runat="server"> 
       <td runat="server" style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;"> 
        <asp:DataPager ID="DataPager1" runat="server"> 
         <Fields> 
          <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" /> 
         </Fields> 
        </asp:DataPager> 
       </td> 
      </tr> 
     </table> 
    </LayoutTemplate> 
    <SelectedItemTemplate> 
     <tr style="background-color:#008A8C;font-weight: bold;color: #FFFFFF;"> 
      <td> 
       <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" /> 
       <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" /> 
      </td> 
      <td> 
       <asp:Label ID="DistrictIDLabel" runat="server" Text='<%# Eval("DistrictID") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="DistrictNameLabel" runat="server" Text='<%# Eval("DistrictName") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' /> 
      </td> 
      <td> 
       <asp:Image ID="DistrictImageLabel" runat="server" Height="100px" ImageUrl='<%# "~/Handlers/ImageHandler.ashx?ID="+Eval("DistrictID")+"&Entity=District"%>'/> 
      </td> 
      <td> 
       <asp:Label ID="CityFKLabel" runat="server" Text='<%# Eval("CityFK") %>' /> 
      </td> 
     </tr> 
    </SelectedItemTemplate> 
</asp:ListView> 

<asp:EntityDataSource ID="DistrictEntityDataSource" runat="server" ConnectionString="name=MedicalEntities" 
    DefaultContainerName="MedicalEntities" EnableDelete="True" EnableFlattening="False" EnableInsert="True" 
    EnableUpdate="True" EntitySetName="Districts" Include="City" EntityTypeFilter="District"> 
</asp:EntityDataSource> 

因此,我试图在插入和编辑操作期间将该下拉列表值绑定到CityFK。

回答

0

我想通了。作为解决方法,我在下拉列表的同一列添加了一个文本框,并将其可见属性设置为false;

<asp:TextBox ID="CityFKTextBoxInsert" runat="server" Visible="false" Text='<%# Bind("CityFK") %>' /> 

如果你也遇到这样的问题,我在这里添加代码隐藏和asp端;

 <InsertItemTemplate> 
     <tr style=""> 
      <td> 
       <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" /> 
       <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" /> 
      </td> 
      <td> 
       <asp:TextBox ID="DistrictIDTextBox" runat="server" Text='<%# Bind("DistrictID") %>' /> 
      </td> 
      <td> 
       <asp:TextBox ID="DistrictNameTextBox" runat="server" Text='<%# Bind("DistrictName") %>' /> 
      </td> 
      <td> 
       <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' /> 
      </td> 
      <td> 
       <asp:FileUpload ID="FileUploadDistrictInsert" runat="server" /> 

      </td> 
      <td> 

       <asp:TextBox ID="CityFKTextBoxInsert" runat="server" Visible="false" Text='<%# Bind("CityFK") %>' /> 
       <asp:DropDownList ID="CityFKDropDownListInsert" runat="server" AutoPostBack="true" DataSourceID="ddlCityFK_DataSource2" ViewStateMode="Enabled" 
        DataTextField="CityName" DataValueField="CityID" OnSelectedIndexChanged="CityFKDropDownListInsert_SelectedIndexChanged" 
        AppendDataBoundItems="true"> 
        <asp:ListItem Text="-Stadt Wählen-" Value="0" ></asp:ListItem> 
       </asp:DropDownList> 

       <asp:EntityDataSource ID="ddlCityFK_DataSource2" runat="server" 
        ConnectionString="name=MedicalEntities" DefaultContainerName="MedicalEntities" 
        EntitySetName="Cities" > 
       </asp:EntityDataSource> 

      </td> 
     </tr> 
    </InsertItemTemplate> 

然后我添加了一个“OnSelectedIndexChanged”事件向下拉,并分配所选择的值到该文本框;

protected void CityFKDropDownListInsert_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DropDownList DropdownDistrict = (DropDownList)DistrictList.InsertItem.FindControl("CityFKDropDownListInsert"); 
     TextBox DistrictTextBox = (TextBox)DistrictList.InsertItem.FindControl("CityFKTextBoxInsert"); 

     DistrictTextBox.Text = DropdownDistrict.SelectedValue; 
    } 

如果您想将该解决方案也应用于EditItemTemplate,那么您必须更改该行;

DropDownList DropdownDistrict = (DropDownList)DistrictList.InsertItem.FindControl("CityFKDropDownListInsert"); 

to this;

DropDownList DropdownDistrict = (DropDownList)DistrictList.EditItem.FindControl("CityFKDropDownListInsert"); 
相关问题