unable to access a control in Master file from the code behind - c#

basically I have a button in my navbar which is defined in master file. And I want to call a c# method when that button**(bt)** is clicked.
But I can not access the control in code-behind. I also need to control visibility, so I believe it has to stay in one of those Views.
<asp:GridView runat="server" ID="lw3">
<EmptyDataTemplate>
<ul class="nav navbar-nav navbar-right" style="color:#ffffff !important">
<li><a runat="server" href=""></a></li>
<li><a runat="server" href="" title=""></a></li>
<li><a runat="server" id="btout">bt</a></li>
</ul>
</EmptyDataTemplate>
</asp:GridView>

Put the <a> link outside of Gridview to directly access in code behind.
If you still want to put inside the grid then you will be able to access these <a> link through grid by doing lw3.FindControl.
After you get access to those control then you will be able to control visibility as well by doing:
btout.Visible = true; or
btout.Visible = false;
I Hope this will help you.

Related

Trying to call a variable using C# in HTML

I bet this question was asked before, but I couldn't find the right keywords to search for.
I am trying to call my variable (currentUser.FirstName) in the middle of the HTML, but it won't recognize the variable if I try. It does work if I don't put HTML tags around the code.
Here's my code:
<% var currentUser = ProjectGalaxy.Website.GetCurrentUser(); %>
<% if (currentUser != null)
{ %>
<ul class="nav navbar-nav navbar-right">
<li><a runat="server" href="~/pages/Dashboard.aspx">Welcome <%=
currentUser.FirstName %></a></li>
</ul>
<% } %>
Thank you,
Thomas
The variable you are creating within the first server block <%...%> is likely no longer in scope so when you exit the first server block and are writing the html you would have lost the context.
There are a couple of options depending if you are limited to addressing this in the display page only or are able to edit both the display and code behind.
Option 1. Remove the variable and refer to the underlying value from the server method directly.
There is a duplication of the call but it removes any scoping issues and keeps it localised if you are unable to edit the code behind.
<% if (ProjectGalaxy.Website.GetCurrentUser() != null) {%>
<ul class="nav navbar-nav navbar-right">
<li><a runat="server" href="~/pages/Dashboard.aspx">Welcome <%:ProjectGalaxy.Website.GetCurrentUser().FirstName%></a></li>
</ul>
<%}%>
Create a property in the code behind and assign the method value to that property. (Requires both display and code behind file changes)
// aspx.cs code behind
protected bool CurrentUser
{
get { return ProjectGalaxy.Website.GetCurrentUser(); }
}
// aspx display page
<% if (CurrentUser != null) {%>
<ul class="nav navbar-nav navbar-right">
<li><a runat="server" href="~/pages/Dashboard.aspx">Welcome <%:CurrentUser.FirstName%></a></li>
</ul>
<%}%>
Move control to the code behind and create an asp:panel server control with the code and toggle the visibility of the panel during the page lifecycle.
// aspx.cs code behind
pnlWelcomeMessage.Visible = true; // during page load if condition is true
// aspx display page
<asp:Panel ID="pnlWelcomeMessage" runat="server" CssClass="nav navbar-nav navbar-right" Visible="false">
<ul class="nav navbar-nav navbar-right">
<li><a runat="server" href="~/pages/Dashboard.aspx">Welcome <%:ProjectGalaxy.Website.GetCurrentUser().FirstName%></a></li>
</ul>
</asp:Panel>
Hope it helps.

Access a control in Codebehind that's in a Template

In my ASP.NET WebForms application using Scafolding I have many pages where I need to restrict certain links based on user's role.
For instance, in my Site.Master in my <LoggedTemplate> along with other <li>, I have a <li> for Admin page also. By default, that is not visible, but if the user is logged as an Admin, then I want it to make visible. Which I am not able to do. Here's the code for it :
<LoggedInTemplate>
<ul class="nav navbar-nav">
<li><a runat="server" id="adminLink" visible="false" href="~/Admin/Admin_Page">Admin</a></li>
<li><a runat="server" href="~/Inquiries/Default.aspx">Inquiry</a></li>
In my Codebehind, in Page_Load I am not able to access adminLink only.
Simialrly, in one of the Default page of a Model, the list has links to View, Insert & Delete. If the user is admin, then only I want to show Insert & Delete links. Here's the code for it :
<td>
<asp:HyperLink runat="server" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Details", Item.ChannelId) %>' Text="View" /> |
<asp:HyperLink runat="server" ID="editLink" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Edit", Item.ChannelId) %>' Text="Edit" /> |
<asp:HyperLink runat="server" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Delete", Item.ChannelId) %>' Text="Delete" />
</td>
</tr>
</ItemTemplate>
I tried adding
<% if (CommonUtilities.IsUserAdmin) { %>
hyperlinks for Insert & delete & finally
<% } %>
but this was giving error. I added ID to editLink, but again cannot access it in Page_Load method.
I am sure, their must be some method to work out with this which I am not able to find yet.
How to deal with this problem ?? Please help me, I have several pages & links to hide & show based on admin role.
Any help is highly appreciated.
Thanks
I think you are looking for FindControl. For example:
Label adminLabel = LoggedInTemplate.FindControl("adminLink") as Label;
adminLabel.visible = true;
Works for me in a few templates, dont know about LoggedInTemplate tho, but can't see why not.
edit: didn't realize ur using <a>. Not sure why you mix asp hyperlink and html but anyway, logic is still the same.
Thanks WEDEBE for pointing out to mix <a> and .
That point gave me a way out. In my design, I change <a> to <asp:HyperLink> & removed code from Codebehind. In design only I tried checking hte role of user & then adding full <li>. Like this :
<LoggedInTemplate>
<ul class="nav navbar-nav">
<% if (VincitoreCRMApplication.CommonUtilities.IsCurrentUserAdmin)
{ %>
<li> <asp:HyperLink runat="server" id="adminLink" NavigateUrl="~/Admin/Admin_Page.aspx">Admin</asp:HyperLink> </li>
<% } %>
<li><a runat="server" href="~/Inquiries/Default.aspx">Inquiry</a></li>
With the other 2 HyperLink's also I did the same way :
<% if (VincitoreCRMApplication.CommonUtilities.IsCurrentUserAdmin)
{ %>
<asp:HyperLink runat="server" ID="editLink" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Edit", Item.ChannelId) %>' Text="Edit" /> |
<asp:HyperLink runat="server" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Delete", Item.ChannelId) %>' Text="Delete" />
<% } %>
And this worked. But I realized one thing, when If I add
<% if (VincitoreCRMApplication.CommonUtilities.IsCurrentUserAdmin) { %>
like this, in a single line, it's not working. But on adding the curly braces in new line, it works. I know this sounds very strange, I also can't make out why it happens so. But it is fact, that what I have faced & learned.
I know this is quiet simple thing, but just in case my code helps anyone, have shared here.
Thanks

How do I manipulate the text in my Site.Master file in codebehind

I would like to carry out some string operations on the username in my Site.Master page before rendering it on the page.
Here's what the current code looks like:
<div class="login">
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<LoggedInTemplate>
Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>
</LoggedInTemplate>
</asp:LoginView>
</div>
For some reason, I cannot reference HeadLoginName.Text or something similar. What am I missing here?
Thanks for looking.
Possible Duplicate:
Find control in loginview
It is necessary to find the "HeadLoginName" control inside the "HeadLoginView" container first and then specify its Text property (see the Find Control with in LoginView control blog post for more information):
LoginName ln = (LoginName)HeadLoginView.FindControl("HeadLoginName");
ln.Text = ...;

replace controls in <li>

How do i replace the controls in a list item using server side code. I need to replace this
<li>
<asp:LinkButton ID="btnUpload" runat="server" OnPreRender="btn_PreRender" CommandName="Uploader"
TabIndex="2">Upload</asp:LinkButton>
or <a target="_blank" href="../PersonalInfo/MailingAddress.htm">Mail</a> the form.
</li>
with
<li>
<asp:LinkButton ID="hplnkViewDocument" runat="server" Text="View Document" SkinID="lnkBtnBlue"></asp:LinkButton>
</li>
I would have both controls in the li, then only show/hide the one you want, using the Visible property.
I believe you can put a pannel or some server side control that works as a container. Then you can add or remove from their controls collection whatever controls you like.
Lets say a pannel that you want to add a button to, just to give you an idea:
Button button = new Button ();
//Set properties accordingly
Pannel1.Controls.Add(button);
Also, Controls is a property of Control, so you will find it in any class that inherits Control. Even the page inherits from a class that inherits from control.
You have to read about "Page.FindControl Method (String)".
There is some sample as well.
You can also use
Page.Controls.Remove(btnUpload);
and then create the hplnkViewDocument as new LinkButton control
Page.Controls.Add(hplnkViewDocument);
;-)
If you wanted to be super lazy you could just wrap the either of them in a span with a given class and then add some css to the page head (assuming it runs at server) that has display:none; visiblity: hidden; for the class that shouldn't show
HTML:
<li>
<span class="one">
<asp:LinkButton ID="btnUpload" runat="server" OnPreRender="btn_PreRender" CommandName="Uploader" TabIndex="2">Upload</asp:LinkButton>
or <a target="_blank" href="../PersonalInfo/MailingAddress.htm">Mail</a> the form.
</span>
<span class="two">
<asp:LinkButton ID="hplnkViewDocument" runat="server" Text="View Document" SkinID="lnkBtnBlue"></asp:LinkButton>
</span>
</li>
Make sure to add either:
.one {display:none; visiblity: hidden;}
...or
.two {display:none; visiblity: hidden;}
...based on your runtime needs

Customized bulleted list items in ASP.NET

I am just a beginner in ASP.NET. My question is simple, I wanna add list items dynamically from the code behind file and I want each item to have a text and couple of images as hyperlinks. The HTML sample should be like,
<ul>
<li>do foo <img src="some_image.png" /></li>
<li>do bar <img src="some_image.png" /></li>
...
</ul>
The number of items is dependent on the collection retrieved by the code behind file.
P.S. my code behind file is written in C#
The Repeater control is the simplest way to create a customized bulleted list, plus it gives you complete control over the HTML you generate. To use it, set up a template like this:
<ul>
<asp:Repeater runat="server" ID="ListRepeater">
<ItemTemplate>
<li>do foo <a href='#'><img src='<%# Eval("ImageSource") %>' /></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
Then in your code-behind (or declaratively in your markup, depending on your preference), set the repeater's data source and bind it:
void Page_Load(object sender, EventArgs e) {
// Some method you've defined to get your images
List<string> imageList = GetImages();
ListRepeater.DataSource = imageList;
ListRepeater.DataBind();
}
ASP.NET renders the template once for each item in your data source.
The Repeater control has more features than what I've shown here, but this should get you started. Good luck!
Edit: a year after writing this answer, I still think repeaters are the best option among server controls, but more and more I prefer foreach statements right in my .aspx templates:
<ul>
<% foreach(Image image in this.Images) { %>
<li>do foo <a href='#'><img src='<%= image.Source %>' /></a></li>
<% } %>
</ul>
Just use the Repeater control. Simply and easy. :)
ASP.Net BulletedList. MSDN

Categories

Resources