DataGird中使用RadioButton的一个解决办法

在DataGird中使用RadioButton时,最郁闷的莫过于不能单选,而且没有CommandName,CommandArgument属性了,下面的代码解决了这个问题.
下载代码

using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly:TagPrefix(
"Calfenyin.Web.Controls""calfenyin")]
namespace Calfenyin.Web.Controls
{
    
/// <summary>
    
/// An enhanced radiobutton,especially for use in datagrid,and add commandName and commandArgument property
    
/// </summary>

    
    [ToolboxBitmap(
typeof(RadioButtonPlus),"Resources.RadioButtonPlus.bmp")]
    
public class RadioButtonPlus : CheckBox, IPostBackDataHandler,IPostBackEventHandler
    
{
        
protected override void OnPreRender(EventArgs e)
        
{
            
base.OnPreRender(e);
            
if (((this.Page != null&& !this.Checked) && this.Enabled)
            
{
                
this.Page.RegisterRequiresPostBack(this);
            }

            
if (this.GroupName.Length == 0)
            
{
                
this.GroupName = this.UniqueID;
            }

        }


        
protected override void Render(HtmlTextWriter writer)
        
{
            
this.RenderInputTag(writer,this.ClientID,this.Attributes["onclick"]);
        }


        
private void RenderInputTag(HtmlTextWriter writer, string clientID, string onClick)
        
{
            writer.AddAttribute(HtmlTextWriterAttribute.Id, clientID);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, 
"radio");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, 
this.UniqueGroupName);
            writer.AddAttribute(HtmlTextWriterAttribute.Value, 
this.ValueAttribute);
            
if (this.Checked)
            
{
                writer.AddAttribute(HtmlTextWriterAttribute.Checked, 
"checked");
            }

            
if (!this.Enabled)
            
{
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, 
"disabled");
            }

            
if (this.AutoPostBack)
            
{
                
if (onClick != null)
                
{
                    onClick 
= onClick + this.Page.GetPostBackClientEvent(this"");
                }

                
else
                
{
                    onClick 
= this.Page.GetPostBackClientEvent(this"");
                }

                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
                writer.AddAttribute(
"language""javascript");
            }

            
else if (onClick != null)
            
{
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
            }

            
string accessKey = this.AccessKey;
            
if (accessKey.Length > 0)
            
{
                writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, accessKey);
            }

            
int tabIndex = this.TabIndex;
            
if (tabIndex != 0)
            
{
                writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, tabIndex.ToString(NumberFormatInfo.InvariantInfo));
            }

            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
        }


        
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
        
{
            
string strValue = postCollection[this.UniqueGroupName];
            
bool bFound = false;
            
if ((strValue != null&& strValue.Equals(this.ValueAttribute))
            
{
                
if (!this.Checked)
                
{
                    
this.Checked = true;
                    bFound 
= true;
                }

                
return bFound;
            }

            
if (this.Checked)
            
{
                
this.Checked = false;
            }

            
return bFound;
        }



        
void IPostBackDataHandler.RaisePostDataChangedEvent()
        
{
            
this.OnCheckedChanged(EventArgs.Empty);
        }


        
public virtual string GroupName
        
{
            
get
            
{
                
string strGroupName = (stringthis.ViewState["GroupName"];
                
if (strGroupName != null)
                
{
                    
return strGroupName;
                }

                
return string.Empty;
            }

            
set
            
{
                
this.ViewState["GroupName"= value;
            }

        }


        
private string UniqueGroupName
        
{
            
get
            
{
                
return this.GroupName;
            }

        }


        
private string ValueAttribute
        
{
            
get
            
{
                
return this.UniqueID;
            }

        }


        [DefaultValue(
"")]
        
public string CommandArgument
        
{
            
get
            
{
                
string strCommandArgument = (stringthis.ViewState["CommandArgument"];
                
if (strCommandArgument != null)
                
{
                    
return strCommandArgument;
                }

                
return string.Empty;
            }

            
set
            
{
                
this.ViewState["CommandArgument"= value;
            }

        }


        [DefaultValue(
"")]
        
public string CommandName
        
{
            
get
            
{
                
string strCommandName = (stringthis.ViewState["CommandName"];
                
if (strCommandName != null)
                
{
                    
return strCommandName;
                }

                
return string.Empty;
            }

            
set
            
{
                
this.ViewState["CommandName"= value;
            }

        }


        
#region IPostBackEventHandler Members

        
public void RaisePostBackEvent(string eventArgument)
        
{
            
this.RaiseBubbleEvent(this,new CommandEventArgs(this.CommandName, this.CommandArgument));
        }


        
#endregion

    }

}

posted on 2005-07-01 15:05  永远有多远  阅读(1699)  评论(6编辑  收藏  举报

导航