Pergunta

I am trying to build an admin page in order to let the user create edit update the records of the specific table. "District" table has the following properties: DistrictID, DistrictName, Description, DistrictImage, CityFK. Since CityFK is just numbers which represents CityID of the table "City" and does not mean anything to the user I have used a dropdown menu instead and it displays all city names as "DataTextField" and CityIDs as "DataValueField" from the table "City". But my aim is to insert that "DataValueField" into the District table as CityFK. But whenever I enter a new District record and choose a City from the dropdown and press "Insert" button of the listview, CityFK of that regarding record is "0".

I paste the whole listview but if you look at the dropdownlist at the InsertItemTemplate or EditItemTemplate you will what I mean;

 <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>

So I am trying to bind that dropdownlist values to the CityFK during Insert and Edit operations.

Foi útil?

Solução

I figured it out. As workaround I added one textbox at the same column with the dropdownlist and set its visible attribute to false;

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

In case you also encounter such a problem I add the code-behind and asp side here;

        <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>

Then I have added a "OnSelectedIndexChanged" event to the dropdown and assign the selected value to that textbox;

 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;
    }

If you want to apply that solution also to EditItemTemplate only thing you have to do changing that line;

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

to this;

DropDownList DropdownDistrict = (DropDownList)DistrictList.EditItem.FindControl("CityFKDropDownListInsert");
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top