.NET 对象数据源、DetailsView 和 DropDownList

发布于 2024-08-06 02:50:26 字数 1703 浏览 4 评论 0原文

好吧,我将尝试对我需要完成的任务进行非常简短而详细的描述。

首先,我定义了一个具有员工对象的 LinqToSQL 数据上下文。然后我创建了一个 BLL(员工的一部分)来处理我的验证、插入、更新等。此时一切都很好。接下来,我创建了一个详细信息视图,其中的对象数据源绑定到 Employee 对象上的选择/更新/插入方法。这也很好,数据按应有的方式提取、更新、插入,除了一个问题之外。我在详细信息视图内的下拉列表中遇到问题。这些是必填字段,不能为空。当该约束被删除时,代码就可以正常运行。

详细信息视图的代码片段 - 缩写字段以显示相关信息。

<asp:DetailsView ID="grd_empDetails"  runat="server" DataSourceID="empDataSource" DataKeyNames="EmployeeID" DefaultMode="Insert" AutoGenerateRows="false">
            <Fields>
                <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                <asp:TemplateField HeaderText="Type of Employee">
                    <ItemTemplate>
                        <asp:DropDownList ID="empType" runat="server" DataValueField="empType" AppendDataBoundItems="true">
                            <asp:ListItem Text="Full Time" Value="1" />
                            <asp:ListItem Text="Part Time" Value="2" />
                            <asp:ListItem Text="Subcontractor or 1099" Value="3" />
                            <asp:ListItem Text="Intern" Value="4" />
                        </asp:DropDownList>
                    </ItemTemplate>                   
                </asp:TemplateField>
                <asp:CommandField ShowInsertButton="true" ShowCancelButton="true" />    
            </Fields>
        </asp:DetailsView>

当调用 insert 方法时,empType 始终返回为 Nothing(empType 是数据库字段)。

将下拉列表的值附加到我的对象以便可以在数据库中正确创建它的正确方法是什么?我的大多数搜索一直告诉我如何将下拉列表绑定到对象数据源,但这不是我需要做的。这是一个基本的下拉列表,有 4 个永远不会改变的项目。在这里仅使用标准 HTML 选择标签是否有意义,或者是否有帮助。

感谢您的浏览,如果您需要更多信息,请告诉我。

Ok, I am going to try and give a very bried yet detailed description of what I need to accomplish.

First, I have a LinqToSQL datacontext defined that has an employee object. I then created a BLL (partial of employee) that handles my validations, inserts, updates etc. Everthing is good at this point. Next, I created a detailsview with an object data source binding to my select/update/insert methods on my Employee object. This is good too, data pulls like it should, updates, inserts like it should except for one issue. I am having trouble with drop down list inside of the details view. These are required fields, cannot be null. When that constraint is dropped, the code functions just fine.

Code Snippet of details view - fields abbreviated to show relevant information.

<asp:DetailsView ID="grd_empDetails"  runat="server" DataSourceID="empDataSource" DataKeyNames="EmployeeID" DefaultMode="Insert" AutoGenerateRows="false">
            <Fields>
                <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                <asp:TemplateField HeaderText="Type of Employee">
                    <ItemTemplate>
                        <asp:DropDownList ID="empType" runat="server" DataValueField="empType" AppendDataBoundItems="true">
                            <asp:ListItem Text="Full Time" Value="1" />
                            <asp:ListItem Text="Part Time" Value="2" />
                            <asp:ListItem Text="Subcontractor or 1099" Value="3" />
                            <asp:ListItem Text="Intern" Value="4" />
                        </asp:DropDownList>
                    </ItemTemplate>                   
                </asp:TemplateField>
                <asp:CommandField ShowInsertButton="true" ShowCancelButton="true" />    
            </Fields>
        </asp:DetailsView>

When the insert method is called, the empType is always returned as Nothing (empType is the database field).

What is the proper way to attach the value of the dropdownlist to my object so that it can be properly created in the database? Most of my searches keep telling me how to bind a dropdownlist to an object data source, but that is not what I need to do. This is a basic drop down list, 4 items that will never change. Would it make sense to use just a standard HTML select tag here or would that even help.

Thanks for looking, let me know if you need any more information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提赋 2024-08-13 02:50:27

看起来很简单,但 VS2008 IDE 并没有告诉我这是一个选项。为了绑定到 ObjectDataSource,你必须这样做:

<asp:TemplateField HeaderText="Type of Employee">
                    <ItemTemplate>
                        <asp:DropDownList ID="empType" runat="server" DataValueField="empType" SelectedValue='<%#Bind("empType") %>'>
                            <asp:ListItem Text="Full Time" Value="1" />
                            <asp:ListItem Text="Part Time" Value="2" />
                            <asp:ListItem Text="Subcontractor or 1099" Value="3" />
                            <asp:ListItem Text="Intern" Value="4" />
                        </asp:DropDownList>
                    </ItemTemplate>                   
                </asp:TemplateField>

下拉列表的 SelectedValue 部分没有显示为下拉列表的有效标记,但 VS 在编译或运行时不会阻塞它最重要的是,它有效!

Looks like it was something simple, yet the VS2008 IDE wasn't showing me that this was an option. In order to bind to the ObjectDataSource, you have to do something like this:

<asp:TemplateField HeaderText="Type of Employee">
                    <ItemTemplate>
                        <asp:DropDownList ID="empType" runat="server" DataValueField="empType" SelectedValue='<%#Bind("empType") %>'>
                            <asp:ListItem Text="Full Time" Value="1" />
                            <asp:ListItem Text="Part Time" Value="2" />
                            <asp:ListItem Text="Subcontractor or 1099" Value="3" />
                            <asp:ListItem Text="Intern" Value="4" />
                        </asp:DropDownList>
                    </ItemTemplate>                   
                </asp:TemplateField>

The SelectedValue part of the drop down list was not showing up for me as a valid tag for the dropdownlist, but VS doesn't choke on it at compile or runtime and most importantly, it works!

似梦非梦 2024-08-13 02:50:27

一切看起来都很好,但是当您加载它时,下拉列表是否最初已填充?如果没有,请尝试在加载时将其中一项设置为选中,否则如果用户不选择一项,则可能不会返回任何值。

Everything looks fine, but when you load it up, is the drop down list initially populated? If not, try setting one of the items to selected when it loads up, otherwise it might return no value if the user doesn't select one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文