zoukankan      html  css  js  c++  java
  • RadioButton in GridView

    在GridView中实现RadioButton的单选功能,主要是从两个方面来考虑
    1.客户端
        在客户端来开发,我们主要考虑两个方面的问题
        1. 怎么取得GridView所在行的主键值
        2. 怎么在选定某个选项,提交给服务器以后,再能保持选项是被选上的
        这个问题可以这样解决,我们知道,在客户端,RadioButton的HTML代码是
        <input type="radio" id="***" name="**" value="*****"  />
        我们可以通过一个Literal空间,在GridView的OnRowCreated事件中动态的来控制这个的HTML代码就可以
        服务器可以通过Request.Form["**"]来得到该行记录的主键
    2.服务器端
        其原理也是修改RadioButton的HTML代码.扩张RadioButton类,写成一个服务器控件.
        相关代码如下:
        
     1using System;
     2using System.Collections.Generic;
     3using System.ComponentModel;
     4using System.Text;
     5using System.Web;
     6using System.Web.UI;
     7using System.Web.UI.WebControls;
     8
     9namespace Utilities
    10{
    11    [DefaultProperty("Text")]
    12    [ToolboxData("<{0}:GridViewRowSelector runat=\"server\"></{0}:GridViewRowSelector>")]
    13    public class GridViewRowSelector : RadioButton
    14    {
    15        protected override void Render(HtmlTextWriter writer)
    16        {
    17            GridViewRow row = (GridViewRow)this.NamingContainer;
    18            int currentIndex = row.RowIndex;
    19            GridView grid = (GridView)row.NamingContainer;
    20            this.Checked = (grid.SelectedIndex == currentIndex);
    21            this.Attributes.Add("onClick""javascript:" + Page.ClientScript.GetPostBackEventReference(grid, "Select$" + currentIndex.ToString(), true));
    22            base.Render(writer);
    23        }

    24
    25    }

        

  • 相关阅读:
    我谈编程语言竞争
    从基础学起----xuld版高手成长手记[1]
    自己动手开发语言.笔记@2014-1-13
    删除 QQ 最新版右键菜单 通过QQ发送文件到手机
    客观评价C#的优点和缺点
    一个会做你就是高手的问题
    计划开发的语言及一些细节求吐槽
    面向接口设计思想
    计划添加的复杂语法
    面向对象中的设计陷阱
  • 原文地址:https://www.cnblogs.com/liuqhui/p/1098936.html
Copyright © 2011-2022 走看看