在检查后面的代码中的条件后,在转发器内添加复选框或单选按钮

时间:2016-02-08 12:34:55

标签: c# asp.net

我想在检查条件后为转发器内的每条记录动态添加radiobutton或复选框。取决于条件将决定控制是什么...... 如果条件为真,那么所有记录都应该是带复选框的单选按钮。我试图将此条件添加到myRpt_ItemDataBound。但我卡住了请帮帮我。

.aspx代码

<asp:Repeater ID="myRpt" runat="server" OnItemCommand="myRpt_ItemCommand" OnItemDataBound="myRpt_ItemDataBound">
    <HeaderTemplate>
        <table>
            <tr class="">
                <td>
                    Name
                </td>
                <td>
                    Address
                </td>
                <td>
                    Age
                </td>
                <td>
                    Year
                </td>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                    ** the place, I want to add the control **
                    // need  to add checkbox or radiobutton after checking the condition in code behind file
            </td>
            <td>
                <%#Eval("Name")%>
            </td>
            <td>
                <%#Eval("Address")%>
            </td>
            <td>
                <%#Eval("Age")%>&nbsp;
            </td>
            <td>
                <%# Eval("Year")%>&nbsp;
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

在代码隐藏文件

之后
public void BindUserData()
{
    myRpt.DataBind();
}

protected void myRpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        //check for condition
        if(*******)
        {
            //should add a radio button for the record
        }
        else
        {
            //shoud add a check box for the record
        }
    }
}

0 个答案:

没有答案