Title: A Solution to Using Radio Buttons in Data Grid When
终极管理员 知识笔记 115阅读
When using radio buttons in data grid, the most depressing thing is that he can't select radio buttons, and he has no command name and command parameter attributes. The following code solves this problem. Download code to use the system; Use the system. Collection. Specialization; Use the system. Component model; Use the system. Drawing; Use the system. Globalization; Use the system. Network. UI uses the system. Network. UI。 WebControls[assembly : tag prefix ('Calfenyin. network. Controls'' calfenyin')] namespace calfenyin. Network. Controls {/* */8,128,1)'//summary//AnenchedRadioButton, especially foruseindatagrid, and AddCommandNameandCommandParameter Property///summary [Toolbox bitmap (type of (radiobutton plus),' Resources. radio button plus . BMP ')]publiclassradiobutton plus 3360 checkbox,IPostBackDataHandler,IPostBackEventHandler { protected
0, 0, 255, 1)">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 = (string) this.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 = (string) this.ViewState["CommandArgument"];
if (strCommandArgument != null)
{
return strCommandArgument;
}
return string.Empty;
}
set
{
this.ViewState["CommandArgument"] = value;
}
}
[DefaultValue("")]
public string CommandName
{
get
{
string strCommandName = (string) this.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
}
}
