C# 不太晦涩的方式来填充DetailsView?

发布于 2024-09-30 04:32:30 字数 3398 浏览 1 评论 0原文

我似乎无法将单个 GridView 的行正确绑定到 DetailsView 。目前我有这个:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace WebApp
{
    public partial class CrudGrid : System.Web.UI.UserControl
    {
        public const string EditCommand = "EditDialog";

        public object DataSource
        {
            get { return Grid.DataSource; }
            set { Grid.DataSource = value; }
        }

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

        protected void Grid_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch(e.CommandName)
            {
                case EditCommand:
                {
                    int index;

                    if(!int.TryParse(e.CommandArgument.ToString(), out index))
                        throw new ArgumentException();

                    TableCellCollection cells = Grid.Rows[index].Cells;

                    Details.DataSource = new object[] { new DataSourceProvider(cells[2].Text, cells[3].Text, cells[4].Text) };
                    Details.DataBind();

                    DetailsPanel.Update();
                    break;
                }
            }
        }

        protected void Grid_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }

        private class DataSourceProvider
        {
            public string Cell1 { get; set; }
            public string Cell2 { get; set; }
            public string Cell3 { get; set; }

            public DataSourceProvider(string cell1, string cell2, string cell3)
            {
                Cell1 = cell1;
                Cell2 = cell2;
                Cell3 = cell3;
            }
        }
    }
}

和客户端:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CrudGrid.ascx.cs" Inherits="Firelight.WebApp.WebControls.CrudGrid" %>

<asp:UpdatePanel ID="GridPanel" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:ImageButton ID="GridInsert" ImageUrl="/images/crud/insert.png" runat="server" />

        <asp:GridView ID="Grid" GridLines="None" AllowPaging="true" PageSize="15" runat="server"
            OnRowCommand="Grid_OnRowCommand"
            OnRowDeleting="Grid_OnRowDeleting"
        >
            <Columns>
                <asp:ButtonField CommandName="EditDialog" ButtonType="Image" ImageUrl="/images/crud/edit.png" HeaderImageUrl="/images/crud/edit.png" />
                <asp:ButtonField CommandName="Delete" ButtonType="Image" ImageUrl="/images/crud/delete.png" HeaderImageUrl="/images/crud/delete.png" />
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel ID="DetailsPanel" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:DetailsView ID="Details" GridLines="None" runat="server"
         AutoGenerateRows="true" DefaultMode="ReadOnly" AllowPaging="false">
        </asp:DetailsView>
    </ContentTemplate>
</asp:UpdatePanel>

这个“有效”:我在详细信息视图中得到一个列表,枚举我手动构建到类中的单元格...

我真正想要的是类似于

Details.DataSource = Grid.Rows[index]

枚举字段及其名称的 东西,但这似乎也不能正常工作。

有什么建议或想法吗?

这是针对用户控件的,以防它改变任何内容。

I can't seem to bind a single GridView's row to a DetailsView properly. Currently I have this:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace WebApp
{
    public partial class CrudGrid : System.Web.UI.UserControl
    {
        public const string EditCommand = "EditDialog";

        public object DataSource
        {
            get { return Grid.DataSource; }
            set { Grid.DataSource = value; }
        }

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

        protected void Grid_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch(e.CommandName)
            {
                case EditCommand:
                {
                    int index;

                    if(!int.TryParse(e.CommandArgument.ToString(), out index))
                        throw new ArgumentException();

                    TableCellCollection cells = Grid.Rows[index].Cells;

                    Details.DataSource = new object[] { new DataSourceProvider(cells[2].Text, cells[3].Text, cells[4].Text) };
                    Details.DataBind();

                    DetailsPanel.Update();
                    break;
                }
            }
        }

        protected void Grid_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }

        private class DataSourceProvider
        {
            public string Cell1 { get; set; }
            public string Cell2 { get; set; }
            public string Cell3 { get; set; }

            public DataSourceProvider(string cell1, string cell2, string cell3)
            {
                Cell1 = cell1;
                Cell2 = cell2;
                Cell3 = cell3;
            }
        }
    }
}

and client-side:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CrudGrid.ascx.cs" Inherits="Firelight.WebApp.WebControls.CrudGrid" %>

<asp:UpdatePanel ID="GridPanel" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:ImageButton ID="GridInsert" ImageUrl="/images/crud/insert.png" runat="server" />

        <asp:GridView ID="Grid" GridLines="None" AllowPaging="true" PageSize="15" runat="server"
            OnRowCommand="Grid_OnRowCommand"
            OnRowDeleting="Grid_OnRowDeleting"
        >
            <Columns>
                <asp:ButtonField CommandName="EditDialog" ButtonType="Image" ImageUrl="/images/crud/edit.png" HeaderImageUrl="/images/crud/edit.png" />
                <asp:ButtonField CommandName="Delete" ButtonType="Image" ImageUrl="/images/crud/delete.png" HeaderImageUrl="/images/crud/delete.png" />
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel ID="DetailsPanel" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:DetailsView ID="Details" GridLines="None" runat="server"
         AutoGenerateRows="true" DefaultMode="ReadOnly" AllowPaging="false">
        </asp:DetailsView>
    </ContentTemplate>
</asp:UpdatePanel>

this "works": I get a list in the detailsview enumerating the cells I manually built into a class...

what I'd actually want is something similar to

Details.DataSource = Grid.Rows[index]

enumerating the fields and their names, but that doesn't seem to work properly either.

any suggestions or ideas?

this is for a usercontrol, in case it changes anything.

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

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

发布评论

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

评论(1

娇妻 2024-10-07 04:32:30

为什么不简单地使用主/详细信息方式来使用 griview 项目作为详细信息视图的控制参数来执行此操作?

<asp:DetailsView AutoGenerateRows="False" DataKeyNames="au_id" DataSourceID="SqlDataSource3"
        HeaderText="Author Details" ID="DetailsView1" runat="server" Width="275px">
        <Fields>
          <asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />
          <asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />
                   </Fields>
      </asp:DetailsView>
      <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:Pubs %>" ID="SqlDataSource3"
        runat="server" SelectCommand="SELECT [au_id], [au_lname] FROM [authors] WHERE ([au_id] = @au_id)">
        <SelectParameters>
          <asp:ControlParameter ControlID="GridView1" Name="au_id" PropertyName="SelectedValue"
            Type="String" />
        </SelectParameters>
      </asp:SqlDataSource>

像这样的东西:
http://quickstarts.asp .net/QuickStartv20/util/srcview.aspx?path=~/aspnet/samples/data/GridViewMasterDetails.src

why dont you simply use master/details way to do this using griview item as a control parameter to the detailsview?

<asp:DetailsView AutoGenerateRows="False" DataKeyNames="au_id" DataSourceID="SqlDataSource3"
        HeaderText="Author Details" ID="DetailsView1" runat="server" Width="275px">
        <Fields>
          <asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />
          <asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />
                   </Fields>
      </asp:DetailsView>
      <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:Pubs %>" ID="SqlDataSource3"
        runat="server" SelectCommand="SELECT [au_id], [au_lname] FROM [authors] WHERE ([au_id] = @au_id)">
        <SelectParameters>
          <asp:ControlParameter ControlID="GridView1" Name="au_id" PropertyName="SelectedValue"
            Type="String" />
        </SelectParameters>
      </asp:SqlDataSource>

Something like this:
http://quickstarts.asp.net/QuickStartv20/util/srcview.aspx?path=~/aspnet/samples/data/GridViewMasterDetails.src

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