Asked by:
Error 'System.Web.UI.WebControls.DataList' does not contain a definition for 'Row'

Question
-
User-2074858223 posted
I have this code i want to use to hide code from unauthenticated users that is if User A login the button Edit will be visible. The issue with the code is
the code was formally used on FormView but am trying to use it on DataList and its showing this erro, any idea on how to fix this error
if (GetShowEdit.Row.RowType == DataControlRowType.DataRow) { string usernameprof = (GetShowEdit.Row.FindControl("lblusernameprof") as Label).Text; if (usernameprof == Session["userName"].ToString()) { (GetShowEdit.Row.FindControl("btnnone") as LinkButton).Visible = true; } else { (GetShowEdit.Row.FindControl("btnmessage") as LinkButton).Visible = true; } } }
error
Error 65 'System.Web.UI.WebControls.DataList' does not contain a definition for 'Row' and no extension method 'Row' accepting a first argument of type 'System.Web.UI.WebControls.DataList' could be found (are you missing a using directive or an assembly reference?)
Friday, January 26, 2018 9:39 AM
All replies
-
User632428103 posted
Hello,
did you try this in the method :
protected void DataList_ItemDataBound(object sender, DataListItemEventArgs e)
i don't know really well data list but i think you need to do this in the item data bound => means the data is loaded.
Friday, January 26, 2018 11:19 AM -
User1400794712 posted
Hi micah2012,
If you want to get each row of DataList, we use itmes instead of Row. DataList doesn't contain a definition for 'Row'. I make a demo, you can refer to it:
Aspx:
<body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" CellSpacing="3"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("LastName") %>'></asp:Label> <asp:LinkButton ID="btnnone" runat="server" Visible="false">btnnone</asp:LinkButton> <asp:LinkButton ID="btnmessage" runat="server" Visible="false">btnmessage</asp:LinkButton> </ItemTemplate> </asp:DataList> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> </div> </form> </body>
Aspx.cs:
protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("LastName")); dt.Rows.Add("Andy"); dt.Rows.Add("Amy"); dt.Rows.Add("Arron"); DataList1.DataSource = dt; DataList1.DataBind(); Label2.Text=DataList1.Items.Count.ToString(); Session["userName"] = "Amy"; for (int i= 0;i< DataList1.Items.Count; i++) { string usernameprof = (DataList1.Items[i].FindControl("Label1") as Label).Text; if (usernameprof == Session["userName"].ToString()) { (DataList1.Items[i].FindControl("btnnone") as LinkButton).Visible = true; } else { (DataList1.Items[i].FindControl("btnmessage") as LinkButton).Visible = true; } } }
How it works:
Best Regards,
Daisy
Monday, January 29, 2018 7:31 AM -
User-2074858223 posted
see how i tried it, still both button still showing, the idea is if am login hid the button edit
private void Populatebooks() { // string userId = Session["userName"].ToString(); // string userId = Session["userName"].ToString(); // string str = ConfigurationManager.ConnectionStrings["DB"].ConnectionString; string Populatebooks = "GetUserPOSTS"; using (SqlConnection con = new SqlConnection(constring4)) { con.Open(); using (SqlCommand cmd = new SqlCommand(Populatebooks, con)) { cmd.CommandType = CommandType.StoredProcedure; // cmd.Parameters.AddWithValue("@Name", Request.QueryString["Id"].ToString()); cmd.Parameters.AddWithValue("@UserName", Session["userName"]); // cmd.Parameters.AddWithValue("@Id", Id); //lblpost.Text = TextBixcomment.Text.Replace(Environment.NewLine, "<BR />"); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); ViewState["DataTable"] = dt; // dt.Columns.Add(new DataColumn("UserName")); GetMergedAll.DataSource = dt; GetMergedAll.DataBind(); // Session["userName"]); Label2.Text = GetMergedAll.Items.Count.ToString(); for (int i = 0; i < GetMergedAll.Items.Count; i++) { string usernameprof = (GetMergedAll.Items[i].FindControl("lblUserName") as Label).Text; if (usernameprof == Session["userName"].ToString()) { (GetMergedAll.Items[i].FindControl("btnedit") as LinkButton).Visible = true; } else { (GetMergedAll.Items[i].FindControl("btnedit") as LinkButton).Visible = true; } } } }
Monday, January 29, 2018 11:15 AM -
User1400794712 posted
see how i tried it, still both button still showing, the idea is if am login hid the button editIt seems Populatebooks method isn't called.
Please add breakpoint to Populatebooks to debug the project. Check if this method is called and works correctly.
Best Regards,
Daisy
Tuesday, January 30, 2018 11:00 AM