ASP.Net - 在DetailsView控件中使用EditItemTemplate中的DropDownList [英] ASP.Net - DropDownList used in EditItemTemplate in DetailsView

查看:183
本文介绍了ASP.Net - 在DetailsView控件中使用EditItemTemplate中的DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的 DetailsView控件的一个问题。我需要databaze使用的的DropDownList 的进行更新值。这些值我有一个不同的表。我用一个ObjectDataSource,它工作正常。我的问题是如何使用DropDownList的时候,我不能绑定的的SelectedValue 的在设计,因为缺少。我发现很多建议,比如

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") %>'/>

但正如我写的SelectedValue属性没有在设计允许的。我正在寻找另一种方式的的SelectedValue ='&LT;%#绑定(PermissionID)%>。的,因为我想用DropDownList的SelectedValue属性为parametr为ObjectDataSource UpdateMethod

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.

我的更新方法:

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

下面是我的DetailsVeiw:

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>

数据库表:

用户


  • USER_ID - INT

  • PERMISSION_ID - INT

  • USER_NAME_A - 为nvarchar(20)

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

权限


  • PERMISSION_ID - INT

  • PERMISSION_NAME_A - 为nvarchar(20)

  • PERMISSION_ID - int
  • PERMISSION_NAME_A - nvarchar(20)

我使用VS2012 .Net框架4.5 ...
所以,任何人都可以帮我吗?

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

推荐答案

解决......这是非常简单的。谢谢你的时间;)

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

我添加键值到的DetailView在ItemUpdating事件的关键。

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

这篇关于ASP.Net - 在DetailsView控件中使用EditItemTemplate中的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆