CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
花落谁家,你作主! 盛大widget设计大赛英雄榜
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

关于 DataGrid 的模版列放入 RadioButtonList的问题!!给分100

楼主xjhlzl(花乌鸦)2005-03-11 10:56:56 在 .NET技术 / C# 提问

DataGrid   的模版列放入   RadioButtonList   现在想更具更具所选择的项取该行   DataGrid   中其他列的值。  
  说明:   该页为弹出窗口,取完值后要给他的父窗口的文本框负值,所以考虑用javaScript来实现,请各位高手给点建议,或者贴点代码上来,大家讨论,昨天一天都没有搞定,现在很郁闷!! 问题点数:100、回复次数:21Top

1 楼heqheq(小荷)回复于 2005-03-11 11:04:43 得分 5

up~Top

2 楼JeasonZF(哭泣的鱼)回复于 2005-03-11 11:09:37 得分 5

頂!Top

3 楼CSDNATM(飞行员,麻烦帮开个窗让我透透气)回复于 2005-03-11 11:09:59 得分 1

我头晕,看不明白  
  Top

4 楼yzsb1118(我)回复于 2005-03-11 11:12:03 得分 1

太明显了吧!Top

5 楼xjhlzl(花乌鸦)回复于 2005-03-11 11:14:39 得分 0

怎么做呀!谁给说说呀!  
  js   中如何取DataGrid   某一行的值   实在是不会!!   谢谢各位大哥帮帮忙!!解决了马上送分Top

6 楼xjhlzl(花乌鸦)回复于 2005-03-11 11:26:12 得分 0

简单点说吧!!  
  第一先判断 RadioButtonList  中radio的个数  
      document.Form1.DataGrid1.RadioButtonList1.Items.Count 这个方法我没有取出出来个数。返回 num  
  第二更具个数循环,判断那个按钮别选择  
        var   row   ;  
    for(i=0;i<num;i++)  
  {  
  obj   =   eval("document.forms[0].DataGrid1.RadioButtonList1_"+i);  
  if(obj.checked   ==   true)  
  {  
  alert(obj.value);  
                row   =i;  
   
  }  
  }  
                   
  第三更具返回的行号取DataGrid 该行需要列的值  
                      这一步不会写  
               
  第四给父窗口负值  
      window.opener.document.forms[0].txtID.value=dgValue;  
   
  大体思想就是这样的。各位大哥帮帮忙。  
  对了还有就是怎么写 RadioButtonList 的onclick 事件!!  
  Top

7 楼xjhlzl(花乌鸦)回复于 2005-03-11 11:35:42 得分 0

自己帮忙顶顶,在线等待向大家请教!Top

8 楼LoveCherry(论成败,人生豪迈;大不了,重头再来!^_^)回复于 2005-03-11 11:39:30 得分 1

参考Top

9 楼LoveCherry(论成败,人生豪迈;大不了,重头再来!^_^)回复于 2005-03-11 11:39:33 得分 40

http://dev.csdn.net/article/64/64292.shtmTop

10 楼xjhlzl(花乌鸦)回复于 2005-03-11 12:12:07 得分 0

看了!对document.getElementsByName('rad').length   这句我测试了,可是不管用  
  我的'RadioButtonList1'是id   所以我这样使用的     document.getElementsById('RadioButtonList1').length  
   
  报错:缺少对象Top

11 楼xjhlzl(花乌鸦)回复于 2005-03-11 12:20:38 得分 0

又测了,可以但是输出是   0   上来给你说一声,我继续测试!!Top

12 楼niunj(南航牛)回复于 2005-03-11 12:24:00 得分 10

如果你想在编辑的时候找到这个radiobuttonlist就要在itemcreate或者itemdatabind事件里面用e.item.findcontrol来寻找这个控件Top

13 楼syeerzy(快乐永远*先天下之乐而乐*后天下之忧而忧*)回复于 2005-03-11 12:28:13 得分 5

(System.Web.UI.WebControls.RadioButtonList)thisItem.Cells[0].Controls[1]  
  Top

14 楼amendajing(学习,是个漫长的旅途!)回复于 2005-03-11 12:34:46 得分 5

markTop

15 楼xjhlzl(花乌鸦)回复于 2005-03-11 12:44:00 得分 0

to   niunj   and   syeerzy(快乐永远)    
  我现在是想些,JavaScript   来实现如何取到   DataGrid   中RadioButtonList   选中的是那一行  
  过程:  
  判断RadioButtonList的长度——〉更具长度循环判断那一行被选中——〉找   DataGrid   某一列   对应行的值——〉把值传给父窗口  
  可是我才进行到第一步就搞不定。不知道该如何写!  
  多谢大家捧场,先谢谢了!Top

16 楼xjhlzl(花乌鸦)回复于 2005-03-11 15:01:49 得分 0

自己顶!!顶!!Top

17 楼xjhlzl(花乌鸦)回复于 2005-03-11 21:40:13 得分 0

感谢   LoveCherry  
  好好读了你那篇文章,最后还是按照你那个做了挺不错的利用cs文件输出js代码挺好用的  
  我把代码大致贴一下  
  ------------------------------------cs文件------------------------  
  private   void   Button2_Click(object   sender,   System.EventArgs   e)  
  {  
  try{  
  rd.Value   =   Request.Form["rad"].ToString();  
  string   employeeName   =   DataGrid1.Items[Convert.ToInt32(rd.Value)-1].Cells[2].Text;  
  string   javascp=@"  
                                                                  <script>window.opener.document.forms[0].txtID.value='"+employeeName+"';    
   
  window.close();     </script>";  
  this.RegisterStartupScript("javascp",javascp);  
  }  
  catch  
  {  
    Page.RegisterStartupScript("alert","<script   language=javascript>alert('请选择用户!')  
   
  </script>");  
  }  
    }  
  这个事件中主要就是给父窗口负值,利用try catch 减少第一次dg中没有值得时候会抱错的情况。  
   
  //保存RadioButton   的选择状态  
   
  private   void   BandRadioButton()  
  {  
  string   js   =   "";  
  js+="<script>\r\n";  
  js+="function   ld(){\r\n";  
  js+="for(i=0;i<document.getElementsByName('rad').length;i++)\r\n";  
  js+="if(document.getElementsByName('rad')[i].value==";  
  js+="document.getElementById('"   +   this.rd.ClientID   +   "').value)   ";  
  js+="document.getElementsByName('rad')[i].checked=true\r\n";  
  js+="}\r\n";  
  js+="window.onload=ld\r\n";  
  js+="</script>\r\n";  
  this.RegisterStartupScript("js",js);  
   
  }  
  把BandRadioButton放在Page_Load中  
  ----------------------------------------页面文件----------------------------------------  
  <asp:TemplateColumn   HeaderText="选择">  
  <ItemTemplate>  
  <INPUT   type=radio   CHECKED   value="<%#    
   
  Container.ItemIndex+1%>"   name=rad>  
  </ItemTemplate>  
  </asp:TemplateColumn>  
  这是页面文件主要是加在模版列中设置,radioButtion  
  string   javascp=@"  
                                                                  <script>window.opener.document.forms[0].txtID.value='"+employeeName+"';   window.close();     </script>";  
  this.RegisterStartupScript("javascp",javascp);  
  }  
  catch  
  {  
    Page.RegisterStartupScript("alert","<script   language=javascript>alert('请选择用户!')</script>");  
  }  
    }  
  这个事件中主要就是给父窗口负值,利用try catch 减少第一次dg中没有值得时候会抱错的情况。  
   
  Top

18 楼Grisson(海盗-王)回复于 2005-03-11 21:53:00 得分 1

顶!!Top

19 楼fccvip(www.anagenesis.cn)回复于 2005-03-12 10:56:44 得分 11

在DataGrid中选择,确认,删除多行复选框列表    
   
   
     
  Selecting,   Confirming   &   Deleting   Multiple   Checkbox   Items   In   A   DataGrid   (i.e.   HotMail   &   Yahoo)  
   
  Introduction  
   
  Although   I   don't   have   either   a   Hotmail   or   Yahoo   email   account,   I   do   have   friends   that   do,   and   I   have   often   seen   them   check   their   e-mail   and   noticed   how   it   was   all   neatly   displayed   in   a   table.   Right   next   to   each   item   in   their   e-mail   table   was   a   simple   little   checkbox.   At   the   bottom   or   top   of   this   table   you   would   have   another   checkbox   giving   them   the   ability   to   select   all   of   the   e-mail,   and   a   button   to   submit   back.   Once   this   was   done   they   would   receive   a   nice   popup   alert   to   confirm   what's   about   to   happen,   and   then   once   this   is   OK'   ed,   boom,   all   the   selected   items   are   deleted.  
   
  .NET,   of   course,   has   the   DataGrid,   their   finest   and   most   popular   control   to   date,   and   it   should   be   pretty   easy   to   get   the   same   functionality,   right?   Well,   not   quite.   Doing   this   with   .NET   is   not   that   straightforward   and   functional   as   I   would've   liked   it   to   be.   Sure   it's   quite   easy   to   add   an   edit   /   update   button   or   link   to   each   row   and   even   a   delete   button   to   each,   alongside   a   pop   up   alert   as   well!   However,   applying   this   delete   feature   to   each   and   every   button   and   deleting   them   one   by   one   is   a   little   maddening.   Nor   is   the   DataGrid   set   up   in   allowing   a   way   of   adding   a   main   "select   all"   checkbox   to   easily   select   all   of   the   boxes,   and   then   applying   any   particular   action.  
   
  In   this   article,   we   will   examine   how   to   create   a   fully   functional   DataGrid   with   all   the   features   you'd   find   set   up   on   Hotmail   or   Yahoo.   As   an   added   bonus   we'll   be   performing   all   of   our   data   tasks   strictly   utilizing   Microsoft's   new   Data   Access   Application   Block   or   DAAB   v2.   To   any   who   may   feel   a   little   behind   with   DAAB,   have   no   fear,   everything   here   can   still   be   accomplished   with   pre-DAAB   data   objects   as   the   only   difference   here   is   the   data   access   part   of   it.   Trust   me   there   is   a   huge   difference   between   the   two,   for   one   DAAB   enable   you   to   write   about   75%   less   code   that   you   would   normally   need   when   compared   with   regular   ADO.NET!  
   
  So,   before   we   begin,   download   the   DAAB   dll   from   the   above   link,   install   it,   and   copy   it   into   you   application’s   bin   folder   and   you're   ready   to   go.   Also,   be   sure   and   take   a   peek   at   the   DAAB   documentation   that   came   with   your   installation   for   an   overview   and   any   questions   you   may   have.  
   
  Ok,   let's   get   to   it   then.  
   
     
   
  Our   fully-featured   DataGrid  
   
  Selecting   &   deleting   multiple   items   will   definitely   be   set   up   quite   differently   than   any   other   type   of   .NET   DataGrid   deleting   you   probably   have   seen.   However,   we'll   still   follow   the   same   logical   flow   of   deletion,   and   we'll   still   confirm   any   delete   actions   about   to   take   place   after   we   have   selected   all   our   items.   Much   of   the   magic   in   making   this   work   is   really   going   to   come   from   client-side   JavaScript,   that   is   ultimately   responsible   for   wiring   up   our   main   "select   all"   checkbox   to   handle   the   selecting   and   deselecting   of   our   checkboxes.   Also,   included   is   our   server-side   delete   method   that   erases   our   data,   and   a   DataGrid   refresher   method   to   rebind   our   DataGrid   after   we   have   completed   our   data   deletion.  
   
  Have   a   look   at   Figure   1   to   get   an   idea   of   what   your   DataGrid   will   look   like:  
   
   
   
  Figure   1  
   
  Here   is   the   code   to   set   up   our   DataGrid:  
   
     
  Top

20 楼fccvip(www.anagenesis.cn)回复于 2005-03-12 10:57:17 得分 10

<form   runat="server">    
  <h3>Selecting,   Confirming   &   Deleting   Multiple   Checkbox   Items   In   A   DataGrid   (i.e.   HotMail   &   Yahoo)</h3>  
  <br>  
   
  <ASP:DataGrid   id="MyDataGrid"   runat="server"  
  Width="700"  
  BackColor="white"    
  BorderColor="black"  
  CellPadding=3    
  CellSpacing="0"  
  Font-Size="9pt"  
  AutoGenerateColumns="False"  
  HeaderStyle-BackColor="darkred"  
  HeaderStyle-ForeColor="white"  
  >  
   
  <Columns>  
   
  <asp:TemplateColumn>  
   
  <HeaderTemplate>  
            <asp:CheckBox   ID="CheckAll"   OnClick="javascript:   return   select_deselectAll   (this.checked,   this.id);"   runat="server"/>    
            <font   face="Webdings"   color="white"   size="4">a</font>  
  </HeaderTemplate>  
   
  <ItemTemplate>  
            <asp:CheckBox   ID="DeleteThis"   OnClick="javascript:   return   select_deselectAll   (this.checked,   this.id);"   runat="server"/>  
  </ItemTemplate>  
   
  </asp:TemplateColumn>  
   
  //   ...   Rest   of   our   Custom   Template   &   Bound   Columns  
   
  </Columns>  
   
  </form>  
     
   
  The   code   listed   above   is   what   makes   our   DataGrid   set   up   behave   just   like   the   grids   on   Hotmail   and   Yahoo.   Our   .NET   DataGrid   will   have   the   same   functionality   and   options   available   for   selecting   however   many   items,   or   perhaps   all,   that   you'd   like   to   delete,   and   once   you   do   and   submit,   kiss   them   goodbye.  
   
     
   
  DataGrid   Setup  
   
  The   first   step   towards   creating   this   functionality   is   to   begin   by   adding   a   new   DataGrid   column   (either   before   or   after   our   main   data,   it   doesn't   matter)   that'll   contain   our   individual   checkboxes,   but   also   include   our   main   top   "select   all"   checkbox   (if   and   when   we   need   to   select   and   deselect   all   the   checkboxes   below   it).   To   do   this   we   first   make   sure   our   DataGrid's   AutoGenerateColumns   property   is   set   to   false,   since   we're   going   to   create   the   necessary   custom   Header   and   Item   Templates,   alongside   our   necessary   BoundColumns,   within   our   DataGrid   Columns   section   (that   can   be   customized   and   could   contain   whatever   you   need   them   to).  
   
  The   only   important   thing   to   notice   here,   regarding   our   objective,   is   our   first   TemplateColumn   that   contains   our   checkboxes.   The   main   focal   point   with   this   whole   application   rests   solely   on   this   first   Template   Column   and   its   two   Sub   Templates:   HeaderTemplate   and   ItemTemplate.   Each   contain   the   necessary   checkboxes,   both   are   wired   to   onClick   events   that   executes   select_deselectAll   (our   super-slick   client-side   JavaScript   method)   when   either   checkbox   is   checked,   and   in   turn   are   fully   responsible   for   giving   us   all   the   great   functionality   I've   spoken   of.   Incidentally,   the   WebDings   font's   lowercase   letter   "a"   give   us   our   check   symbol.  
   
     
   
  Selecting   and   De-Selecting   our   Checkboxes  
   
  Now   that   both   checkboxes   are   wired   to   our   multi-faceted   JavaScript   method,   how   is   that   one   function   going   to   determine   the   checkbox   its   dealing   with,   and   the   action   it   needs   to   carry   out?   Ah,   here's   how   :-)  
   
  Our   function   select_deselectAll,   listed   below,   accepts   two   arguments:   the   Checkbox's   checked   value,   and   its   ID.   Once   this   function   is   called,   and   its   two   arguments   have   been   passed   in,   it'll   begin   looping   through   our   form.   Next,   it   begins   performing   some   conditional   checking   utilizing   JavaScript's   indexOf   method   to   locate   the   appropriate   checkbox,   and   is   based   on   both   the   values   passed   in,   which   it   turn   ultimately   will   give   us   one   of   several   causes   and   effects:  
   
  If   the   main   "select   all"   checkbox   is   checked,   it   will   select   all   of   the   DataGrid   checkboxes    
  If   the   main   "select   all"   checkbox   is   unchecked,   then   all   of   the   DataGrid   checkboxes   get   unselected    
  Finally,   if   after   the   main   "select   all"   checkbox   is   selected   and   all   of   the   DataGrid's   checkboxes   are   all   checked,   any   one   of   those   checkboxes   gets   unchecked,   then   the   main   checkbox   is   also   unchecked.   This   way   we   don't   end   up   having   our   checkbox's   logic   behaving   inconsistently   or   erratically.  
   
  function   select_deselectAll   (chkVal,   idVal)   {    
  var   frm   =   document.forms[0];  
   
  //   Loop   through   all   elements  
  for   (i=0;   i<frm.length;   i++)   {  
   
  //   Look   for   our   Header   Template's   Checkbox  
  if   (idVal.indexOf   ('CheckAll')   !=   -1)   {  
   
  //   Check   if   main   checkbox   is   checked,   then   select   or   deselect   datagrid   checkboxes    
  if(chkVal   ==   true)   {  
   
  frm.elements[i].checked   =   true;  
   
  }   else   {  
   
  frm.elements[i].checked   =   false;  
   
  }  
   
  //   Work   here   with   the   Item   Template's   multiple   checkboxes  
  }   else   if   (idVal.indexOf   ('DeleteThis')   !=   -1)   {  
   
  //   Check   if   any   of   the   checkboxes   are   not   checked,   and   then   uncheck   top   select   all   checkbox  
  if(frm.elements[i].checked   ==   false)   {  
   
  frm.elements[1].checked   =   false;   //Uncheck   main   select   all   checkbox  
   
  }  
   
  }  
   
  }  
   
   
  }  
     
   
     
   
  Figure   2   shows   you   the   effect   of   the   JavaScript   above   interacting   with   the   DataGrid   when   selecting   the   top   main   "select   all"   checkbox.  
   
   
   
  Figure   2  
   
  Top

21 楼fccvip(www.anagenesis.cn)回复于 2005-03-12 10:58:08 得分 5

。。。。。。。。。。。。。。  
   
  作者Blog:http://blog.csdn.net/hbzxf/Top

相关问题

  • 新手问题: DataGrid中使用了模版列,放入RadioButtonList,怎样获得该RadioButtonList选中哪一项?
  • datagrid 模版的小问题?
  • DataGrid模版列问题
  • DataGrid模版列的排序问题
  • 急!datagrid模版列的问题
  • 关于DataGrid和DataList的模版
  • 如何取得DataGrid模版里TextBox值
  • 模版
  • DataGrid中的模版列中添加LinkButton问题
  • 怎样在DataGrid中"绑定"一个模版列?

关键词

  • .net
  • 模版
  • datagrid
  • js
  • 选择
  • javascp
  • radiobuttonlist
  • 模版列
  • registerstartupscript
  • 负值

得分解答快速导航

  • 帖主:xjhlzl
  • heqheq
  • JeasonZF
  • CSDNATM
  • yzsb1118
  • LoveCherry
  • LoveCherry
  • niunj
  • syeerzy
  • amendajing
  • Grisson
  • fccvip
  • fccvip
  • fccvip

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo