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

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - 汗水房

LINQ to sQL:业务层多个Class Library,or 只用一个? window.open() 第2次进入不执行 page_load 中的代码 MicrosoftMiles: AJAX Updatepanel中保持Gridview的滚动位置 转载:Ajax中“Sys未定义”错误的解决方法 - 汗水房 - 博客园 翻译:LINQ to SQL N-层智能客户端 - 第一部分 构建中间层 插件开发时代 Adobe的RIA平台 使用Flash的数据可视化开发(Flash Data Visualization) 转载自Microsoft:每个开发人员现在应该下载的十种必备工具 转载:Google API简介 转载:移动widget玩玩可以 LINQ与Entity Framework以及其它新特性 转载LINQ to SQL(LINQ2SQL) vs. ADO.NET Entity Framework(ADOEF)-ccBoy版 LINQ to SQL建立独立的BLL实体层 LINQ in Action笔记 Architecting Your Data Access Layer with the Entity Framework | Architecting with .NET 转摘好文:Barry Gervin's Software Architecture Perspectives : The Entity Framework vs. The Data Access Layer (Part 1: The EF as a DAL) LinQ to SQL系统的层次架构设计 Linq-To-Sql Business Layer - 汗水房
CodeProject: 自定义控件实现Radiobox选择Gridview一行
汗水房 · 2009-09-11 · via 博客园 - 汗水房

这是一个不错的实现方式。我已经将其应用于我的项目中了。

Simple GridView Radio Button Row Selector

By Adam Crawford

Introduction

This article provides a drag and drop way of creating radio button GridView row selectors that behave in the same way as the GridView's own "Select" link buttons.

Background

One thing that's always bothered me about the GridView control is the counter-intuitive appearance of the Select link provided to select a single row. Since pretty much every other time you ask your user to select one item in a list you do it with radio buttons, I couldn't help but feel it would have been more intuitive to make the Select button a radio button. As it happens, this was one of the first things I thought when I started learning ASP.NET two years ago. But now, it didn’t exactly prove trivial to implement. I found some articles telling me how to do it, but these involved a bit of wiring per control and, really, if you're going to apply radio button row selection across your entire site, it should be much simpler than that just from a maintainability point of view.

Now that I've had more experience and studied for a few qualifications, I came up against the same issue again, and decided to solve it properly so I wouldn't have to worry about it anymore. I wanted a pure drag and drop control with no wiring, to replace the Select link, and this is what I came up with.

The Code

Since it's a pretty simple piece of code all in all, you can see the control in all its glory below:

[DefaultProperty("Text")]
[ToolboxData("<{0}:GridViewRowSelector 
  runat="\""server\"></{0}:GridViewRowSelector>")]
public class GridViewRowSelector : RadioButton
{
    protected override void Render(HtmlTextWriter writer)
    {
        GridViewRow row = (GridViewRow)this.NamingContainer;
        int currentIndex = row.RowIndex;
        GridView grid = (GridView)row.NamingContainer;
        this.Checked = (grid.SelectedIndex == currentIndex);
        this.Attributes.Add("onClick", "javascript:" + 
             Page.ClientScript.GetPostBackEventReference(grid, 
             "Select$" + currentIndex.ToString(), true));
        base.Render(writer);
   }
}

Using the Code

Using the code is simple, just follow these steps:

  1. Click on your website in the Solution Explorer and click "Add Reference..."
  2. Select the Browse button on the .NET Components tab, or select the Browse tab in VS2008.
  3. Select the DLL provided (this is the Utilities.dll found in the Debug/bin directory on the zip file).

Then, just drag and drop the GridViewRowSelector from your toolbox into a template field on the GridView control.

To create the template field on the GridView, select the Common Functions menu for your GridView (the little arrow at the top right corner in Design view), select Add Column, and then add a TemplateColumn with no header text.

To get the designer up that allows you to drag an element into the template field, select the Common Functions menu again for your GridView, then select “Edit Templates”. You should be able to select the ItemTemplate for your template column and drag the GridViewRowSelector into the appropriate box on the designer. Do not worry that it displays an error – this is simply because I haven’t yet added an appropriate control designer that deals with the design mode. I may do this later, but feel it’s not necessary since the control displays correctly at design time outside of template editing.

If you set the GridView's SelectedRow style to display with a different background colour, then your selected row will be highlighted in the colour as well (as shown in the picture at the top of the article).

Points of Interest

The hoops that had to be jumped through to get this working really stem from one major problem in the GridView control: the absence of a SelectRow function that allows you to select a row and fire the GridView.SelectionChanged event. If you can make do without the GridView.SelectionChanged event, then the process of creating a control to select the row isn’t too bad. You can just catch the CheckedChanged event of your RadioButton, update the GridView.SelectedIndex with the row number that the radio button belongs to, and away you go. However, I like using the GridView.SelectionChanged event in my pages to display subsidiary data (particularly now that AJAX is in the mix) so, since I’ll be using this control quite a bit, I really don’t want to work that particular piece of functionality.

Since there isn’t a GridView.SelectRow method and there isn’t a way of invoking the SelectionChanged event on the GridView since it’s protected, the only way I could realistically think to do this was to create a client-side onClick event handler that would fire in exactly the same way as the Select link of the GridView itself, thus fooling it into thinking its own Select link had been clicked.

Of course, you can’t just fire the same JavaScript command to impersonate a postback from the Select link as ASP.NET provides measures against this for security reasons (such as preventing XSS attacks), so you need to register the postback link.

The simple bit was keeping radio button display in line with that of the selected row. The server control itself is just an extended radio button that updates its own Checked property during the Render event by navigating up its control parent hierarchy to reach the GridView row that it’s contained by, then casting to a GridViewRow object and using the Selected property of the row.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Adam Crawford

Member

Adam lives in York and is just starting a new job as a senior web developer for a major travel company in the UK. He holds an MCPD for Web Development in ASP.NET 2 and has an interest in further developing his AJAX skills in the near future and will be practicing by developing his own site: Solid Code

Occupation:
Web Developer

Location:
England England

CodeProject: Simple GridView Radio Button Row Selector. Free source code and programming help