GridView 和DetailsView 更新DetailsView 背后的代码

发布于 2024-11-15 09:57:19 字数 492 浏览 2 评论 0原文

我花了整个下午尝试使用 CODE BEHIND 来完成此操作,但没有成功,所以我要求一些 C# 代码。

基本上,我有一个处于主/详细关系的 GV 和 DV。 GV 显示 ID 和名称。如果我单击 GV 行上的“选择”,我想在 DV 中查看其 ID、名称和地址。我知道如何让它在 aspx 文件中以声明方式工作。但在后面的 C# 代码中,我不知道如何继续执行此函数:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Select"))
    {

    //  PLEASE GIVE ME THE CODE HERE TO BIND THE DETAILSVIEW. THANKS!
    // I am using a sqldatasource if it makes any difference

    }
 }

I've spent all afternoon trying to do this using CODE BEHIND without success so I am asking for some C# code.

Basically, I have a GV and a DV in a master/detail relationship. GV displays ID and Name. If I click Select on a GV row, I want to see its ID, Name and Address in DV. I know how get this to work declaratively in an aspx file. But in C# code behind, I don't know how to proceed at this function:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Select"))
    {

    //  PLEASE GIVE ME THE CODE HERE TO BIND THE DETAILSVIEW. THANKS!
    // I am using a sqldatasource if it makes any difference

    }
 }

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

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

发布评论

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

评论(1

你列表最软的妹 2024-11-22 09:57:19

这是一个通用解决方案,向您展示如何实现此目的,请注意,该解决方案并不是非常安全,但我想您会明白它的要点。如果有什么不清楚的地方请评论。

代码隐藏:

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        GridViewRow selected = gv.Rows[Convert.ToInt32(e.CommandArgument)];
        List<ThatClass> cList = new List<ThatClass>();
        cList.Add(new ThatClass(selected.Cells[0].Text, selected.Cells[1].Text));
        dv.DataSource = cList;
        dv.DataBind();
    }
}

标记:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" OnRowCommand="gv_RowCommand">
    <Columns>
        <asp:BoundField DataField="A" HeaderText="A"/>
        <asp:BoundField DataField="B" HeaderText="B" />
        <asp:CommandField ShowSelectButton="true" />
    </Columns>
</asp:GridView>

<asp:DetailsView runat="server" ID="dv">
</asp:DetailsView>

仅供参考:我使用列表绑定 GV:

protected void Page_Load(object sender, EventArgs e)
{
    List<ThatClass> cList = new List<ThatClass>();
    cList.Add(new ThatClass("123", "abc"));
    cList.Add(new ThatClass("456", "def"));
    gv.DataSource = cList;
    gv.DataBind();
}

Here's a general solution showing you how to achieve this, please note that this solution isn't extremely error-safe but I suppose you'll get the jist of it. Please comment if there's anything unclear.

Code-behind:

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        GridViewRow selected = gv.Rows[Convert.ToInt32(e.CommandArgument)];
        List<ThatClass> cList = new List<ThatClass>();
        cList.Add(new ThatClass(selected.Cells[0].Text, selected.Cells[1].Text));
        dv.DataSource = cList;
        dv.DataBind();
    }
}

Markup:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" OnRowCommand="gv_RowCommand">
    <Columns>
        <asp:BoundField DataField="A" HeaderText="A"/>
        <asp:BoundField DataField="B" HeaderText="B" />
        <asp:CommandField ShowSelectButton="true" />
    </Columns>
</asp:GridView>

<asp:DetailsView runat="server" ID="dv">
</asp:DetailsView>

FYI: I bound the GV using a List:

protected void Page_Load(object sender, EventArgs e)
{
    List<ThatClass> cList = new List<ThatClass>();
    cList.Add(new ThatClass("123", "abc"));
    cList.Add(new ThatClass("456", "def"));
    gv.DataSource = cList;
    gv.DataBind();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文