详细信息视图给我带来了一些问题,因为我使用的是自定义业务逻辑层而不是 SQLDataSource

发布于 2024-12-04 03:10:51 字数 8134 浏览 2 评论 0原文

我有一个详细信息视图,它在后面的代码中使用我的业务逻辑层。我仍然以声明方式使用 DetailsView,就像在页面上放置标签控件一样,但我没有在 XHTML 的属性部分中声明 datasourceID。

在网上进行大量搜索后,我注意到大多数示例都以声明方式使用 SqlDataSource 并将 id 分配给 DetailsView datarouceID 属性。

我不想让 UI 直接连接到数据逻辑层。我有一个 BLL,它执行所有业务结束事务并将对象传递到 UI。

这就是我的详细信息视图的样子。

   <asp:DetailsView ID="dvCapability" 
                               runat="server" 
                               Width="90%" AutoGenerateEditButton="true"
                               AutoGenerateRows="False" 
                               EmptyDataText="Unable to retrieve the capability details." 
                               BorderColor="LightGray" 
                               BorderStyle="Solid" 
                               BorderWidth="1px" 
                               DataKeyNames="IAID" >
                <AlternatingRowStyle CssClass="altrow" />
                <EmptyDataRowStyle HorizontalAlign="Center" />
                <Fields>
                  <asp:TemplateField HeaderText="Status:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblLabStatus" runat="server" Text='<%# Eval("LabStatus") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:DropDownList ID="ddLabStatus" runat="server" Width="200px" BackColor="White" Font-Size="10px" SelectedValue='<%# Bind("LabStatus") %>' />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Type:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblLabType" runat="server" Text='<%# Eval("LabType") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbLabType" runat="server" Text='<%# Bind("LabType") %>' Width="200px" BackColor="White" Enabled="false" Font-Size="10px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="UIC:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblUIC" runat="server" Text='<%# Eval("UIC") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:DropDownList ID="ddUIC" runat="server" Width="200px" BackColor="White" Font-Size="10px" SelectedValue='<%# Bind("UIC") %>' />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Org code:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblOrgCode" runat="server" Text='<%# Eval("OrgCode") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:DropDownList ID="ddOrgCode" runat="server" Width="200px" BackColor="White" Font-Size="10px" SelectedValue='<%# Bind("OrgCode") %>' />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="VSYSCOM:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblVSYSCOM" runat="server" Text='<%# Eval("VSYSCOM") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbVSYSCOM" runat="server" Text='<%# Bind("VSYSCOM") %>' Width="200px" BackColor="White" Font-Size="10px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Name:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblLabName" runat="server" Text='<%# Eval("LabName") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbLabName" runat="server" Text='<%# Bind("LabName") %>' Width="500px" BackColor="White" Font-Size="10px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Description:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblLabDescription" runat="server" Text='<%# Eval("LabDescription") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbLabDescription" runat="server" Text='<%# Bind("LabDescription") %>' TextMode="MultiLine" Width="500px" Height="75px" BackColor="White" Font-Size="11px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Technical Capability:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblTechnicalCapability" runat="server" Text='<%# Eval("TechnicalCapability") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbTechnicalCapability" runat="server" Text='<%# Bind("TechnicalCapability") %>' TextMode="MultiLine" Width="500px" Height="75px" BackColor="White" Font-Size="11px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Facility:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblFacility" runat="server" Text='<%# Eval("Facility") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbFacility" runat="server" Text='<%# Bind("Facility") %>' TextMode="MultiLine" Width="500px" Height="75px" BackColor="White" Font-Size="11px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Notes:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblComments" runat="server" Text='<%# Eval("Comments") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbComments" runat="server" Text='<%# Bind("Comments") %>' TextMode="MultiLine" Width="500px" Height="75px" BackColor="White" Font-Size="11px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:CommandField ShowEditButton="true" />
                </Fields>
                <FooterStyle CssClass="footer" />
                <HeaderStyle Width="15%" />
                <PagerStyle CssClass="footer" />
                <RowStyle CssClass="row" />
              </asp:DetailsView>

我将它绑定到从页面代码隐藏中的 BLL 返回的 DataTable,如下所示。

 Utilities.BindCompositeControl(dvCapability, Controller.GetCapability(getIaid()));

BindCompositeControl() 方法位于一个静态类中,我用它来减少冗余编码。该方法的编码如下:

public static void BindCompositeControl(CompositeDataBoundControl control, DataTable values)
        {
            control.DataSource = values;
            control.DataBind();
        }

但是,当我尝试使用由DetailsView 控件生成的编辑或插入按钮时,什么也没有发生。为了以编程方式绑定此控件并更改其在 UI 上的操作状态,我缺少什么?

I have a DetailsView that uses my Business Logic Layer in the code behind. I still use the DetailsView declaratively the same as if you drop a label control on the page but I don't declare a datasourceID in the attributes section of the XHTML.

After extensive searching on the web I noticed that most examples use a SqlDataSource declaratively and assign the id to the DetailsView datarouceID attribute.

I don't want to have the UI directly connected to the Data Logic Layer. I have a BLL that does all the busines end transactions and passes objects to the UI.

This is what my DetailsView looks like.

   <asp:DetailsView ID="dvCapability" 
                               runat="server" 
                               Width="90%" AutoGenerateEditButton="true"
                               AutoGenerateRows="False" 
                               EmptyDataText="Unable to retrieve the capability details." 
                               BorderColor="LightGray" 
                               BorderStyle="Solid" 
                               BorderWidth="1px" 
                               DataKeyNames="IAID" >
                <AlternatingRowStyle CssClass="altrow" />
                <EmptyDataRowStyle HorizontalAlign="Center" />
                <Fields>
                  <asp:TemplateField HeaderText="Status:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblLabStatus" runat="server" Text='<%# Eval("LabStatus") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:DropDownList ID="ddLabStatus" runat="server" Width="200px" BackColor="White" Font-Size="10px" SelectedValue='<%# Bind("LabStatus") %>' />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Type:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblLabType" runat="server" Text='<%# Eval("LabType") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbLabType" runat="server" Text='<%# Bind("LabType") %>' Width="200px" BackColor="White" Enabled="false" Font-Size="10px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="UIC:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblUIC" runat="server" Text='<%# Eval("UIC") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:DropDownList ID="ddUIC" runat="server" Width="200px" BackColor="White" Font-Size="10px" SelectedValue='<%# Bind("UIC") %>' />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Org code:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblOrgCode" runat="server" Text='<%# Eval("OrgCode") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:DropDownList ID="ddOrgCode" runat="server" Width="200px" BackColor="White" Font-Size="10px" SelectedValue='<%# Bind("OrgCode") %>' />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="VSYSCOM:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblVSYSCOM" runat="server" Text='<%# Eval("VSYSCOM") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbVSYSCOM" runat="server" Text='<%# Bind("VSYSCOM") %>' Width="200px" BackColor="White" Font-Size="10px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Name:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblLabName" runat="server" Text='<%# Eval("LabName") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbLabName" runat="server" Text='<%# Bind("LabName") %>' Width="500px" BackColor="White" Font-Size="10px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Description:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblLabDescription" runat="server" Text='<%# Eval("LabDescription") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbLabDescription" runat="server" Text='<%# Bind("LabDescription") %>' TextMode="MultiLine" Width="500px" Height="75px" BackColor="White" Font-Size="11px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Technical Capability:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblTechnicalCapability" runat="server" Text='<%# Eval("TechnicalCapability") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbTechnicalCapability" runat="server" Text='<%# Bind("TechnicalCapability") %>' TextMode="MultiLine" Width="500px" Height="75px" BackColor="White" Font-Size="11px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Facility:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblFacility" runat="server" Text='<%# Eval("Facility") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbFacility" runat="server" Text='<%# Bind("Facility") %>' TextMode="MultiLine" Width="500px" Height="75px" BackColor="White" Font-Size="11px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Notes:" HeaderStyle-Width="15%" ItemStyle-Width="85%">
                    <ItemTemplate>
                      <asp:Label ID="lblComments" runat="server" Text='<%# Eval("Comments") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="tbComments" runat="server" Text='<%# Bind("Comments") %>' TextMode="MultiLine" Width="500px" Height="75px" BackColor="White" Font-Size="11px" />
                    </EditItemTemplate>
                  </asp:TemplateField>
                  <asp:CommandField ShowEditButton="true" />
                </Fields>
                <FooterStyle CssClass="footer" />
                <HeaderStyle Width="15%" />
                <PagerStyle CssClass="footer" />
                <RowStyle CssClass="row" />
              </asp:DetailsView>

I bind it to a DataTable that is returned from my BLL in the Page's codebhind like so.

 Utilities.BindCompositeControl(dvCapability, Controller.GetCapability(getIaid()));

The BindCompositeControl() method is in a static class that I use to reduce redundatnt coding. The method is coded as follows:

public static void BindCompositeControl(CompositeDataBoundControl control, DataTable values)
        {
            control.DataSource = values;
            control.DataBind();
        }

However, when I try to use the Edit or Insert Buttons that are generated by the DetailsView control nothing happens. What am I missing in order to bind this control programatically and change it's operational state on the UI?

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

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

发布评论

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

评论(2

夜巴黎 2024-12-11 03:10:51

如果您可以发布要绑定的数据源的代码,这将有助于查看您是否允许插入和更新,这可能会导致问题。

但是我怀疑您在更改详细信息视图的模式时可能需要手动维护数据源。当您单击“编辑/插入”时,它会导致回发,并且我相信它不再附加数据源。

第一次加载控件时,您将附加数据源,但之后每次都需要重新创建控件,可能是将控件状态保存到 ViewState,重新创建数据源并将控件状态从 ViewState 中拉出来在每个页面加载时。

听起来比实际更复杂。执行此操作的一种方法是通过页面的 OnInit 事件创建数据源。如果您需要维护选定的行或页面,请使用 ViewState 来维护这些详细信息。

您应该在 ViewState 启动之前创建数据源并绑定它,因此创建数据源并在 OnInit 中应用它

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    datatype myData = /* .... */
    //Create DataSource here
    for (int i = 0; i < 10; i++) {
        //Do something
        myData.add(/* some data */);
    }

    dvCapability.DataSource = myData;
    dvCapability.DataBind();
}

要添加和删除项目,您需要以页面其余部分可以访问的方式存储数据,例如 Session[],然后在 dvCapability_Inerting 事件期间删​​除/添加项目等。添加或删除后,您需要将数据与更新的数据重新绑定。

看看这个 http://weblogs。 asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx。这解释了2下的事情。持久静态数据部分。

If you can post your code for your data source you are binding, that will help see if you've allowed inserting and updating which may be causing an issue.

However I suspect you may need to manually maintain the datasource as you change the mode of the DetailsView. When you click Edit / Insert it causes a postback and I believe it no longer has the datasource attached.

On the first load of the control, you'll attach the datasource but after that you need to recreate the control each time, probably by saving the control state to the ViewState, recreating the data source and pull the control state back out of the ViewState on each page load.

It sounds more complex than it is. One way to do this is by creating the datasource through the OnInit event for the page. If you need to maintain selected rows or pages, use the ViewState to maintain those details.

You should create the DataSource and bind it before the ViewState is up, so create the datasource and apply it in OnInit

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    datatype myData = /* .... */
    //Create DataSource here
    for (int i = 0; i < 10; i++) {
        //Do something
        myData.add(/* some data */);
    }

    dvCapability.DataSource = myData;
    dvCapability.DataBind();
}

To add and delete items, you'll need to store the data in a way that is accessible to the rest of a page, such as Session[] and then remove / add items to that during the dvCapabilities_Inerting event, etc. After an add or delete you'll need to rebind the data with the updated data.

Take a gander at this http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx. This sort of explains things under the 2. Persisting static data section.

吻风 2024-12-11 03:10:51

对于那些可能会遇到这种情况的人。这是我想出的并且有效。我向 ViewState 字典添加一个值,并将其设置在 Page_OnInit 事件处理程序中。在 Page_PreRender 事件处理程序中,我绑定了详细信息视图并设置其模式。请注意,如果您使用视图状态,则必须在视图状态加载后绑定数据并更改模式。在加载 ViewState 之前调用 Page 的 Initialize 方法。希望这对有需要的人有帮助。我发现很多关于这个主题的帖子都没有成功回答。非常感谢柯克给了我一个可靠的答案,并为我想去的地方提供了很好的指导。

public partial class About : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            if (!IsPostBack)
            {
                //Initially add the view state variable
                ViewState.Add("DetailsViewMode", DetailsViewMode.ReadOnly);

            }
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (ViewState["DetailsViewMode"] != null)
            {
                //change the mode of the DetailsView
                dvTest.ChangeMode((DetailsViewMode)ViewState["DetailsViewMode"]);

                //Bind the details view to a source
                //This is my own custom class but this is where you yould put
                //DetailsView.Datasource = <source>
                //DetailsView.DataBind()
                Utilities.BindCompositeControl(dvTest, NadController.GetCapability(19));
            }

            base.OnPreRender(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        //Must handle ModeChanging event or you'll get a run time error when binding DetailsView programatically
        protected void dvTest_ModeChanging(object sender, DetailsViewModeEventArgs e)
        { 
        }

        //Handles button clicks from the DetailsView control
        protected void dvTest_OnItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            ViewState["DetailsViewMode"] = GetDetailsViewMode(e.CommandName);
        }



        protected DetailsViewMode GetDetailsViewMode(string mode)
        {
            switch (mode)
            {
                case Constants.Edit:
                    return DetailsViewMode.Edit;
                case Constants.Insert:
                    return DetailsViewMode.Insert;
                default:
                    return DetailsViewMode.ReadOnly;
            }
        }
    }

For those of you who may run into this down the line. Here's what I came up with and it works. I add a value to the ViewState dictionary and set it in the Page_OnInit event handler. In the Page_PreRender event handler is where I bind the detailsview and set it's Mode. Please note that if you are using viewstate you must bind your data and change mode after the view state has loaded. the Page's Initialize method is called before the ViewState is loaded. Hope this helps those who need it. I found many posts that weren't successfully answered regarding this topic. Much thanks to Kirk for giving me a solid answer and a great lead on where I wanted to go.

public partial class About : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            if (!IsPostBack)
            {
                //Initially add the view state variable
                ViewState.Add("DetailsViewMode", DetailsViewMode.ReadOnly);

            }
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (ViewState["DetailsViewMode"] != null)
            {
                //change the mode of the DetailsView
                dvTest.ChangeMode((DetailsViewMode)ViewState["DetailsViewMode"]);

                //Bind the details view to a source
                //This is my own custom class but this is where you yould put
                //DetailsView.Datasource = <source>
                //DetailsView.DataBind()
                Utilities.BindCompositeControl(dvTest, NadController.GetCapability(19));
            }

            base.OnPreRender(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        //Must handle ModeChanging event or you'll get a run time error when binding DetailsView programatically
        protected void dvTest_ModeChanging(object sender, DetailsViewModeEventArgs e)
        { 
        }

        //Handles button clicks from the DetailsView control
        protected void dvTest_OnItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            ViewState["DetailsViewMode"] = GetDetailsViewMode(e.CommandName);
        }



        protected DetailsViewMode GetDetailsViewMode(string mode)
        {
            switch (mode)
            {
                case Constants.Edit:
                    return DetailsViewMode.Edit;
                case Constants.Insert:
                    return DetailsViewMode.Insert;
                default:
                    return DetailsViewMode.ReadOnly;
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文