google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Thêm sự kiện onClick vào các điều khiển RadioButtonList trong ASP.NET Bài viết này sẽ hướng dẫn bạn cách thêm nhiều sự kiện onClick vào các điều khiển RadioButtonList trong ASP.NET một cách nhanh và dễ dàng nhất.


Nhãn: sự kiện, onClick, điều khiển, RadioButtonList, ASP.NET

Miễn phí web hosting 1 năm đầu tại iPage



Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?

Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.

Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Thử iPage miễn phí cho năm đầu tiên NGAY

I've been fighting to add javascript code to the onclick event of ASP.NET RadioButtonList controls for the last hour, and given the frustration it caused me, I am writing the solution up for future reference. The real world code I am writing is far more complex than this example, but hopefully this illustrates enough to act as a primer, and to jog my own memory in future.

It's actually quite simple. For the example, I'm going to make a RadioButtonList make a Textbox dynamically appear and disappear.

The ASP.NET RadioButton List Control

Here's how the RadioButtonList might look...

<asp:RadioButtonList ID="rblFoo" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
  <asp:ListItem Text="Show" Value="show" />
  <asp:ListItem Text="Hide" Value="hide" />
</asp:RadioButtonList>
<asp:TextBox ID="tbFoo" runat="server"></asp:TextBox>

The Javascript Function

Here's a javascript function that we are going to attach to the onclick event of each radio button (you would typically put this in a javascript file, and reference it via a SCRIPT tag in your ASPX page)

function fooClick(option_value,tb_id){
  var textbox_object = document.getElementById(tb_id);
  if (option_value == 'show'){
    textbox_object.style.display = 'block';
  } else {
    textbox_object.style.display = 'none';
  }
}

Attaching the Javascript to the OnClick event of the ASP.NET RadioButtonList Options

In the OnLoad() event of your page, do the following;

foreach(ListItem li in rblFoo.Items){
  li.Attributes.Add("onclick","fooClick('" + li.Value + "','" + tbFoo.ClientID + "');");
}

The important point to realise is that ASP.NET Server controls typically get assigned their ID and Name dynamically at runtime. The last part of this solution solves that by attaching the javascript to the objects on the server side, where it knows the ClientID it has already generated for the control we want to interact with.

iPhoneKer.com
Save up to 630$ when buy new iPhone 15

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language

Dall-E-OpenAI.com
Generate creative images automatically with AI

AIVideo-App.com
Render creative video automatically with AI

JavaScript theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web