2011-01-06 2 views
3

의 HtmlInputRadioButton 이름을 설정하십시오. RowDataBound 이벤트에서 gridview는 GridViewRow 안에 HtmlInputRadioButton의 이름을 설정합니다. 문제는 asp.net이 자동으로 이름을 고유하게 만들어서 라디오 버튼을 그룹 해제하는 것입니다.ASP.NET GridView에서 코드 뒤에있는

내가 이것을 사용 중지하는 방법이 있습니까?

편집 - 가 여기에 내가 이름을 설정하고 방법은 다음과 같습니다 "때문에 숫자"106 "에

<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"> 

<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"> 

대신이 HTML을

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 

을 생산하고 107 "을 각각의 라디오 버튼의 이름으로 붙이면 그룹이 해제됩니다.

+0

에서 예를 참조하십시오 "이름"당신은 서버 측 컨트롤 ID 또는 클라이언트 측 입력을 의미합니까 이름? – jrummell

+0

클라이언트 측 입력 이름 – Drahcir

+0

어떻게 독특하게 만들 수 있습니까? 그것은 무엇을 휘젓다? –

답변

0
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" 
} 
1

대신 일반 라디오 버튼을 사용해야한다고 생각합니다. <input type="radio" name="blahblah"/>

+0

변수 값으로 설정 했으므로 코드 뒤에서 이름을 설정해야합니다. – Drahcir

+0

일부 콜렉션 또는 목록을 gridview에 바인딩하는 경우 해당 클래스의 한 속성을 설정하고'

+0

변수가 다른 곳의 데이터 소스가 아닙니다 – Drahcir

5

당신은 내가 Repeater 컨트롤 내에서의 RadioButton 또는 HtmlInputRadioButton를 사용하여 라디오 그룹을 정의하려고 시도하는 유사한 문제로 도망을 UniqueID

private class MyHtmlInputRadioButton : HtmlInputRadioButton 
    { 
     public override string UniqueID 
     { 
      get 
      { 
       return string.IsNullOrEmpty(Name) ? base.UniqueID : Name; 
      } 
     } 
    } 
+0

왜 이것을 답변으로 표시하지 않습니까?간단하고 우아하며 작동합니다. 고맙습니다. @ igor-z – getsetcode

0

의 게터를 재정 의하여 라디오 버튼의 이름을 대체 할 수 있습니다 . RadioButtonList는 (기존의 ASP.NET WebForms 사이트에 Twitter Bootstrap을 적용하고있었습니다.) HTML을 렌더링 할 필요가 없기 때문에 RadioButtonList가 제 경우에는 작동하지 않습니다.

내 해결 방법은 JavaScript가있는 w/jQuery를 사용하여 UniqueID 정보를 제거합니다.이 정보는 라디오 단추의 name 특성에 고정되어 있으며 양식을 제출할 때 원래 이름 특성을 다시 적용합니다. 원래 이름을 복원해야합니다. 그렇지 않으면 ASP.NET이 해당 컨트롤의 상태를 복원하려고 시도 할 때 PostBack에서 오류가 발생합니다.

이 해결 방법은 제 시나리오에서는 문제가 없지만 가능한 부작용이나 엣지 경우를주의하십시오. 이것을 적용한 페이지는 매우 간단했습니다. 클라이언트 쪽 유효성 검사와 함께 작동하는지 확인하지 않았습니다. 여기

내가 사용하는 자바 스크립트 코드의 예입니다 : 당신이 말할 때

$(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; 
     }); 
    }); 

}); 

http://jsfiddle.net/yumN8/

관련 문제