Check TreeView has any Node selected or not? - c#

I have a treeview control on my aspx page and a button.on the basis of treeview node selection I want to show and hide the button.but when initially page loads there is no any seleceted node that's in if condition it is generating exception.Can anyone tell me that how can i find that is there any node selected or not ?
//Here I want to check is there any selected node "ApplicationTree.Nodes.Count>0"
<%if(ApplicationTree.Nodes.Count>0)
{%>
<%
//Here it is generating exception because initially no any node is selected
IsReviewPending = view_access.IsWaitingForViewAccess(ApplicationTree.SelectedNode.Value, Session["empCode"].ToString());
if (IsReviewPending)
{
CanReviewAccess = true;
}
else
{
CanReviewAccess = false;
}
%>
<%if(CanReviewAccess)
{%>
<asp:Button ID="btn_Review_Access" OnClick="btn_Review_Access_Click" runat="server" BackColor="#C6304A" ForeColor="White" Text="Confirm Access Review" Width="200px" CssClass="center3" />
<%} %>
<%} %>

I found the solution :)
//Here is my Code
<%
bool HasSelectedNode = false;
//i iterated through the overall nodes of the tree and checked any of the node is selected or not
for (int i = 0; i < ApplicationTree.Nodes.Count;i++ )
{
if(ApplicationTree.Nodes[i].Selected==true)
{
HasSelectedNode = true;
}
}
%>
<%if(HasSelectedNode)
{%>
<%
IsReviewPending = view_access.IsWaitingForViewAccess(ApplicationTree.SelectedNode.Value, Session["empCode"].ToString());
// IsReviewPending = true;
if (IsReviewPending)
{
CanReviewAccess = true;
}
else
{
CanReviewAccess = false;
}
%>
<%if(CanReviewAccess)
{%>
<asp:Button ID="btn_Review_Access" OnClick="btn_Review_Access_Click" runat="server" BackColor="#C6304A" ForeColor="White" Text="Confirm Access Review" Width="200px" CssClass="center3" />
<%} %>
<%} %>

Related

Session Variable with if/else statement in Views?

I want to include an ascx file with navigator bar. There are two versions, one shows up when user is not logged in and one is for logged user.
This is what I'm lookin for
<div class="navigator">
<%
if(Session["loggedin"] == null) {
Include this file <uc1:nav runat="server" ID="nav" />
} else {
Include this file <uc1:nav runat="server" ID="nav2" />
}
%>
</div>
This is how I can link one of the nav... but I have no idea how to make this path into if/else statment
Tried to do some things inside of <% %> but.... without any success.
Could anyone help me?
Appeareantly I figured it out.
Here is what I did.
<% if(Session["loggedin"] == null) { %>
<uc1:nav runat="server" ID="nav" />
<% } else { %>
<uc1:nav runat="server" ID="nav2" />
<% } %>

How to show/hide a nested list using JavaScript in ASP.NET?

Edit: Found the solution, and posted the answer below.
In my ASP.NET c# project, I have a ListView (ParentList) bound to a DataSource. Within the ParentList, inside the ItemTemplate, I have another Repeater (ChildList) bound to an attribute of each ListViewDataItem.
<asp:ListView ID="ParentList" runat="server" DataSourceID="objectDataSourceID" DataKeyNames="ID">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Attribute1") %>' />
</td>
<td valign="top">
<asp:Repeater ID="ChildList" runat="server" DataSource='<%# Eval("Attribute2ReturnsAnotherList") %>'>
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<%# DataBinder.Eval(Container.DataItem, "childAttribute") %>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
...
</LayoutTemplate>
</asp:ListView>
The code above works just fine, everything renders great. Now I want to add a link that will show/hide the ChildList. Something like the below:
<td valign="top">
<a href="javascript:ToggleListVisibility()" >Show/Hide</a>
<asp:Repeater ID="ChildList" runat="server" DataSource='<%# Eval("Attribute2ReturnsAnotherList") %>'>
</asp:Repeater>
</td>
How can I achieve this? I can't just use getElementById as I normally would, as the ul lists are within a Repeater nested inside the ListView. I tried obtaining the parentNode, then accessing the children and toggling the visibility of the ul element within:
function ToggleListVisibility(source) {
var childrenlist = source.parentNode.children;
for (var i = 0; i < childrenlist.length; i++) {
if (childrenlist[i].tagName == 'ul') {
if (childrenlist.style.display == "none") {
childrenlist.style.display = "block";
} else {
childrenlist.style.display = "none";
}
}
}
}
<a href="javascript:ToggleListVisibility(this)" >Show/Hide</a>
but that didn't work. IE's 'error on page' gave me this error:
The parentNode is null or not an object.
I also tried setting the a runat="server" attribute to my ul element, then using <%# ulID.ClientID %> to pass the ul id to the Js function, but visual studio complained:
Server elements cannot span templates.
Finally, I tried just passing the ul object into the Js function, like this:
function ToggleListVisibility(src) {
if (src.style.display == "none") {
src.style.display = "block";
} else {
src.style.display = "none";
}
}
<a href="javascript:ToggleListVisibility(ulID)" >Show/Hide</a>
...
<ul id="ulID">
which works, but it toggles the visibility for the ChildList in all rows within my ParentList. I want it to only toggle the visibility for the ChildList in its own row.
I'm at a loss of what to do. JavaScript is not my forte, and I would appreciate if someone can provide some pointers. Thanks in advance.
Ok hopefully this will get you going - it worked for me in hiding a list. My source HTML looks like this:
<table>
<tbody>
...
<tr>
<td>
Hide
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</td>
</tr>
...
</tbody>
</table>
<script type="text/javascript">
function toggleListVisibility(src) {
var childrenList = src.nextSibling.nextSibling;
childrenList.style.display = "none";
}
</script>
Note I had to use two "nextSibling"'s due to a text node that is created right after the "hide" anchor. Depending on how you structure your HTML, that bit will be different.
I found the painfully simple answer that makes me feel like a doofus. All I needed to do is wrap my Repeater in a <div> element, then show/hide the entire thing.
<a href="javascript:ToggleListVisibility('<%# Container.FindControl("divWrapper").ClientID %>')" >Show/Hide</a>
<div id="divWrapper" runat="server">
<asp:Repeater ID="ChildList" runat="server">
</asp:Repeater>
</div>
function ToggleListVisibility(id) {
var wrapper = document.getElementById(id);
if (wrapper.style.display == "none") {
wrapper.style.display = "block";
} else {
wrapper.style.display = "none";
}
}
Hooray for overthinking!

Unable to render html in ascx control page

Here is my ascx control page code
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Menu.ascx.cs" Inherits="CrossQueue.Web.Menu" %>
<asp:Label ID="lblMenu" runat="server"></asp:Label>
Here is my c# code
private void CreateMenu()
{
StringBuilder menuHtml = new StringBuilder();
int profileId = 0;
MenuBL menuManager = new MenuBL();
DataTable dtMenu = null;
if (Session["USR_ID"] != null)
{
profileId = Convert.ToInt32(Session["USR_PROFILE"]);
dtMenu = menuManager.GetAllMenuItemsForProfile(profileId);
if (dtMenu != null && dtMenu.Rows.Count > 0)
{
menuHtml.Append("<table id='tblMenu' cellpadding='0' cellspacing='0' width='939' border='0' align='center'>");
menuHtml.Append("<tr>");
menuHtml.Append("<td align='left'>");
menuHtml.Append(Convert.ToString(Session["USR_USERNAME"]));
menuHtml.Append("</td>");
menuHtml.Append("<td width='739' valign='middle' align='right' style='height: 30px;'>");
foreach (DataRow dr in dtMenu.Rows)
{
if (dr["MenuName"].ToString() == "Profile")
{
menuHtml.Append("<img src='/images/home_icon.jpg' width='25' height='25' align='middle' /><a href='AllProfile.aspx>Profile</a> ");
}
else if (dr["MenuName"].ToString() == "User")
{
menuHtml.Append("<img src='/images/home_icon.jpg' width='25' height='25' align='middle' /><a href='AllUsers.aspx>User</a> ");
}
}
menuHtml.Append("</td>");
menuHtml.Append("</tr>");
menuHtml.Append("</table>");
}
lblMenu.Text = menuHtml.ToString();
}
}
When i load the page i only see a html code printed as text and not rendering.Can any one point out what may be wrong
You could use a literal instead of a label.
See this
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.literal.aspx
You can make a div server accessible by assigning ID and setting runat="server" instead of label and set its InnerHTML = menuHtml.ToString();
<div id="div1" runat="server" ></div>
div1.InnerHTML = menuHtml.ToString();
change the Label
<asp:Label ID="lblMenu" runat="server"></asp:Label>
to Literal as
<asp:Literal ID="lblMenu" runat="server" EnableViewState="false"></asp:Literal>

Access the Repeater.Item.Count value within a codebehind method

I have a repeater on my page:
<asp:Repeater id="attachmentsRepeater" runat="server">
<HeaderTemplate>
<%
if (attachmentsRepeater.Items.Count > 0) {
if (attachmentsRepeater.Items.Count == 1) {
Response.Write("<h3>Attachment</h3>");
Response.Write("<p>");
} else {
Response.Write("<h3>Attachments</h3>");
Response.Write("<ul>");
}
}
%>
</HeaderTemplate>
<ItemTemplate>
<%# OutputAttachment(Container)%>
</ItemTemplate>
<FooterTemplate>
<%
if (attachmentsRepeater.Items.Count > 0) {
if (attachmentsRepeater.Items.Count == 1) {
Response.Write("</p>");
} else {
Response.Write("</ul>");
}
}
%>
</FooterTemplate>
</asp:Repeater>
The original ItemTemplate code looked like this:
<ItemTemplate>
<%
if (attachmentsRepeater.Items.Count > 0) {
if (attachmentsRepeater.Items.Count > 1) {
Response.Write("<li>");
}
%>
<a href="<%# DataBinder.Eval(Container.DataItem, "location") %>">
<%# DataBinder.Eval(Container.DataItem, "name") %>
</a>
<%
if (attachmentsRepeater.Items.Count > 1) {
Response.Write("<li>");
}
}
%>
</ItemTemplate>
In the codebehind, I would like to access the number of Items in the Repeater (line 4):
public string OutputAttachment(RepeaterItem Container) {
string returnValue = "";
Repeater ContainerParent = (Repeater)Container.Parent;
if (attachmentsRepeater.Items.Count > 0) {
if (attachmentsRepeater.Items.Count > 1) {
returnValue += "<li>";
}
returnValue += "<a href=\"" + DataBinder.Eval(Container.DataItem, "location");
if (DataBinder.Eval(Container.DataItem, "location").ToString().EndsWith("/")) {
returnValue += DataBinder.Eval(Container.DataItem, "name");
}
returnValue += ">" + DataBinder.Eval(Container.DataItem, "name") + "</a>";
if (attachmentsRepeater.Items.Count > 1) {
returnValue += "</li>";
}
}
return returnValue;
}
The code that is output is
<h3>Attachment</h3>
<p> </p>
From this output I know that Item.Count == 1 since there is output, the H3 is singular and there is a P tag. If Item.Count > 1, H3 would be plural and there would be a UL tag.
Is this codebehind method being run before the data is bound? Any workarounds for this? Thanks for your help.
This was working for me before, but I had to change it to fulfill a new requirement, which is when it stopped working.
The best place to do your data binding is in the code behind in the page_load event or some other event that is fired when the page is first created.
That way you can control when the data is bound - and you can call the OuputAttachment method after you have bound your data - and you can be sure that the data actually exists.

custom paging in gridview

I am looking for paging(Top,Bottom) functionality for gridview(or any databind controls)
i am using .net framework 2.0.
i would like to have pointers
thank you
solution:
i used jQuery plugin jquery.tablePagination.0.1.js for my solution
You can asp:repeater for this this can easily handle by Repeater control.
This aspx page code
<asp:Repeater ID="rptImages" runat="server" onitemcommand="rptImages_ItemCommand">
<ItemTemplate >
<div class="image"> <a ><asp:Image ID="Image1" runat="server" imageUrl=' <%# Eval("ImageUrl") %>' /></a> </div>
</ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="rptPages" Runat="server"
onitemcommand="rptPages_ItemCommand">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" border="0">
<tr class="text">
<td><b style="color:White;">Page:</b> </td>
<td>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnPage" ForeColor="White"
CommandName="Page"
CommandArgument="<%#
Container.DataItem %>"
CssClass="text"
Runat="server"><%# Container.DataItem %>
</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
this code behind page code
public void LoadData()
{
try
{
PagedDataSource pgitems = new PagedDataSource();
DataView dv = new DataView(dtImage);
pgitems.DataSource = dv;
pgitems.AllowPaging = true;
pgitems.PageSize = 8;
pgitems.CurrentPageIndex = PageNumber;
if (pgitems.PageCount > 1)
{
rptPages.Visible = true;
ArrayList pages = new ArrayList();
for (int i = 0; i < pgitems.PageCount; i++)
pages.Add((i + 1).ToString());
rptPages.DataSource = pages;
rptPages.DataBind();
}
else
rptPages.Visible = false;
rptImages.DataSource = pgitems;
rptImages.DataBind();
}
catch { }
}
public int PageNumber()
{
get
{
if (ViewState["PageNumber"] != null)
return Convert.ToInt32(ViewState["PageNumber"]);
else
return 0;
}
set
{
ViewState["PageNumber"] = value;
}
}
public void itemGet()
{
try
{
var Images = dataContext.sp_getCards(categoryId);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = Images;
pds.AllowCustomPaging = true;
pds.AllowPaging = true;
pds.PageSize = 8;
pds.CurrentPageIndex = CurrentPage;
lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of "
+ pds.PageCount.ToString();
// Disable Prev or Next buttons if necessary
//cmdPrev.Enabled = !pds.IsFirstPage;
//cmdNext.Enabled = !pds.IsLastPage;
rptImages.DataSource = pds;
rptImages.DataBind();
}
catch { }
}
protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
{
try
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
LoadData();
}
catch { }
}
This will be hardly achievable in GridView because it uses rigid table structure with only single pager template. You need two - one on the top and one on the bottom. For such control you need Repeater (.NET 2.0) or ListView (.NET 3.5). When you have buttons placed you can handle their click or command events and rebind your grid to newly selected set of data. In repeater you will probably have to store current page and number of items per page somewhere (ViewState). If you want jQuery based paging (client side) with partial rendering you need to handle client side onclick and add AJAX call to get new page based on pushed button.
i used jQuery plugin jquery.tablePagination.0.1.js for my solution
refer this link
As others have said, I don't think this is a good job for GridView. I'd take a look at ListView or Repeater as they are better suited for something like this and quite easy to add custom pagination to.

Categories

Resources