生活随笔
收集整理的這篇文章主要介紹了
怎样判断RadioButtonList控件是否有选择
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
實(shí)現(xiàn)這個(gè)功能,方法很多的。 你可以使用Javascript來實(shí)現(xiàn),http://www.cnblogs.com/insus/archive/2013/01/14/2859079.html?當(dāng)然你可以不使用Javascript使用JQuery一樣可以完成。 你還可以使用程序后臺(tái)實(shí)現(xiàn),http://www.cnblogs.com/insus/archive/2012/09/05/2671729.html? 你還可以使用asp.net自帶的驗(yàn)證控件來判用戶是否有對(duì)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運(yùn)行效果如下: 其實(shí)上面所說的,均是一個(gè)前提而已。今天Insus.NET想實(shí)現(xiàn)的這個(gè)問題, 這帖當(dāng)時(shí)已經(jīng)收藏了,不過手上工作繁忙,沒能及時(shí)按自己的想法提供答案,雖然給帖了,沒有關(guān)系,把想法分享于博客上。 沒有帖主的環(huán)境,可以模擬一個(gè)的。 創(chuàng)建一個(gè)對(duì)象:
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, 我們新建一個(gè)網(wǎng)頁,并在網(wǎng)頁中填充一些演示數(shù)據(jù)入剛才創(chuàng)建好的個(gè)對(duì)象中:
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;} 有了數(shù)據(jù)了,在.aspx設(shè)計(jì)前端代碼,拉一個(gè)Repeater控件至網(wǎng)頁:
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綁定數(shù)據(jù)給Repeater控件。
演示一下效果,看看是否有達(dá)到效果:
算是完成了,效果也達(dá)到了,不過Insus.NET就此例子,想玩玩其它。望你也能有所收獲。 由于整個(gè)RadioButtonList控件在Repeater控件中每一行是獨(dú)立的。也可以看到Insus.NET在設(shè)計(jì)對(duì)象時(shí),也沒有設(shè)計(jì)留有此列相關(guān)的屬性,只是用了ID和Title屬性。既然這旨獨(dú)立的,因此,可以把這個(gè)塊地方抽出來,放置于一個(gè)用戶控件中去,用戶控件(ascx)沒錯(cuò)吧?是的,沒有錯(cuò)。 好象復(fù)雜度比上面完成的例子更大喔。面向?qū)ο舐?#xff01;把獨(dú)立的部分分開,總比藕合性強(qiáng)的程序好些。就是說,某一天程序有改變,某些記錄的評(píng)分是改為其它,而不是所有都是RadioButtonList控件有5個(gè)選項(xiàng),因此,改為使用一個(gè)用戶控件替代,這樣有改變量,只改用戶控件可。 好吧,創(chuàng)建一個(gè)用戶控件在站點(diǎn)上。 把下圖comment的部分移至用戶控件。 移過去之后,用戶控件稍作改動(dòng): 現(xiàn)在有兩個(gè)問題需要想到的,就是網(wǎng)頁上的Repeater控件內(nèi)的記錄有些信息需要傳至用戶控件,另外還要想到,用戶控件對(duì)用戶選擇的值傳至網(wǎng)頁,因?yàn)檫x擇的值傳至網(wǎng)頁之后,用戶提交投票時(shí),需要存儲(chǔ)起來。 這是頁面與用戶控件之間的交互。下面是Insus.NET使用Interface(接口)來處理這個(gè)交互的問題。?
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實(shí)用這個(gè)接口。 現(xiàn)在,我們把用戶控件完成了,就可以把它拉至網(wǎng)頁去。
?
好此時(shí)網(wǎng)頁與用戶控件已經(jīng)碰面了,還差心靈與語言溝能了。怎樣讓它們之間可以溝通呢? 做法是在Repeater控件上寫一個(gè)事件。
在.aspx啟用了Repeater控件的OnItemDataBound事件,還得去.aspx.cs實(shí)現(xiàn)這樣事件的程序:
看到否,上圖中高亮的代碼,就是把Repeater控件內(nèi)的記錄的信息傳給用戶控件。 效果跟沒有使用用戶控件時(shí)沒有兩樣。
??
總結(jié)
以上是生活随笔 為你收集整理的怎样判断RadioButtonList控件是否有选择 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。