2014-03-12 137 views
0

我遇到了一個奇怪的情況。我無法啓用連續的複選框。使用jquery在gridview中啓用禁用的複選框

目前我在,我想成爲一個查看文檔時啓用一個GridView一個TemplateField複選框..

<asp:HyperLinkField DataNavigateUrlFormatString="/documents/{0}" DataNavigateUrlFields="document_name" 
    Text="View" Target="_blank" > 
    <ItemStyle HorizontalAlign="Center" /> 
</asp:HyperLinkField> 

<asp:TemplateField HeaderText="Complete" SortExpression="complete"> 
    <ItemTemplate> 
     <asp:CheckBox ID="gv5cbComplete" runat="server" Checked='<%# Convert.ToBoolean(Eval("complete")) %>' 
     OnCheckedChanged="GridView5_OnCheckedChanged" AutoPostBack="True" Enabled="False" /> 
    </ItemTemplate> 
</asp:TemplateField> 

後面的代碼中,我通過添加一個onclick事件到HyperLinkField字段一排排倒數。

string cbid = e.Row.Cells[7].ClientID.ToString(); 
     ((HyperLink)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "enableGV5CheckBoxFunc(\"" + cbid + "\")"); 

而且我已經從後面的代碼註冊了一個javascript jquery函數。

// register script 
     StringBuilder cstext = new StringBuilder(); 
     cstext.Append("<script type=\"text/javascript\">" + Environment.NewLine); 
     cstext.Append("function enableGV5CheckBoxFunc(id){" + Environment.NewLine); 
     cstext.Append("window.alert(id);" + Environment.NewLine); 
     //cstext.Append("$(\"#\" + id).removeAttr(\"disabled\");" + Environment.NewLine); 
     cstext.Append("$(\"#\" + id).prop(\"disabled\", false);" + Environment.NewLine); 
     cstext.Append("}</script>"); 
     cs.RegisterClientScriptBlock(cstype, csname1, cstext.ToString(), false); 

現在我知道這是相反的。如果複選框已啓用,並且在jquery中設置.prop(禁用,true),那麼當我單擊超鏈接時,它將禁用複選框。然而,試圖做我想做的事情是行不通的。

是否有某種特定的方式需要我這樣做?

+0

嘗試'.removeAttr('disabled')' – Ted

+0

@Ted已被註釋掉。但是我確實刪除了這個測試,並且它沒有改變。 – Prescient

+0

它正確地提醒身份證?開頭沒有'#'?只需簽入。完全側面的說明,出於好奇,爲什麼不把它放在頁面上,而不是放在代碼後面呢? – Ted

回答

0

非常感謝泰德!他讓我看着正確的方向。

我切換了

string cbid = e.Row.Cells[7].Controls[0].ClientID.ToString(); 

string cbid = e.Row.FindControl("gv5cbComplete").ClientID.ToString(); 

和它的作品。這似乎是因爲.NET做一個時髦的添加

<span class="aspNetDisabled"> 

周圍的複選框,當你禁用複選框它用id食堂。

謝謝!