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

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

None.gif using  System;
None.gif
using  System.Collections.Specialized;
None.gif
using  System.ComponentModel;
None.gif
using  System.Drawing;
None.gif
using  System.Globalization;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
None.gif[assembly:TagPrefix(
" Calfenyin.Web.Controls " " calfenyin " )]
None.gif
namespace  Calfenyin.Web.Controls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// An enhanced radiobutton,especially for use in datagrid,and add commandName and commandArgument property
ExpandedSubBlockEnd.gif    
/// </summary>

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

InBlock.gif            
if (this.GroupName.Length == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.GroupName = this.UniqueID;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Render(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.RenderInputTag(writer,this.ClientID,this.Attributes["onclick"]);
ExpandedSubBlockEnd.gif        }

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

InBlock.gif            
if (!this.Enabled)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, 
"disabled");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (this.AutoPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (onClick != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    onClick 
= onClick + this.Page.GetPostBackClientEvent(this"");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    onClick 
= this.Page.GetPostBackClientEvent(this"");
ExpandedSubBlockEnd.gif                }

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

InBlock.gif            
else if (onClick != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
string accessKey = this.AccessKey;
InBlock.gif            
if (accessKey.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, accessKey);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
int tabIndex = this.TabIndex;
InBlock.gif            
if (tabIndex != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, tabIndex.ToString(NumberFormatInfo.InvariantInfo));
ExpandedSubBlockEnd.gif            }

InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Input);
InBlock.gif            writer.RenderEndTag();
ExpandedSubBlockEnd.gif        }

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

InBlock.gif                
return bFound;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (this.Checked)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Checked = false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return bFound;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
void IPostBackDataHandler.RaisePostDataChangedEvent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.OnCheckedChanged(EventArgs.Empty);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public virtual string GroupName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strGroupName = (stringthis.ViewState["GroupName"];
InBlock.gif                
if (strGroupName != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return strGroupName;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return string.Empty;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ViewState["GroupName"= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string UniqueGroupName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.GroupName;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string ValueAttribute
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.UniqueID;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DefaultValue(
"")]
InBlock.gif        
public string CommandArgument
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strCommandArgument = (stringthis.ViewState["CommandArgument"];
InBlock.gif                
if (strCommandArgument != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return strCommandArgument;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return string.Empty;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ViewState["CommandArgument"= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DefaultValue(
"")]
InBlock.gif        
public string CommandName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strCommandName = (stringthis.ViewState["CommandName"];
InBlock.gif                
if (strCommandName != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return strCommandName;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return string.Empty;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ViewState["CommandName"= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
IPostBackEventHandler Members#region IPostBackEventHandler Members
InBlock.gif
InBlock.gif        
public void RaisePostBackEvent(string eventArgument)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.RaiseBubbleEvent(this,new CommandEventArgs(this.CommandName, this.CommandArgument));
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/calfenyin/archive/2005/07/01/184630.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值