2011-01-06 20 views
3

在GridView的RowDataBound事件我設置的HtmlInputRadioButtonGridViewRow內的名稱。問題是,asp.net自動使名稱獨特,因此取消了單選按鈕的組合。ASP.NET設置GridView中的HtmlInputRadioButton名稱後面的代碼

有沒有辦法讓我禁用?

編輯 - 下面是我如何設置名稱:中

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

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 

產生這個網站

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

而是由於數字 「106」 和「 107「,以其拆分它們的相應單選按鈕的名義。

+0

一個例子當你說「名字」你的意思是服務器端控件的ID或客戶端輸入名稱? – jrummell 2011-01-06 16:28:29

+0

客戶端輸入名稱 – Drahcir 2011-01-06 16:31:19

+0

它如何使它獨一無二?它流出了什麼? – 2011-01-06 16:34:50

回答

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

5

您可以通過重寫的UniqueID

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

我碰到了類似的問題,試圖在Repeater控件中定義使用一個單選按鈕或HtmlInputRadioButton單選按鈕組,吸氣覆蓋單選按鈕的名稱。 RadioButtonList在我的情況下不起作用,因爲HTML需要以RadioButtonList無法使用的方式呈現(我將Twitter Bootstrap應用於現有的ASP.NET WebForms站點)。使用JavaScript的瓦特/ jQuery來刪除的UniqueID信息

我的解決辦法,那就是上漲到單選按鈕的名稱屬性,然後重新原來的名稱屬性的表單提交時。原始名稱必須恢復,否則當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; 
     }); 
    }); 

}); 

看到http://jsfiddle.net/yumN8/

相關問題