惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园_首页
Vercel News
Vercel News
Last Week in AI
Last Week in AI
罗磊的独立博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
美团技术团队
U
Unit 42
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
J
Java Code Geeks
V
V2EX
量子位
腾讯CDC
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
D
DataBreaches.Net
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
L
LangChain Blog
C
Check Point Blog

博客园 - 一帆(老鼠粮仓之路)

这个壳为什么这么难,这个软件难道就无法破! 得到IP地址的来源,数据库动网纯真ip数据库mdb 给img添加onclick属性 - 一帆(老鼠粮仓之路) - 博客园 漂浮广告,关闭按钮 - 一帆(老鼠粮仓之路) - 博客园 一个漂亮的js轮换图,在ff下只能切换图片 - 一帆(老鼠粮仓之路) - 博客园 截取指定长度的字符串 新文章显示new字样小图标 全球主要城市时区时差转换计算表 - 一帆(老鼠粮仓之路) - 博客园 完美下拉导航条,适应ie6,ie7和firefox - 一帆(老鼠粮仓之路) - 博客园 google站内搜索,没有和博客园一样最上面的图片 - 一帆(老鼠粮仓之路) - 博客园 一种做事的精神,记师傅陕北吴旗娃 图片大小随鼠标滚动而改变的兼容IE和firefox的代码 一个网友的ajax进度条,出现错误的原因 99收藏夹用atlas重新改版及中文分词在线演示 用gmail发送邮件 梅花给俺的代码集合,以做纪念,感谢梅花的无私帮助! 分析百度的中文分词结果 蜘蛛中可以用到的正则收集 成功将99收藏夹实现基于Lucene的站内全文搜索
在GridView中加入单选按钮RadioButton
一帆(老鼠粮仓之路) · 2007-07-17 · via 博客园 - 一帆(老鼠粮仓之路)

首先鄙视下dudu:不管你是什么文章,如果你加上一句:恳请dudu能够让我在首页挂一天,那么这文章就不会被dudu撤下来的,但是如果你没加,那不好意思,dudu的大刀就会向你举起!这是典型的顺风耳!

这段时间一直为网站的事醉心于代码,除了每天早晨给出交易信号外,基本上没有关注过行情了.但代码仅是我的爱好而已,操盘才是我的正业,趁现在心思还没有完全转到操盘上面来,把这个项目我整理的一些代码记下来,有点意义的就是如何给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>

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>

后台代码:

protected void gridVote_RowDataBound(object sender, GridViewRowEventArgs e) 
    

        PlaceHolder place 
= (PlaceHolder)e.Row.FindControl("placeVote"); 
        
if (place != null
        

            
if (gridVote.DataKeys[e.Row.RowIndex].Values["vote_type"].ToString().ToLower() == "false"
            
{  
               
//单选 
                RadioButton radioVote = new RadioButton(); 
                radioVote.ID 
= "radioVote"
                
string script = "SetUniqueRadioButton('gridVote.*Vote',this)"
                radioVote.Attributes.Add(
"onclick",script); 
                
//绑定数据 
                
//BindItemData(radioVote, Int32.Parse(gridVote.DataKeys[e.Row.RowIndex].Values["this_id"].ToString())); 
                radioVote.Text = gridVote.DataKeys[e.Row.RowIndex].Values["this_title"].ToString(); 
                place.Controls.Add(radioVote); 
                
            }
 
            
if (gridVote.DataKeys[e.Row.RowIndex].Values["vote_type"].ToString().ToLower() == "true"
            

               
//多选 
                CheckBox checkVote = new CheckBox(); 
                checkVote.ID 
= "checkVote"
                checkVote.Text 
= gridVote.DataKeys[e.Row.RowIndex].Values["this_title"].ToString(); 
                place.Controls.Add(checkVote); 
            }
 
        }
 
    }