Search code examples
asp.netdetailsviewedititemtemplate

ASP.Net - DropDownList used in EditItemTemplate in DetailsView


I have a problem with DetailsView. I need to use DropDownList for update values in databaze. These values I've got in a different table. I use an ObjectDataSource and it works properly. My problem is how to use the DropDownList when I cannot bind the SelectedValue in the designer because it's missing. I found many advices like

<asp:DropDownList ID="ddlEditPermissions" runat="server" DataSource='<%#getPermissions() %>' SelectedValue='<%# Bind("PermissionID") %>'/>

But as I wrote the SelectedValue property is not allowed in designer. I'm looking for another way to SelectedValue='<%# Bind("PermissionID") %>' because I wanna use DropDownList SelectedValue property as parametr for the ObjectDataSource UpdateMethod.

My update method:

    public static void UpdateUser(int UserId, string UserName, int PermissionID)
    {
        using (DC_databazeDataContext db = new DC_databazeDataContext())
        {
            if (!db.DatabaseExists())
                throw new Exception("Databáze neexistuje nebo se k ní nedá připojit!");

            var users = db.USERs.Where(u => u.USER_ID == UserId);

            if (users.Count() == 0) return;

            USER aktU = users.First();

            aktU.USER_NAME_A = UserName;
            aktU.OPRAVNENI_ID = PermissionID;

            db.SubmitChanges();
        }
    }

Here is my DetailsVeiw:

<asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="False" DataSourceID="ODS_UzivatelDetail" DataKeyNames="UserId">
    <Fields>
        <asp:BoundField DataField="UserId" HeaderText="UserId" SortExpression="UserId" ReadOnly="true" />
        <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
        <asp:TemplateField HeaderText="PermissionID" SortExpression="PermissionID">
            <EditItemTemplate>
                <asp:DropDownList ID="ddlEditPermissions" runat="server" DataSource='<%# getPermissions() %>'/>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lPermissions" runat="server" Text='<%# Bind("PermissionID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
    </Fields>
</asp:DetailsView>

DB tables:

Users

  • USER_ID - int
  • PERMISSION_ID - int
  • USER_NAME_A - nvarchar(20)

Permissions

  • PERMISSION_ID - int
  • PERMISSION_NAME_A - nvarchar(20)

I'm using VS2012 .Net Framework 4.5... So could anyone help me?


Solution

  • Solved... It was really simple. Thank you for your time ;)

    I add the key into the keys in ItemUpdating event in DetailView.

    protected void dtlvUserDetail_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
            {
                DropDownList ddlEditPermissions= (DropDownList)dtlvUzivatel.FindControl("ddlEditPermissions");
                e.Keys["permission"] = ddlEditPermissions.SelectedValue;                
            }