这段时间一直为网站的事醉心于代码,除了每天早晨给出交易信号外,基本上没有关注过行情了.但代码仅是我的爱好而已,操盘才是我的正业,趁现在心思还没有完全转到操盘上面来,把这个项目我整理的一些代码记下来,有点意义的就是如何给GridView加入单选按钮了.
在网上查阅了一些文章,无非就是:
1,放一个html单选控件:<input type="radio"...
2,放一个RadioButtonList;
这两种方法都有弊端;第一种如果我们要在后台取radiobutton的值就显得很麻烦,第二种会让我们不好布局,但是大家知道,如果仅仅是在GridView里面加入RadioButton,我们就实现不了单选,后面查阅了一老外的文章,找到了一种自认为比较好的方法,实现思路是:在后台给radiobutton加入个onclick属性,在前台当用户单击这个单选按钮的时候用js去遍历,取消其他单选的选择,下面是配合一个投票系统的代码,这个投票系统能够在后台自动生成是单选还是复选按钮,最后面的是一个实例.demo请见: http://www.yinuof.com/topiccontent.aspx?id=135&class=5
1
<
asp:Panel ID
=
"
literalVote
"
runat
=
"
server
"
Visible
=
"
false
"
>
2
<
asp:GridView ID
=
"
gridVote
"
runat
=
"
server
"
AutoGenerateColumns
=
"
False
"
BackColor
=
"
#B6C9E7
"
CssClass
=
"
tableBack
"
CellPadding
=
"
3
"
CellSpacing
=
"
1
"
ShowHeader
=
"
False
"
Width
=
"
100%
"
DataKeyNames
=
"
this_id,vote_type,this_title
"
OnRowDataBound
=
"
gridVote_RowDataBound
"
>
3
<
Columns
>
4
<
asp:TemplateField
>
5
<
ItemTemplate
>
6
<
asp:PlaceHolder ID
=
"
placeVote
"
runat
=
"
server
"
/>
7
</
ItemTemplate
>
8
<
ItemStyle BackColor
=
"
White
"
/>
9
</
asp:TemplateField
>
10
<
asp:TemplateField
>
11
<
ItemTemplate
>
12
<
asp:Image ID
=
"
voteImage
"
Runat
=
"
server
"
Height
=
"
8px
"
Width
=
'
<%# FormatImage(Convert.ToInt32(Eval("vote_num").ToString())) %>
'
ImageUrl
=
"
img/bar2.gif
"
></
asp:Image
><%
#Eval(
"
vote_num
"
)
%>
13
</
ItemTemplate
>
14
<
ItemStyle Width
=
"
50%
"
BackColor
=
"
White
"
/>
15
</
asp:TemplateField
>
16
</
Columns
>
17
</
asp:GridView
>
18
<
div style
=
"
margin-top:5px;font-size:12px;color:#000000;
"
>
19
<
asp:Button UseSubmitBehavior
=
"
false
"
Text
=
"
我要投票
"
ID
=
"
buttonVote
"
runat
=
"
server
"
OnClick
=
"
buttonVote_Click
"
/>&
nbsp;【截至时间:
<
asp:Literal ID
=
"
literalTimeout
"
runat
=
"
server
"
/>&
nbsp;
|&
nbsp;
<
a href
=
'
voteuser.aspx?id=<%=Request.QueryString["id"] %>
'
class
=
"
aTitle
"
target
=
"
_blank
"
>
查看投票用户
</
a
>
】
20
</
div
>
21
</
asp:Panel
>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

js代码:
1
<
script language
=
"
javascript
"
>
function SetUniqueRadioButton(nameregex, current)
2
{
3
re = new RegExp(nameregex);
4
for(i = 0; i < document.forms[0].elements.length; i++)
5
{
6
elm = document.forms[0].elements[i]
7
if (elm.type == 'radio')
8
{
9
if (re.test(elm.name))
10
{
11
elm.checked = false;
12
}
13
}
14
}
15
current.checked = true;
16
}
17
</
script
>

2



3

4

5



6

7

8



9

10



11

12

13

14

15

16

17

后台代码:



































