从 ASP Repeater 内的按钮调用 Javascript?

发布于 2024-12-01 12:42:58 字数 1036 浏览 4 评论 0 原文

我正在使用 ASP 中继器。在 中,除了其他标签等之外,还有 3 个 HTML 按钮 (),单击时进行不同的 javascript/jquery 调用。这工作正常。

现在,我想根据绑定到中继器的布尔值动态显示/隐藏这些按钮。我知道如果我使用的是 ASP 按钮,我可以简单地使用如下内容:

Visible='<%# Eval(bMyBoolean) %>'

我尝试将 HTML 按钮切换为 ASP 按钮,但随后我无法让 javascript 调用正常工作。我知道您可以在每个按钮的后面代码中连接 Javascript 调用(在 Repeater.ItemDataBound 事件中),但我不确定如何在我的场景的后面代码中执行此操作。下面是我的一个 HTML 按钮的示例:

<input type="button" id="myButton" value="Test" onclick="MyJavascriptFunction(this,'<%# CType(Container.DataItem, myClassObject).FirstName%>');" />

在上面的示例中,我将 FirstName 值(它是我绑定到转发器的类对象的属性)传递给我的 Javascript 函数。

对我来说,如果我使用 ASP 中继器,那么使用 ASP 按钮就有意义,但我不知道它是否真的有区别?

所以,我的问题是:如何根据绑定到转发器的布尔值动态显示/隐藏 ASP 转发器内的 HTML 按钮?或者,如果这是一个不好的做法,我如何使用 ASP 按钮进行 javascript 调用,如上面的调用(引用来自中继器的值)?

预先感谢您的任何帮助/建议!

I'm using an ASP Repeater. In the <ItemTemplate> and the <AlternatingItemTemplate> there are, among other labels, etc, 3 HTML buttons (<input type="button"...) that make different javascript/jquery calls when clicked. This is working fine.

Now, I want to dynamically show/hide these buttons based on a boolean that is bound to my Repeater. I know if I were using an ASP Button, that I could simply use something like:

Visible='<%# Eval(bMyBoolean) %>'

I tried switching my HTML buttons to ASP Buttons but then I couldn't get the javascript calls to work correctly. I know you can wire up the Javascript calls in the code behind for each button (in the Repeater.ItemDataBound event), but I'm not sure how to do that in the code behind with my scenario. Here is an example of one of my HTML buttons:

<input type="button" id="myButton" value="Test" onclick="MyJavascriptFunction(this,'<%# CType(Container.DataItem, myClassObject).FirstName%>');" />

In the above example, I'm passing the FirstName value (which is a property of the class object I'm binding to the repeater) to my Javascript function.

To me, it seems like if I'm using an ASP Repeater, then it would make sense to use ASP Buttons, but I don't know if it really makes a difference?

So, my question is: How can I either dynamically show/hide an HTML Button inside an ASP Repeater based on a boolean value that is bound to the repeater? Or, if that is a bad practice, how can I make a javascript call with an ASP Button, like the call above (referencing a value from the Repeater)?

Thanks in advance for any help/advice!

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

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

发布评论

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

评论(1

江南烟雨〆相思醉 2024-12-08 12:42:58

您可以根据布尔值动态输出样式信息。考虑一下:

<itemTemplate>
    <input type='button' style='<%# getVisbility(Eval("MyBoolValue")) %>' 
           value='click me' />
</itemTemplate>

然后在您的代码中,您将拥有:

protected function getVisibility(isVisible as boolean) as string 
   if isVisible then
     return "display:inline; "
   else
     return "display:none;"
   end if 
end function 

现在,如果您想从 asp.net 控件执行 JavaScript 调用,您也可以选择:

最简单但绝不是最好的选择是内联 JS:

<itemTemplate>
   <asp:button runat="server" onclientClick="return confirm('click to submit');" 
               text="Click!" />
</itemTemplate>

使用 JQuery 可以提供更复杂的选项,允许您动态捕获单击事件,并确定您要选择的转发器的确切行。一个例子是:

<itemTemplate>
   <asp:button cssclass ='clickMe' runat="server" rel='<%# Eval("MyCol")%>' 
               text="Click!" />
</itemTemplate>

然后 jquery 代码:

$(".clickMe").click(function(){
  var myVal = $(this).attr("rel");
})

you can dynamically output style information based on your boolean. Consider this:

<itemTemplate>
    <input type='button' style='<%# getVisbility(Eval("MyBoolValue")) %>' 
           value='click me' />
</itemTemplate>

Then in your code, you'd have:

protected function getVisibility(isVisible as boolean) as string 
   if isVisible then
     return "display:inline; "
   else
     return "display:none;"
   end if 
end function 

Now, if you wanted to perform a JavaScript call from the asp.net control, you have options as well:

The simplest -- but by no means best option, is to do inline JS:

<itemTemplate>
   <asp:button runat="server" onclientClick="return confirm('click to submit');" 
               text="Click!" />
</itemTemplate>

there are more complex options with the use of JQuery that would allow you to dynamically capture a click event, and determine the exact row of the repeater that you're selecting for. One example of this is:

<itemTemplate>
   <asp:button cssclass ='clickMe' runat="server" rel='<%# Eval("MyCol")%>' 
               text="Click!" />
</itemTemplate>

Then the jquery code:

$(".clickMe").click(function(){
  var myVal = $(this).attr("rel");
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文