ASP.NET 自定义按钮

发布于 2024-10-16 07:44:23 字数 1519 浏览 3 评论 0原文

按钮布局

Button Layout

我在 div 内有 3 个标签。我想让 div 可点击并通过点击 div 触发回发。 本质上我想制作一个看起来像图片的自定义按钮。到目前为止,我发现这样做的唯一方法是让 div onclick 事件触发 javascript,或者执行一些自定义用户控制魔法,但我无法正常工作。

用户控制

<div class="meetingContainer" >
    <div class="meetingCityState">
      <asp:Label ID="lblMeetingCityState" runat="server" Text='<%# Eval("City") %>'></asp:Label>
    </div>
    <div>
      <asp:Label ID="lblMeetingDate" runat="server" Text='<%# Eval("MeetingDate") %>'></asp:Label>
    </div>
    <div>
      <asp:Label ID="lblMeetingSite" runat='server' Text='<%# Eval("Location") %>'></asp:Label>
    </div>
  </div>

代码隐藏

<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Partial Public Class MeetingButton
  Inherits System.Web.UI.UserControl
  Implements System.Web.UI.IPostBackEventHandler

  Public Event Click As EventHandler

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

  End Sub

  Protected Overridable Sub OnClick(ByVal E As EventArgs)
    RaiseEvent Click(Me, E)
  End Sub

  Public Sub RaisePostBackEvent(ByVal eventArgument As String) _
         Implements IPostBackEventHandler.RaisePostBackEvent

    OnClick(New EventArgs())
  End Sub
End Class

Button Layout

Button Layout

I have 3 labels inside of a div. I want to make the div clickable and trigger a postback from clicking the div. In essensce I want to make a custom button that looks like the picture. So far the only way I found out of doing this is either having the div onclick event trigger javascript, or do some custom usercontrol magic, which i cant get to work.

User Control

<div class="meetingContainer" >
    <div class="meetingCityState">
      <asp:Label ID="lblMeetingCityState" runat="server" Text='<%# Eval("City") %>'></asp:Label>
    </div>
    <div>
      <asp:Label ID="lblMeetingDate" runat="server" Text='<%# Eval("MeetingDate") %>'></asp:Label>
    </div>
    <div>
      <asp:Label ID="lblMeetingSite" runat='server' Text='<%# Eval("Location") %>'></asp:Label>
    </div>
  </div>

code behind

<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Partial Public Class MeetingButton
  Inherits System.Web.UI.UserControl
  Implements System.Web.UI.IPostBackEventHandler

  Public Event Click As EventHandler

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

  End Sub

  Protected Overridable Sub OnClick(ByVal E As EventArgs)
    RaiseEvent Click(Me, E)
  End Sub

  Public Sub RaisePostBackEvent(ByVal eventArgument As String) _
         Implements IPostBackEventHandler.RaisePostBackEvent

    OnClick(New EventArgs())
  End Sub
End Class

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

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

发布评论

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

评论(2

分分钟 2024-10-23 07:44:23

是的,您可以使用 __doPostBack 方法来执行此操作。您可以使用以下方法从服务器创建对此的自定义引用: http://msdn.microsoft.com/en-us/library/ms153112(v=vs.80).aspx

否则,您可以通过传递以下内容来手动创建它:

<div id="<Server control's Client ID>" name="<Server control's Unique ID>" onclick="__doPostBack('<unique name>', '<command name and args>');">

在服务器上,检查事件的以下集合:

string target = Request.Form["__EVENTTARGET"];
if (target != null && target.EndsWith("<id>"))
{
   // do this
}

您需要将 __doPostBack 唯一名称计算为用户控件的 UniqueID 属性。
这将自动调用 IPostBackEventHandler 定义的方法;任何实现此接口的控件都调用其 RaisePostBackEvent 方法...但是,我不能 100% 确定它对于用户控件的工作方式相同...

Yes, you can do this, using the __doPostBack method. You can create a custom reference to this from the server using this method: http://msdn.microsoft.com/en-us/library/ms153112(v=vs.80).aspx.

Otherwise, you can manually create it by passing it this:

<div id="<Server control's Client ID>" name="<Server control's Unique ID>" onclick="__doPostBack('<unique name>', '<command name and args>');">

On the server, check the following collection for the event:

string target = Request.Form["__EVENTTARGET"];
if (target != null && target.EndsWith("<id>"))
{
   // do this
}

You need to math the __doPostBack unique name to the UniqueID property of the user control.
This would automatically invoke the method defined by the IPostBackEventHandler; any control that implements this interface has its RaisePostBackEvent method called... however, I'm not 100% sure it works the same way for user controls...

戒ㄋ 2024-10-23 07:44:23

看起来链接 asp:linkbutton 可以满足我的需要,谢谢你

looks link an asp:linkbutton will do what I need thaank you

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