ASP.Net - DropDownList used in EditItemTemplate in

2019-08-28 17:01发布

问题:

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?

回答1:

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
}


回答2:

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