生活随笔
收集整理的這篇文章主要介紹了
怎样判断RadioButtonList控件是否有选择
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實現這個功能,方法很多的。 你可以使用Javascript來實現,http://www.cnblogs.com/insus/archive/2013/01/14/2859079.html?當然你可以不使用Javascript使用JQuery一樣可以完成。 你還可以使用程序后臺實現,http://www.cnblogs.com/insus/archive/2012/09/05/2671729.html? 你還可以使用asp.net自帶的驗證控件來判用戶是否有對RadioButtonList控件是否有選擇:
View Code < asp:RadioButtonList ID ="RadioButtonList1" runat ="server" RepeatDirection ="Horizontal" > < asp:ListItem Text ="1" ></ asp:ListItem > < asp:ListItem Text ="2" ></ asp:ListItem > < asp:ListItem Text ="3" ></ asp:ListItem > < asp:ListItem Text ="4" ></ asp:ListItem > </ asp:RadioButtonList > < asp:RequiredFieldValidator ID ="RequiredFieldValidator1" runat ="server" ControlToValidate ="RadioButtonList1" Display ="none" ErrorMessage ="必須選擇" ></ asp:RequiredFieldValidator > < asp:ValidationSummary ID ="ValidationSummary1" runat ="server" EnableClientScript ="true" ShowMessageBox ="true" ShowSummary ="false" /> < asp:Button ID ="Button1" runat ="server" Text ="Button" /> 上面的html運行效果如下: 其實上面所說的,均是一個前提而已。今天Insus.NET想實現的這個問題, 這帖當時已經收藏了,不過手上工作繁忙,沒能及時按自己的想法提供答案,雖然給帖了,沒有關系,把想法分享于博客上。 沒有帖主的環境,可以模擬一個的。 創建一個對象:
Insus.NET.Survey using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
/// Summary description for Survey
/// </summary>
namespace Insus.NET
{ public class Survey{ private string _ID; private string _Title; public string ID{ get {
return _ID; } set { _ID =
value; }} public string Title{ get {
return _Title; } set { _Title =
value; }} public Survey(){ // // TODO: Add constructor logic here //
} public Survey(
string id,
string title){ this ._ID =
id; this ._Title =
title;}}
} Ok, 我們新建一個網頁,并在網頁中填充一些演示數據入剛才創建好的個對象中:
View Code private List<Survey>
SurveyData(){List <Survey> s =
new List<Survey>
();s.Add( new Survey(
" 1.1 " ,
" title 1 " ));s.Add( new Survey(
" 1.2 " ,
" title 1 " ));s.Add( new Survey(
" 1.3 " ,
" title 1 " )); return s;} 有了數據了,在.aspx設計前端代碼,拉一個Repeater控件至網頁:
View Code < form id ="form1" runat ="server" > < div > < asp:Repeater ID ="Repeater1" runat ="server" > < HeaderTemplate > < table border ="1" border-collapse ="collapse" cellpadding ="3" cellspacing ="0" width ="300" > </ HeaderTemplate > < ItemTemplate > < tr > < td style ="width: 30px;" > <% # Eval ( " ID " ) %> </ td > < td > <% # Eval ( " Title " ) %> </ td > < td > < asp:RadioButtonList ID ="RadioButtonList1" runat ="server" RepeatDirection ="Horizontal" > < asp:ListItem Text ="5" ></ asp:ListItem > < asp:ListItem Text ="4" ></ asp:ListItem > < asp:ListItem Text ="3" ></ asp:ListItem > < asp:ListItem Text ="2" ></ asp:ListItem > < asp:ListItem Text ="1" ></ asp:ListItem > </ asp:RadioButtonList > < asp:RequiredFieldValidator ID ="RequiredFieldValidator1" runat ="server" ControlToValidate ="RadioButtonList1" Display ="none" ErrorMessage ='<%# Eval ("ID") + " 沒有勾選打分。" % > '>
</ asp:RequiredFieldValidator > </ td > </ tr > </ ItemTemplate > < FooterTemplate > </ table > </ FooterTemplate > </ asp:Repeater > < asp:ValidationSummary ID ="ValidationSummary1" runat ="server" EnableClientScript ="true" ShowMessageBox ="true" ShowSummary ="false" /> < asp:Button ID ="Button1" runat ="server" Text ="投票" /> </ div > </ form > 看看高亮的代碼:
?
在.aspx.cs綁定數據給Repeater控件。
演示一下效果,看看是否有達到效果:
算是完成了,效果也達到了,不過Insus.NET就此例子,想玩玩其它。望你也能有所收獲。 由于整個RadioButtonList控件在Repeater控件中每一行是獨立的。也可以看到Insus.NET在設計對象時,也沒有設計留有此列相關的屬性,只是用了ID和Title屬性。既然這旨獨立的,因此,可以把這個塊地方抽出來,放置于一個用戶控件中去,用戶控件(ascx)沒錯吧?是的,沒有錯。 好象復雜度比上面完成的例子更大喔。面向對象嘛!把獨立的部分分開,總比藕合性強的程序好些。就是說,某一天程序有改變,某些記錄的評分是改為其它,而不是所有都是RadioButtonList控件有5個選項,因此,改為使用一個用戶控件替代,這樣有改變量,只改用戶控件可。 好吧,創建一個用戶控件在站點上。 把下圖comment的部分移至用戶控件。 移過去之后,用戶控件稍作改動: 現在有兩個問題需要想到的,就是網頁上的Repeater控件內的記錄有些信息需要傳至用戶控件,另外還要想到,用戶控件對用戶選擇的值傳至網頁,因為選擇的值傳至網頁之后,用戶提交投票時,需要存儲起來。 這是頁面與用戶控件之間的交互。下面是Insus.NET使用Interface(接口)來處理這個交互的問題。?
Insus.NET.IInteractive using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
/// Summary description for IInteractive
/// </summary>
namespace Insus.NET
{ public interface IInteractive{ void SetValue(
object obj); object GetValue();}
} 接口寫好之后,在用戶控件.ascx.cs實用這個接口。 現在,我們把用戶控件完成了,就可以把它拉至網頁去。
?
好此時網頁與用戶控件已經碰面了,還差心靈與語言溝能了。怎樣讓它們之間可以溝通呢? 做法是在Repeater控件上寫一個事件。
在.aspx啟用了Repeater控件的OnItemDataBound事件,還得去.aspx.cs實現這樣事件的程序:
看到否,上圖中高亮的代碼,就是把Repeater控件內的記錄的信息傳給用戶控件。 效果跟沒有使用用戶控件時沒有兩樣。
??
總結
以上是生活随笔 為你收集整理的怎样判断RadioButtonList控件是否有选择 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。