ASP.Net - DropDownList used in EditItemTemplate in DetailsView

风格不统一 提交于 2019-12-13 07:25:49

问题


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


来源:https://stackoverflow.com/questions/18571149/asp-net-dropdownlist-used-in-edititemtemplate-in-detailsview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!