2014-02-09 2 views
0

Я пытаюсь создать страницу администратора, чтобы позволить пользователю создавать редактирование обновлять записи конкретной таблицы. Таблица «Район» имеет следующие свойства: DistrictID, DistrictName, Описание, DistrictImage, CityFK. Поскольку CityFK - это просто цифры, которые представляют CityID таблицы «Город» и ничего не значит для пользователя, я использовал раскрывающееся меню и отображает все названия городов как «DataTextField» и CityID как «DataValueField» из таблицы «Город» ». Но моя цель - вставить этот «DataValueField» в таблицу District как CityFK. Но всякий раз, когда я вхожу в новую запись округа и выбираю «Город» из раскрывающегося списка и нажимаю кнопку «Вставить» в списке, CityFK этого в отношении записи «0».ASP.NET listview dropdown item получение значения из другой таблицы

Я вставляю весь список, но если вы посмотрите на раскрывающийся список на вкладке 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"); 

к этому;

DropDownList DropdownDistrict = (DropDownList)DistrictList.EditItem.FindControl("CityFKDropDownListInsert"); 
Смежные вопросы