2012-05-14 64 views
2

我正在使用.NET建立了一個網站,即時通訊試圖輸出我的標記作爲如此...格式化複選框?

<span class="check-input"> 
    <!-- Terms --> 
    <input type="checkbox" id="terms" class="terms checkbox"> 
    <label for="terms">I agree to the terms and conditions</label> 
    <input type="checkbox" id="age" class="age checkbox"> 
    <label for="age">I am over 18 years old</label> 
    </span> 

只有我的開發人員已到位的代碼是...

<!-- Terms --> 
     <asp:CheckBox ID="CB_Agree" 
     Text="I agree to the terms and conditions" runat="server" TabIndex="4" /> 

     <asp:CheckBox ID="CB_Age" 
     Text="I am over 18 years old" runat="server" TabIndex="5" /> 

,這是我的輸出數據作爲...

<span class="check-input"> 
<!-- Terms --> 
<span class="terms checkbox"> 
    <input type="checkbox" tabindex="4" name="ctl00$ContentPlaceHolder1$CB_Agree" id="ctl00_ContentPlaceHolder1_CB_Agree"> 
    <label for="ctl00_ContentPlaceHolder1_CB_Agree">I agree to the terms and conditions</label> 
</span> 
<span class="age checkbox"> 
    <input type="checkbox" tabindex="5" name="ctl00$ContentPlaceHolder1$CB_Age" id="ctl00_ContentPlaceHolder1_CB_Age"> 
    <label for="ctl00_ContentPlaceHolder1_CB_Age">I am over 18 years old</label> 
</span> 
</span> 
+3

不確定你的確切的問題是。 –

+0

請注意,回答你的問題的人似乎也不知道。你有人在回答如何調整課程,以及回答關於如何讓客戶ID匹配的人...... –

+0

你能否澄清這個問題,以便我們可以嘗試和幫助你? –

回答

5

然後你(或你的開發者)需要將CssClass屬性添加到複選框。

例如:

<asp:CheckBox ID="CB_Agree" 
     CssClass="terms checkbox" 
     Text="I agree to the terms and conditions" runat="server" TabIndex="4" /> 

它也具有InputAttributes控制複選框的佈局和LabelAttributes屬性的跨度。

所以你可以例如在代碼隱藏通過以下方式適用不同的背景顏色:

CB_Agree.InputAttributes.CssStyle.Add("background-color", "Red"); 
CB_Agree.LabelAttributes.CssStyle.Add("background-color", "Green"); 
1

您將無法控制名稱輸出的方式,但你絕對可以控制ID是輸出的方式。要做到這一點的方法之一是在您的標記添加ClientIDMode ="Static"到控件這樣:

<asp:CheckBox ID="CB_Agree" ClientIDMode ="Static" 
    Text="I agree to the terms and conditions" runat="server" TabIndex="4" /> 

蒂姆只是讓我意識到,你有風格也是一個問題。對於這一點,你需要添加CssClass="your_class your_other_class"像這樣:

<asp:CheckBox ID="CB_Agree" ClientIDMode ="Static" CssClass="terms checkbox" 
    Text="I agree to the terms and conditions" runat="server" TabIndex="4" /> 
0

Cleaner HTML Markup with ASP.NET 4 Web Forms - Client IDs (VS 2010 and .NET 4.0 Series)

如果您在使用ASP.NET 4.0那麼你可以使用ClientIDMode上控制值爲static,並且ID不會更改。

否則,你可以使用文字來輸出的元件本身或只是它的價值。在你頁面上你必須:

<input type="hidden" id="ppID" value='<asp:Literal ID="ppIDValue" runat="server" />' /> 

要應用CSS類到你的控件使用服務器端控件CssClass財產:

<asp:CheckBox ID="CB_Agree" 
     Text="I agree to the terms and conditions" runat="server" TabIndex="4" 
     CssClass="terms checkbox" /> 

編號:ASP.NET HtmlInputHidden control - stop name from changing?