0

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?

Jenda Matejicek
  • 151
  • 3
  • 14
  • can you please elaborate your question more so that others can help you better. What actually you are trying to do. – Harish Bhatt Sep 02 '13 at 11:02
  • @HarishBhatt I tried to be more specific. English is not my best skill :) – Jenda Matejicek Sep 02 '13 at 11:20
  • i have posted my answer as per my understanding below, let me know is it working for you. – Harish Bhatt Sep 02 '13 at 11:37
  • you need to get the reference of DropdownList in order to select value by below method, for this you can use below code: `DropdownList ddList = (DropdownList)DetailsView2.Rows[i].FindControl("ddlEditPermissions");` now you can use DropdownList object to select to deselect value using below method. – Harish Bhatt Sep 02 '13 at 11:44
  • @HarishBhatt Thx for your time... I try to be little bit more specific. I wanna use that DropDownList SelectedValue property as parametr for the ObjectDataSource UpdateMethod. – Jenda Matejicek Sep 02 '13 at 12:54
  • I would suggest reading this: [Tutorial: Entity Data Source Control](http://blogs.msdn.com/b/adonet/archive/2008/06/18/tutorial-entity-data-source-control.aspx). You will find an implementation [Here](http://stackoverflow.com/a/18561613/690329). – afzalulh Sep 02 '13 at 16:19
  • I think you need to manipulate it in `onDataBound` event of DetailsView – Harish Bhatt Sep 03 '13 at 08:49

2 Answers2

0

This is how you can select DropdownList values:

if (ddlList.Items.FindByValue(<string type value you want to select from dropdownlist>) != null)
{
cboRegion.SelectedValue = ""; //Value you want to select if you are using Value attribute to select
}

Suppose you want to select vaulue "India" from drop down then it will be:

if (ddlList.Items.FindByValue("India") != null)
{
cboRegion.SelectedValue = "India";
}

or

string strValue = ddlList.Items.FindByValue("India").ToString();
if (strValue  != null)
{
cboRegion.SelectedValue = strValue ;
}

you can use FindByValue or FindByText methods to find values from DropdownList as below:

if (ddlList.Items.FindByText(<string type value you want to select from dropdownlist>) != null)
{
cboRegion.SelectedText = ""; //Value you want to select if you are using Value attribute to select
}
Harish Bhatt
  • 584
  • 6
  • 15
0

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;                
        }
Jenda Matejicek
  • 151
  • 3
  • 14
  • For SelectedValue='<%# Bind("PermissionID") %>' did you call DataBind() for DropDownList? – Emad Mokhtar Sep 03 '13 at 11:08
  • There is a problem with `SelectedValue='<%# Bind("PermissionID") %>'`. It's not available in page design code as a DropDownList's property and I really don't know why. So I had to find a different way. And it is that simple. – Jenda Matejicek Sep 04 '13 at 11:04