asp.net 擴充GridView 增加選項按鈕列的代碼

來源:互聯網
上載者:User

複製代碼 代碼如下:/// <summary>
/// 選項按鈕列
/// </summary>
/// <remarks>
/// 如果沒有設定GroupName,則使用GridView的ID作為GroupName
/// 如果沒有設定DataField,則使用RowIndex作為Value
/// 允許設定DataFormatString格式化資料
/// </remarks>
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
public class RadioButtonSelectField : DataControlField
{
/// <summary>
/// 選項按鈕組名,預設去GridView的ClientID
/// </summary>
[Description("選項按鈕組名,預設去GridView的ClientID")]
public string RadioButtonGroupName
{
get
{
return this.ViewState["RadioButtonGroupName"] as string;
}
set
{
this.ViewState["RadioButtonGroupName"] = value;
}
}

/// <summary>
/// 選項按鈕的樣式
/// </summary>
[Description("選項按鈕的樣式")]
public string RadioButtonCssClass
{
get
{
return this.ViewState["RadioButtonCssClass"] as string;
}
set
{
this.ViewState["RadioButtonCssClass"] = value;
}
}

/// <summary>
/// 要繫結資料運算式
/// </summary>
[Description("要繫結資料運算式")]
public string DataField
{
get
{
return this.ViewState["DataField"] as string;
}
set
{
this.ViewState["DataField"] = value;
}
}

/// <summary>
/// 要繫結資料運算式格式
/// </summary>
[Description("要繫結資料運算式格式")]
public string DataFormatString
{
get
{
return this.ViewState["DataFormatString"] as string;
}
set
{
this.ViewState["DataFormatString"] = value;
}
}

/// <summary>
/// return self;
/// </summary>
/// <returns></returns>
protected override DataControlField CreateField()
{
return this;
}

/// <summary>
/// 添加控制項
/// </summary>
/// <param name="cell"></param>
/// <param name="cellType"></param>
/// <param name="rowState"></param>
/// <param name="rowIndex"></param>
public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
{
base.InitializeCell(cell, cellType, rowState, rowIndex);

if (cellType == DataControlCellType.DataCell)
{
var literal = new Literal();

if (string.IsNullOrEmpty(this.DataField))
{
SetLiteralHtml(literal, rowIndex.ToString());
}
else
{
literal.DataBinding += new EventHandler(literal_DataBinding);
}

cell.Controls.Add(literal);
}
}

void literal_DataBinding(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.DataField))
{
return;
}

var literal = sender as Literal;
if (literal == null)
{
return;
}
var cell = literal.Parent as TableCell;
if (cell == null)
{
return;
}
var container = literal.NamingContainer;
if (container == null)
{
return;
}
bool foundDataItem;
var dataItem = DataBinder.GetDataItem(container, out foundDataItem);
if (!foundDataItem)
{
return;
}

var dataValue = null as string;
if (this.DataField.Contains('.'))
{
dataValue = DataBinder.Eval(dataItem, this.DataField, this.DataFormatString);
}
else
{
dataValue = DataBinder.GetPropertyValue(dataItem, this.DataField, this.DataFormatString);
}

SetLiteralHtml(literal, dataValue);
}

private void SetLiteralHtml(Literal literal, string dataValue)
{
var groupName = this.RadioButtonGroupName;
if (string.IsNullOrEmpty(groupName))
{
groupName = literal.Parent.Parent.Parent.Parent.ID;
}
var cssClass = this.RadioButtonCssClass;
if (!string.IsNullOrEmpty(cssClass))
{
cssClass = string.Format("class=\"{0}\"", this.RadioButtonCssClass);
}

var selected = false;
var selectedValue = literal.Page.Request[groupName];
if (string.IsNullOrEmpty(selectedValue) == false)
{
if (string.Compare(selectedValue, dataValue, true) == 0)
{
selected = true;
}
}

var rbHtml = string.Format("<input type=\"radio\" name=\"{0}\" value=\"{1}\" {2} {3} />",
groupName,
dataValue,
cssClass,
selected ? "checked" : string.Empty);

literal.Text = rbHtml;
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.