开发者

How do I insert Data Using the EntityDataSource control?

On my page there are textboxes which I us开发者_如何学运维e for input. How can I wire those textboxes to an EntityDataSource control?


Instead of using plain text boxes with your EntityDataSource control, I would recommend using a GridView control with a corresponding DetailsView control. The EntityDataSource control was designed to work nicely with these two controls. The DetailsView control will be the one which is actually responsible for the inserting and updating of your data.

When you are configuring the EntityDataSource controls (you will need two, one for the GridView and one for the DetailsView), make sure to check the 'Enable automatic inserts,' 'Enable automatic updates,' and 'Enable automatic deletes' check boxes in the 'Configure Data Selection' screen of the setup wizard. In your case, since you are only interested in inserting the data, you can just check the 'Enable automatic inserts' checkbox.

You can also just use the DetailsView by itself, and leave the GridView out. To do this, you'll need to configure the DetailsView's EntityDataSoruce 'Expression Editor' for the 'Where' clause a bit differently (found in the properties list of the EntityDataSource control).

It's important to point out that the GridView and DetailsView are pretty heavy controls. Unless you are building a small site (with few users) or prototyping a planned site project, I would recommend revising your development strategy and possibly going with raw ADO.Net instead.

Here is a simple example:

      <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
                ConnectionString="name=SomeDatabaseEntities" 
                DefaultContainerName="SomeDatabaseEntities"
                EnableFlattening="False" 
                EntitySetName="Links" EnableDelete="True" EnableInsert="True" 
                EnableUpdate="True">
      </asp:EntityDataSource>
      <asp:GridView ID="GridView1" runat="server" CellPadding="4" 
                ForeColor="#333333" 
                GridLines="None" AllowPaging="True" AllowSorting="True"
                EnableViewState="false"
                DataSourceID="EntityDataSource1" 
                AutoGenerateColumns="False" PageSize="20" 
                DataKeyNames="LinkId"> 
                <Columns>
                <asp:CommandField ShowDeleteButton="True" 
                          ShowSelectButton="True" />
                <asp:BoundField DataField="LinkId" HeaderText="LinkId" 
                          SortExpression="LinkId" ReadOnly="True" />
                <asp:BoundField DataField="Title" HeaderText="Title"
                          SortExpression="Title" />
                <asp:BoundField DataField="URL" HeaderText="URL"
                          SortExpression="URL" />
                <asp:BoundField DataField="ImagePath" HeaderText="ImagePath" 
                          SortExpression="ImagePath" />
                </Columns>
      </asp:GridView>
      <br />
      <asp:EntityDataSource ID="EntityDataSource2" runat="server" 
                ConnectionString="name=SomeDatabaseEntities" 
                DefaultContainerName="SomeDatabaseEntities" 
                EnableFlattening="False" 
                EntitySetName="Links" Where="it.LinkId == @LinkId" 
                          EnableInsert="True" 
                EnableUpdate="True">
                <WhereParameters>
                          <asp:ControlParameter ControlID="GridView1" 
                                   DbType="Int32" Name="LinkId" 
                                   PropertyName="SelectedValue" />
                </WhereParameters>
      </asp:EntityDataSource>
      <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" 
                Width="125px" 
                CellPadding="4" ForeColor="#333333" 
                GridLines="None" DataKeyNames="LinkId" 
                AutoGenerateRows="False" 
                DataSourceID="EntityDataSource2">
                <Fields> 
                          <asp:BoundField DataField="LinkId" 
                                   HeaderText="LinkId" 
                                   ReadOnly="true" InsertVisible="false" 
                                   SortExpression="LinkId" />
                          <asp:BoundField DataField="Title" 
                                   HeaderText="Title" 
                                   SortExpression="Title" />
                          <asp:BoundField DataField="URL" HeaderText="URL"
                                   SortExpression="URL" />
                          <asp:BoundField DataField="ImagePath" 
                                   HeaderText="ImagePath" 
                                   SortExpression="ImagePath" />
                          <asp:CommandField ShowEditButton="True"
                                   ShowInsertButton="True" />
                </Fields>
      </asp:DetailsView>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜