在 ASP.NET 的 ListView 中创建自定义按钮

发布于 2024-10-19 06:06:41 字数 992 浏览 4 评论 0原文

我有一个 Results.aspx 页面,它显示通过 ListView 使用 SqlDataSource 对象查询的结果记录。我想添加一个“查看”按钮,该按钮将显示在每条记录旁边,单击后将进入一个单独的页面,该页面将显示有关该记录的详细信息。我该如何实现这个目标?

编辑

我已经尝试过你所说的,香茅,这就是我想出的:

<td>
    <asp:CheckBox ID="CheckBox1" runat="server" />
</td>
<td>
    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="ViewButtonClick" CommandArgument='<%# Eval("ServiceId") %>'>View</asp:LinkButton>
</td>

这是我想要调用的方法:

protected void ViewButtonClick(object sender, CommandEventArgs e)
    {
        var serviceId = Convert.ToInt32(e.CommandArgument);

        ServiceToView = DataAccessLayer.Service.Select(new Service { ServiceId = serviceId });
        Server.Transfer("~/ViewService.aspx");
    }

不幸的是实际上没有发生任何事情......我错过了什么吗?

编辑——修正了

我遗漏的东西!我的 CommandName 等于我的方法名称,而不是 OnCommand。我取出 CommandName,保留参数位并将 CommandName 替换为 OnCommand。现在一切正常,但我需要 CommandName 做什么?

I have a Results.aspx page that displays the resulting records queried using a SqlDataSource object via a ListView. I want to add a "View" button that will appear next to each record, and when clicked will take me to a separate page that will display details about that record. How do I accomplish this?

Edit

I have tried what you said, citronas and here's what I've come up with:

<td>
    <asp:CheckBox ID="CheckBox1" runat="server" />
</td>
<td>
    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="ViewButtonClick" CommandArgument='<%# Eval("ServiceId") %>'>View</asp:LinkButton>
</td>

And here is the method that I want to be called:

protected void ViewButtonClick(object sender, CommandEventArgs e)
    {
        var serviceId = Convert.ToInt32(e.CommandArgument);

        ServiceToView = DataAccessLayer.Service.Select(new Service { ServiceId = serviceId });
        Server.Transfer("~/ViewService.aspx");
    }

Unfortunately nothing actually happens...am I missing something?

Edit -- Fixed

I was missing something! I had CommandName equal to my method name instead of OnCommand. I took out CommandName, kept the argument bit and replaced CommandName with OnCommand. Everything works now, but what would I ever need CommandName for?

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

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

发布评论

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

评论(2

国产ˉ祖宗 2024-10-26 06:06:41

您可以将 LinkBut​​ton 添加到 ListView 的 ItemTemplate 中。
将标识每条记录的值绑定到 LinkBut​​ton 的 CommandArgument。
订阅 LinkBut​​ton 的命令事件。在那里您可以访问 CommandEventArgs.CommandArgument

You can add a LinkButton into the ItemTemplate of the ListView.
Bind the value that identifies each record to the CommandArgument of the LinkButton.
Subscribe to the Command-Event of the LinkButton. There you have access to CommandEventArgs.CommandArgument

你是年少的欢喜 2024-10-26 06:06:41

你最终所做的事情对风暴起了作用。我决定采纳 Citronas 的建议并分享我的答案。

第一:
在 aspx 上,我使用自己的 CommandName 和 CommandArgument 添加了一个 LinkBut​​ton 到我的 ItemTemplate。我将项目的 ID 作为 CommandArgument 传递,以便稍后可以在我的子组件中使用它。

<asp:LinkButton ID="lnkBtnAnswers" runat="server" CommandName="Answers"
     CommandArgument='<%# Eval("ID")%>'>Answers</asp:LinkButton>

第二:
在代码隐藏中,我创建了一个子程序,每当用户执行操作时都会调用该子程序。正如 Citronas 通常提到的,您在这里使用“选择”、“添加”、“编辑”或“删除”。我决定创造“答案”。

注意: 处理 MyControl.ItemCommand 在这里非常重要,因为它使您订阅命令事件。

Protected Sub lvQuestions_Command(sender As Object, e As CommandEventArgs) Handles lvQuestions.ItemCommand
    If e.CommandName.ToLower() = "answers" Then
          hfSelectedQuestionID.Value = e.CommandArgument
    End If
End Sub

完毕!现在,由于每个命令都会经过新的子命令,因此检查正确的命令名称非常重要,以便您可以执行适当的操作。不要忘记使用 CommandArgument 对您有利。

What you wound up doing worked Storm. I decided to go with Citronas' suggestion and share my answer.

FIRST:
On the aspx I added a LinkButton to my ItemTemplate with my own CommandName and CommandArgument. I passed my item's ID as a CommandArgument so I could later use it inside my sub.

<asp:LinkButton ID="lnkBtnAnswers" runat="server" CommandName="Answers"
     CommandArgument='<%# Eval("ID")%>'>Answers</asp:LinkButton>

SECOND:
On the codebehind I created a sub that would be called whenever the user conducted an action. As Citronas mentioned normally you use "Select", "Add", "Edit", or "Delete" here. I decided to create "answers".

Note: Handles MyControl.ItemCommand is very important here as it is what subscribes you to the command event.

Protected Sub lvQuestions_Command(sender As Object, e As CommandEventArgs) Handles lvQuestions.ItemCommand
    If e.CommandName.ToLower() = "answers" Then
          hfSelectedQuestionID.Value = e.CommandArgument
    End If
End Sub

Done! Now since every command goes through the new sub, it is important to check for the right commandName so you can conduct the appropriate action. Don't forget to use the CommandArgument to your advantage.

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