Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何查找位于datalist itemtemplate内的控件_C#_Asp.net_Datalist - Fatal编程技术网

C# 如何查找位于datalist itemtemplate内的控件

C# 如何查找位于datalist itemtemplate内的控件,c#,asp.net,datalist,C#,Asp.net,Datalist,如何在数据列表中找到控件? 即使我使用useFindControl,它也不起作用 protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {

如何在数据列表中找到控件? 即使我使用use
FindControl
,它也不起作用

    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>

问题编号:


MCQ_类型:
答复:



您可能需要这样的东西:

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

这在我这边起作用了

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

首先需要绑定数据列表,然后循环它

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

您正在将ItemDatabound绑定到错误的方法。在标记中,您有:

 OnItemDataBound="DataList1ItemDataBound"
将其更改为:

 OnItemDataBound="DataList1_ItemDataBound"
因为具有访问控制代码的方法是:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)

那是什么=DL_-Question.*Items[n]**。DL_-Question是数据列表控件,Items[n]是要查找的控件的列索引。我正在修改我的答案。请看。你的代码看起来不错。你能把数据列表的标记也贴出来吗?你得到了什么错误?这是标记,我没有得到任何错误,只是它没有显示我想要的@阿夫扎勒赫只是因为它没有显示我想要什么。这意味着您没有在
FindControl
方法中获取控件,或者您的数据列表没有显示正确的数据。OnItemDataBound=“DataList1ItemDataBound”与受保护的事件名称不同void DataList1\u ItemDataBound(对象发送方,DataListItemEventArgs e)执行此操作-