2017-03-23 6 views
0

私は何時間もこれを理解しようとしています。とても悲しい!私ascxの構造はDetailsViewからCheckBoxFieldを取得する方法

<asp:DetailsView ...> 
    <Fields> 
     . 
     . 
     <asp:CheckBoxField DataField="ThingEnabled" HeaderText="Thing Enabled"/> 
     . 
     . 
     . 
    </Fields> 
</asp:DetailsView> 

のようなもので、私が欲しい要素がThingEnabled一つです。

セットアップ:CheckBoxField sがID性質を持っていないので、私はFindControlを使用できないことを

DetailsView dv = (DetailsView)sender; 
CheckBoxField cbf = ???? 

は注意してください。それでもFindControlを使用したい場合は

// Page_Load is just an example event here, change to any event you need 
protected void Page_Load(object sender, EventArgs e) 
{ 
    DetailsView dv = sender as DetailsView; 

    // the checkbox uses checked state as its value to be passed 
    // n = row/cell/control indexes where CheckBoxField has bound into, starting from 0 
    // e.g. dv.Rows[0].Cells[0].Controls[0] as CheckBox 
    bool checkboxvalue = (dv.Rows[n].Cells[n].Controls[n] as CheckBox).Checked; 
} 

DetailsViewが、それは、したがって、あなたがこのように行、セルとコントロールの位置を使用してCheckBoxField値を得ることができた代わりに、各行のコントロールIDのその上で細胞を使用していることを

答えて

0

注意、ItemTemplateラッパー要素を使用し、その上にCheckBoxコントロールを作成します(あなたはデータバインディングのためTemplateFieldの上にDataField="ThingEnabled"BoundFieldを使用する必要があることに注意してください):

<asp:DetailsView runat="server" ...> 
    <Fields> 
     ... 
     <asp:TemplateField HeaderText="Thing Enabled"> 
      <ItemTemplate> 
       <asp:CheckBox ID="ThingEnabled" runat="server" Checked="<%# Bind("ThingEnabled") %>"> 
       </asp:CheckBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Fields> 
</asp:DetailsView> 

次にあなたがFindControlを使用して、そのチェックボックスコントロールにアクセスすることができます。

DetailsView dv = sender as DetailsView; 

// n = row/cell indexes, starting from 0 
bool checkboxvalue = (dv.Rows[n].Cells[n].FindControl("ThingEnabled") as CheckBox).Checked; 

参照/類似した問題:= '<%#チェック

How to get value from DetailsView Control in ASP.NET?

Get the value of a BoundField from a DetailsView

Accessing DetailsView check box value

+0

を 'バインド( "ThingEnabled")%> '' - エラーが表示されますアトリビュートを単一引用符で囲まないと、タグの形式が正しくありません。 – snumpy

関連する問題