2014年7月13日 星期日

[RESOLVED] Not able to Hide/remove a ChildNode from a Repeater bound to SitemapDatasource


Hi All,


Please help me to hide/remove a child node. I am able to hide a nodeby setting visible property to false but not able to hide a child node. My code


.ASPX CODE





  • <%# Eval("Title") %>






    • <%# Eval("Title") %>










  • Code Behind Code on Repeater OnItemDataBound="menu_ItemDataBound":


    SqlDataSource SqlDataSource1 = new SqlDataSource();
    SqlDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings["IDMServicesDatabaseConnectionString"].ConnectionString;

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    SiteMapNode thisMapNode = (SiteMapNode)e.Item.DataItem;
    thisMapNode.ReadOnly = false;
    if (thisMapNode.Title == "Admin")
    {
    if (Page.User.IsInRole("enterprisenet.org\\gbs idm Shift leads"))
    {
    e.Item.Visible = true;
    }
    else
    {
    e.Item.Visible = false;
    }
    }
    else
    {
    SiteMapNodeCollection siteMapCollection = thisMapNode.ChildNodes;
    foreach (SiteMapNode node in siteMapCollection)
    {
    string requestType = node.Title;
    SqlDataSource1.SelectCommand = "SELECT [Visible] FROM [IDMServicesList] where [RequestType]='" + requestType + "'";
    //SqlDataSource1.SelectParameters.Add("RequestType", requestType);
    DataView dv = (System.Data.DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    if (dv.Count > 0)
    {
    DataRowView drv = dv[0];
    string Visible = drv["Visible"].ToString();
    if (Visible == "Hide")
    {
    node.ChildNodes.Remove(node); }
    }
    }

    }


    }

    Not able to either remove or hide(by setting the visible property) the child node. With the above code to remove node.ChildNodes.Remove(node) getting Collection is read only not supported exception.


    Please help



    Any help would be appreciated...



    i think following line is problematic,


    node.ChildNodes.Remove(node);

    you are trying do delete node from node object, self-delete is not possible :)


    instead try this


      node.ChildNodes.RemoveAll();     OR   node.ChildNodes.Clear();    

    Thanks.








    ijaz_asp



    instead try this


     node.ChildNodes.RemoveAll(); OR node.ChildNodes.Clear(); 




    same error "Collection is Read only" for node.ChildNodes.Clear();  Method Removeall(); does not exist at all



    hmm, if this is the case , try to hide the item instead of deleting it. like if the data-bound item does not fullfill your display/visible logic then get it from template object and set its visibility to none or simply set visible=false.


    thanks



    I am able to set the Visible property (e.Item.Visible =
    false;)
    for the Parent node in ItemDataBound(object
    sender,
    RepeaterItemEventArgs e) but not able to hide the
    hyperlink which is dynamically created using repeater control.


    Please help me with an example



    it may be something like this,


    var hyperlink = e.Item.findControl("ID of HyperLink") as HyperLink;

    if(hyperlink !=null)
    {
    hyperlink.visible=false; // if it does not meet your criteria .
    }

    thanks





    These hyperlinks are dynamically generated in a repeater control. Not sure of the ID.



    Hi,


    The id is you set in makeup code



    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    SiteMapNode thisMapNode = (SiteMapNode)e.Item.DataItem;
    thisMapNode.ReadOnly = false;
    if (thisMapNode.Title == "Admin")
    {
    if (Page.User.IsInRole("enterprisenet.org\\gbs idm Shift leads"))
    {
    e.Item.Visible = true;
    }
    else
    {
    e.Item.Visible = false;
    }
    }
    else
    {
    SiteMapNodeCollection siteMapCollection = thisMapNode.ChildNodes;
    foreach (SiteMapNode node in siteMapCollection)
    {
    string requestType = node.Title;
    SqlDataSource1.SelectCommand = "SELECT [Visible] FROM [IDMServicesList] where [RequestType]='" + requestType + "'";
    //SqlDataSource1.SelectParameters.Add("RequestType", requestType);
    DataView dv = (System.Data.DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    if (dv.Count > 0)
    {
    DataRowView drv = dv[0];
    string Visible = drv["Visible"].ToString();
    if (Visible == "Hide")
    {
    Repeater thisRepeater = (Repeater)e.Item.FindControl("Repeater1"); HyperLink thisLink = (HyperLink)thisRepeater.FindControl("HyperLink2"); thisLink.Visible = false; }
    }
    }
    }
    }

    I have implemented your code but on the runtime thisLink is null. On thisLink.Visisble, I get object reference not set to instance of an object.






  • <%# Eval("Title") %>






    • <%# Eval("Title") %>









  • On the Repeater1 (Inside the menu Repeater) databound event, I am able to find the id of Hyperlink


    HyperLink thisLink = (HyperLink)e.Item.FindControl("HyperLink2");
    if (thisLink != null)
    {
    thisLink.Visible = false;
    }

    Not sure y your code did not work



    Hi,


    Try it like this


     NavigateUrl='<%# Eval("Url") %>'><%# Eval("Title") %>

    And in code behind define the method isVisible()


    public bool isVisible() 
    {
    if (something ...)
    return true;

    else
    return false;
    }

    And refer the thread


    http://stackoverflow.com/questions/10199698/set-visible-of-a-label-from-code-behind


    http://forums.asp.net/t/1409887.aspx/1


    Hope it can help you




    沒有留言:

    張貼留言