locked
Which method should i use this code formally used on formview using it on datalist RRS feed

  • Question

  • User-2074858223 posted

    see error

     if (string.IsNullOrEmpty(((DataRowView)GetMergedAll.DataItem)["Status"].ToString()))
        {
            Control PLNHid = GetMergedAll.FindControl("PLNHid");
            PLNHid.Visible = false;
        }
    Compiler Error Message: CS1061: 'System.Web.UI.WebControls.DataList' does not contain a definition for 'DataItem' and no extension method 'DataItem' accepting a first argument of type 'System.Web.UI.WebControls.DataList' could be found (are you missing a using directive or an assembly reference?)

    using it on datalist

     protected void GetMergedAll_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (string.IsNullOrEmpty(((DataRowView)GetMergedAll.DataItem)["Status"].ToString()))
            {
                Control PLNHid = GetMergedAll.FindControl("PLNHid");
                PLNHid.Visible = false;
            }

    Wednesday, March 7, 2018 3:00 PM

Answers

  • User-1838255255 posted

    Hi micah2012,

    According to your description and needs, I make a sample with OnItemDataBound event, please check the following sample code: 

    Sample Code: 

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:DataList ID="dlCustomers" runat="server" RepeatColumns="3" CellSpacing="3" RepeatLayout="Table" OnItemDataBound="dlCustomers_ItemDataBound">
                    <ItemTemplate>
                        <table class="table">
                            <tr>
                                <th colspan="2">
                                    <b>
                                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("ContactName") %>'></asp:Label></b>
                                </th>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <%# Eval("City") %>,
                                    <%# Eval("PostalCode") %><br />
                                    <%# Eval("Country")%>
                                </td>
                            </tr>
                            <tr>
                                <td>Phone:
                                </td>
                                <td>
                                    <%# Eval("Phone")%>
                                </td>
                            </tr>
                            <tr>
                                <td>Fax:
                                </td>
                                <td>
                                    <%# Eval("Fax")%>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:DataList>
            </div>
        </form>
    </body>
    
     protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("ContactName", typeof(string));
                dt.Columns.Add("City", typeof(string));
                dt.Columns.Add("PostalCode", typeof(string));
                dt.Columns.Add("Country", typeof(string));
                dt.Columns.Add("Phone", typeof(string));
                dt.Columns.Add("Fax", typeof(string));
                dt.Rows.Add("1", "2", "3", "4", "5", "6");
                dt.Rows.Add("1", "2", "3", "4", "5", "6");
                dt.Rows.Add("1", "2", "3", "4", "5", "6");
                dlCustomers.DataSource = dt;
                dlCustomers.DataBind();
            }
    
            protected void dlCustomers_ItemDataBound(object sender, DataListItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    DataRowView drv = (DataRowView)(e.Item.DataItem);
                    int value = int.Parse(drv.Row[0].ToString());
                    //....
                  
                }
            }

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 9, 2018 2:58 AM