2013-10-30 29 views
1

我如何在我的datalist中找到控件? 即使我使用使用FindControl,它不起作用。如何找到位於datalist項目模板內的控件

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) 
    { 
     if (e.Item.ItemType == ListItemType.Item || 
     e.Item.ItemType == ListItemType.AlternatingItem) 
     { 
      DataRowView drv = (DataRowView)(e.Item.DataItem); 

       Label ResponseCatIDLabel = (Label)e.Item.FindControl("ResponseCatIDLabel"); 
       string res = ResponseCatIDLabel.Text; 

       TextBox TextBox1 = (TextBox)e.Item.FindControl("TextBox1"); 
       string text = TextBox1.Text; 

       Label MCQ_TYPELabel = (Label)e.Item.FindControl("MCQ_TYPELabel"); 
       string mcq = MCQ_TYPELabel.Text; 

       RadioButtonList ExcellentRb = (RadioButtonList)e.Item.FindControl("ExcellentRb"); 
       string excellent = ExcellentRb.Text; 

       RadioButtonList YesNoRb = (RadioButtonList)e.Item.FindControl("YesNoRb"); 
       string yesno = YesNoRb.Text; 

       Label RespnseCatIDLabel = (Label)e.Item.FindControl("RespnseCatIDLabel"); 
       string sqn = RespnseCatIDLabel.Text;    

       if (RespnseCatIDLabel.Text == "") 
       { 
        ExcellentRb.Visible = false; 
        YesNoRb.Visible = false; 
       } 

這是我設計代碼的代碼,有什麼不對嗎?

<asp:DataList ID="DataList1" runat="server" BackColor="White" 
      BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
      DataSourceID="SqlDataSource1" Font-Bold="False" 
      Font-Italic="False" Font-Names="Britannic Bold" Font-Overline="False" 
      Font-Strikeout="False" Font-Underline="False" GridLines="Vertical" 
      OnItemDataBound="DataList1ItemDataBound"> 
      <AlternatingItemStyle BackColor="Gainsboro" /> 
      <FooterStyle BackColor="#CCCCCC" ForeColor="Black" /> 
      <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" /> 
      <ItemStyle BackColor="#EEEEEE" ForeColor="Black" /> 
      <ItemTemplate> 

       QuestionNo:&nbsp;<asp:Label ID="SurveyQuestionNoLabel" runat="server" 
        Text='<%# Eval("SurveyQuestionNo") %>' /> 
       <br /> 
       &nbsp;<asp:Label ID="PoolQuestionLabel" runat="server" 
        Text='<%# Eval("PoolQuestion") %>' /> 
       <br /> 
       MCQ_TYPE: 
       <asp:Label ID="MCQ_TYPELabel" runat="server" Text='<%# Eval("MCQ_TYPE") %>' /> 
       <br /> 
        ResponseCatID: 
       <asp:Label ID="RespnseCatIDLabel" runat="server" 
        Text='<%# Eval("ResponseCatID") %>' /><br /> 
       <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox> 
       <br /> 

       <asp:RadioButtonList ID="YesNoRb" runat="server" DataSourceID="YesNoDB" 
        DataTextField="Response" DataValueField="ResponseValue"> 
       </asp:RadioButtonList> 
       <asp:SqlDataSource ID="YesNoDB" runat="server" 
        ConnectionString="<%$ ConnectionStrings:SurveyFdDBConnString %>" 
        SelectCommand="SELECT Response, ResponseValue FROM MCQ_Response_Options WHERE (Response = 'Yes') OR (Response = 'No') ORDER BY ResponseValue DESC"> 
       </asp:SqlDataSource> 
       <asp:RadioButtonList ID="ExcellentRb" runat="server" DataSourceID="ExcellentDB" 
        DataTextField="Response" DataValueField="ResponseValue"> 
       </asp:RadioButtonList> 
       <asp:SqlDataSource ID="ExcellentDB" runat="server" 
        ConnectionString="<%$ ConnectionStrings:SurveyFdDBConnString %>" 
        SelectCommand="SELECT ResponseCatID, Response, ResponseValue FROM MCQ_Response_Options WHERE (Response = 'Excellent') AND (ResponseValue = 5) AND (ResponseCatID = 'R1') OR (Response = 'Good') AND (ResponseValue = 4) AND (ResponseCatID = 'R1') OR (Response = 'Satisfactory') AND (ResponseValue = 3) AND (ResponseCatID = 'R1') OR (Response = 'Marginal') AND (ResponseValue = 2) AND (ResponseCatID = 'R1') OR (Response = 'Poor') AND (ResponseValue = 1) AND (ResponseCatID = 'R1') ORDER BY ResponseValue DESC"> 
       </asp:SqlDataSource> 
       <br /> 
      </ItemTemplate> 
      <SelectedItemStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" /> 
     </asp:DataList> 
+1

您的代碼看起來不錯。你也可以發佈datalist的標記嗎? – afzalulh

+0

你得到的錯誤是什麼? –

+0

這是標記,我沒有得到任何錯誤,只是它不顯示我想要的。 @afzalulh – BinQuan

回答

0

你可能需要的東西是這樣的:

Label MCQ_TYPELabel1 = DataList1.Items[1].FindControl("MCQ_TYPELabel") as Label; 
+0

那是什麼? = DL_Question。** Items [n] **。 – BinQuan

+0

DL_Question是DataList控件&Items [n]是您想要查找的控件的列索引。我正在改變我的答案。請參見。 –

0

這是我的最終

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) 
{ 
    Label Label1 = e.Item.FindControl("Label1") as Label; 
} 
+0

不適合我。 〜 – BinQuan

+0

你可以發佈你的標記嗎?因爲你的代碼似乎沒問題。 – Dusht

0

首先工作,你需要將數據綁定列表,然後循環它

foreach (DataListItem li in DataList1.Items) 
{ 
    TextBox txt= (TextBox) li.FindControl("TextBox1"); 
} 
+0

通過這個-http://www.codeproject.com/Articles/23559/Working-with-the-DataList-Control –

2

您正在綁定ItemDatabou nd到錯誤方法。在標記你:

OnItemDataBound="DataList1ItemDataBound" 

將其更改爲:

OnItemDataBound="DataList1_ItemDataBound" 

由於具有代碼訪問控制方法是:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) 
相關問題