Search code examples
asp.netdatalist

find control inside datalist in button event


I had data list as label for Question and textarea for Answer ,as datalist render Question on label and the user will add the Answer in text area as the answers inserted in database. I did my code , but when I add my code to find the controls (label,textarea) they returned with null value although I add answers for the Questions .

protected void BT_submit_Click(object sender, ImageClickEventArgs e)
{

    Label QID = (Label)Dl_Question.FindControl("lbl_QID");
    HtmlTextArea QAnswer = (HtmlTextArea)Dl_Question.FindControl("Txt_Answer");
}

DataList code:

<asp:DataList ID="Dl_Question" runat="server" onitemdatabound="Dl_Question_ItemDataBound"                                                                          onitemcommand="Dl_Question_ItemCommand">
   <ItemTemplate>
      <asp:Label ID="lbl_QID" runat="server" Text='<%# Eval("ID") %>' Visible="false">
      </asp:Label><br />
      <asp:Label ID="Lbl_Question" runat="server" Text='<%# Eval("Question") %>'></asp:Label>  <br />
      <textarea id="Txt_Answer" cols="80" rows="5" runat="server"></textarea>
   </ItemTemplate>
</asp:DataList>

Solution

  • I would assume you are looking at higher level than you think. You might need something like this:

    Label QID = Dl_Question.**Items[n]**.FindControl("lbl_QID") as Label;