[英]ASP.NET Gridview - setting column header's css class from code behind?
[英]ASP.NET Setting HtmlInputRadioButton name in GridView from code behind
在RowDataBound
一个GridView的事件,我设置的名称HtmlInputRadioButton
里面GridViewRow
。 问题是asp.net自动使名称唯一,因此取消组合单选按钮。
有没有办法禁用它?
编辑 - 以下是我设置名称的方式:
Private Sub gvFoo_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSecPos.RowDataBound
If Not (e.Row.RowType = DataControlRowType.DataRow) OrElse (e.Row.Cells(0) Is Nothing) Then
Return
End If
Dim drFoo As DataRowView = CType(e.Row.DataItem, DataRowView)
Dim rbFoo As HtmlInputRadioButton = CType(e.Row.FindControl("rbFoo"), HtmlInputRadioButton)
rbFoo.Name = "Foo" 'ASP.NET makes this unique which I do not want
rbFoo.Value = "A Value"
End Sub
生成这个HTML
<input type="radio" id="ctl00_ContentPlaceHolder1_ucFoo_gvFoo_ctl06_rbFoo" name="ctl00$ContentPlaceHolder1$ucFoo$gvFoo$ctl06$Foo" value="A Value">
<input type="radio" id="ctl00_ContentPlaceHolder1_ucFoo_gvFoo_ctl07_rbFoo" name="ctl00$ContentPlaceHolder1$ucFoo$gvFoo$ctl07$Foo" value="A Value">
代替
<input type="radio" id="ctl00_ContentPlaceHolder1_ucFoo_gvFoo_ctl06_rbFoo" name="Foo" value="A Value">
<input type="radio" id="ctl00_ContentPlaceHolder1_ucFoo_gvFoo_ctl07_rbFoo" name="Foo" value="A Value">
由于各个单选按钮的名称中的数字“106”和“107”,它将它们取消组合。
您可以通过覆盖UniqueID的getter来覆盖单选按钮的名称
private class MyHtmlInputRadioButton : HtmlInputRadioButton
{
public override string UniqueID
{
get
{
return string.IsNullOrEmpty(Name) ? base.UniqueID : Name;
}
}
}
我认为你应该使用普通的单选按钮。 <input type="radio" name="blahblah"/>
我遇到了一个类似的问题,试图在Repeater控件中使用RadioButton或HtmlInputRadioButton来定义一个无线电组。 RadioButtonList在我的情况下不起作用,因为HTML需要以RadioButtonList不具备的方式呈现(我将Twitter Bootstrap应用于现有的ASP.NET WebForms站点)。
我的解决方法使用JavaScript w / jQuery删除UniqueID信息,该信息被添加到单选按钮的name属性,然后在提交表单时重新应用原始名称属性。 必须还原原始名称,否则当ASP.NET尝试还原这些控件的状态时,PostBack将发生错误。
这种解决方法在我的场景中运行良好,但要注意任何可能的副作用或边缘情况。 我应用它的页面非常简单。 我没有验证它是否适用于客户端验证。
以下是我使用的JavaScript代码示例:
$(function(){
$('input.custom-radio-name').each(function(i, radio){
var name = radio.name,
groupNameIndex = radio.name.lastIndexOf('$') + 1,
groupName = radio.name.substring(groupNameIndex);
// Preserve the original name, so that it can be restored
$(this).data('aspnetname', name);
radio.name = groupName;
});
$('form').submit(function(){
// Restore the name of the radio buttons,
// otherwise errors will ensue upon PostBack.
$('input.custom-radio-name').each(function(i, control){
var aspnetname = $(this).data('aspnetname');
control.name = aspnetname;
});
});
});
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
HtmlInputRadioButton control;
if (e.Row.RowType = DataControlRowType.DataRow)
{
control = e.FindControl("ControlID") as HtmlInputRadioButton;
}
control.name ="xyz"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.