Show/Hide href link on click on a button - c#

On a BUTTON click a detail page is generated.
I have a href link when click on it, it navigates in the page.
But on page load there should be only the button (on Clicking it main page is generated) but the href link is also appearing.
I want on the page load there should be only one button by clicking on it href link should appear.
And should disappear when another button is clicked.
Scrip:
$(document).ready(function () {
$('#priorityC').hide();
$('#perC').hide();
});
$('#btnAnalyse').click(function () {
$('#priorityC').show();
$('#perC').show();
});
This is the button:
<asp:ImageButton ID="btnAnalyse" runat="server" OnClick="btnAnalyse_Click"/>
This is the href link which i want to show only on the click of the above button:
Hourly Detailed Priority Representation
<a name="priorityPer">
<div id="perC" class="center">
<asp:Label ID="lblDPTC" runat="server" Text="Detailed Percentage representation of Ticket Count" Visible="false"></asp:Label>
</div>
</a>
Its hiding on page load but not showing on button click.

<asp:ImageButton ID="btnAnalyse" runat="server" ImageUrl="image1.jpg" OnClick="btnAnalyse_Click"/>
Hourly Detailed Priority Representation
<a name="priorityPer">
<div id="per" class="center">
<asp:Label ID="lblDPTC" runat="server" Text="Detailed Percentage representation of Ticket Count" Visible="false"></asp:Label>
</div>
</a>
and on your backend page( codepage.cs)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
linkid.Visible = false;
}
}
protected void btnAnalyse_Click(object sender, ImageClickEventArgs e)
{
if (linkid.Visible == false)
{
linkid.Visible = true;
}
}
protected void btnAnother_Click(object sender, EventArgs e)
{
linkid.Visible = false;
}

You can write your href link inside a div and using Jquery, you can hide and show the the div accordingly.
Code snippet
<script>
// On load hide the div
$(document).ready(function(){
$('#MYDIV').hide();
};
// call this function on button click to show/hide the link
function showHideLink(buttonName)
{
if(buttonName=='blah')
{
$('#MYDIV').hide();
}
else
{
$('#MYDIV').show();
}
}
</script>
Hope this helps.

test.aspx
<li class="nav-item">
<a class="nav-link" id="AdminFaciliy" href="charts.html" runat="server">
<i class="fas fa-fw fa-user">
</i>
text.aspx.cs
if (Utype.Trim().ToUpper()=="ADMIN"){
AdminFaciliy.Visible = true;
}

Related

How can update usercontrol in masterpage

I have a master page and an user control on it.
I have a two buttons in the user control for create or remove session, and a label that shows a session text, when click the buttons nothing happens and user control doesn't update I should refresh the page,
Would you please anybody help me to fix this issue ?
This is my master page markup:
<form runat="server">
<div>
<!--previous codes-->
<nav class="navigation">
<div class="wrapper">
<controller:menu runat="server" ID="menu" />
<controller:user runat="server" ID="user" />
</div>
</nav>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
<!--Next Codes-->
</div>
</form>
and this is my user control
<ul class="nav signup">
<li class="no-drop-down">
<asp:Label ID="_signupbutton" runat="server" CssClass="sr">Test</asp:Label>
<div class="signup-dropdown">
<asp:PlaceHolder ID="_defaultuser" runat="server" Visible="false">
<div class="notloggeduser">
<asp:LinkButton ID="LinkButton1" runat="server" CssClass="usercontroller"
OnClick="LinkButton1_Click" Text="????">
</asp:LinkButton>
</div>
</asp:PlaceHolder>
<asp:PlaceHolder ID="_signedup" runat="server" Visible="false">
<div class="defaultuser">
<ul>
<li>
<asp:LinkButton ID="_userlogout" runat="server" CssClass="usercontroller" OnClick="_userlogout_Click">
<i class="fa"></i>????
</asp:LinkButton>
</li>
</ul>
</div>
</asp:PlaceHolder>
</div>
</li>
</ul>
and this is user control codebehind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["signup"] == null)
{
_signupbutton.Text = "??? ??? / ????";
_defaultuser.Visible = true;
}
else
{
_signupbutton.Text = "<i class=\"fa\"></i> " + Session["signup"].ToString();
_signedup.Visible = true;
}
}
}
protected void _userlogout_Click(object sender, EventArgs e)
{
Session.Remove("signup");
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Session.Add("signup", "????? ??????????");
}
Your order of events is a bit messed up here.
Page_Load will execute before the event handlers for the click events. You need to perform the setup of the button state from the event handlers, as if you do it in page load your session object wont have been updated yet.
You will need to do a few things here (this isn't tested but should show you the logic):
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) // This will run when the page is loaded but not from a post back / button click etc (e.g. from your page refresh)
{
this.SetButtonState();
}
else
{
// use this for things you want to happen on postback only
}
}
private void SetButtonState()
{
if (Session["signup"] == null)
{
_signupbutton.Text = "??? ??? / ????";
_defaultuser.Visible = true;
}
else
{
_signupbutton.Text = "<i class=\"fa\"></i> " + Session["signup"].ToString();
_signedup.Visible = true;
}
}
protected void _userlogout_Click(object sender, EventArgs e)
{
Session.Remove("signup");
// Update your button state
this.SetButtonState();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Session.Add("signup", "????? ??????????");
// Update your button state
this.SetButtonState();
}

Text property of a TextBox is empty when an async PostBack occurs

I've had a look at lots of posts and I feel like I'm going crazy as nothing I've tried seems to work. I simply want to click a button and retrieve the value of a textbox.
I'm using web forms with a site.master so not sure if this could be affecting the problem, but most of the solutions I've seen don't appear to be using a site.master.
I'm not binding the textbox, initially I just wanted to create a contact form.
<%# Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="Blackburn_Pulse.About" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<script>$("#menuAbout").addClass("navi-active");</script>
<div class="contentBody">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="tb_Test" EnableViewState="true" CausesValidation="false" runat="server"></asp:TextBox>
<asp:LinkButton ID="btn_Test" runat="server" OnClick="btn_Test_Click" Text="send test" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn_Test" EventName="click" />
</Triggers>
</asp:UpdatePanel>
</div>
</asp:Content>
public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Page.Master.EnableViewState = true;
if (IsPostBack)
{
var test_var = tb_Test.Text; //returning empty
}
}
protected void btn_Test_Click(object sender, EventArgs e)
{
var test_var = tb_Test.Text; //returning empty
}
}
UPDATE:
site.master.cs
public partial class SiteMaster : MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindCategories();
}
}
protected void BindCategories()
{
Categories Categories = new Categories();
rpt_categories.DataSource = Categories.Get_Categories();
rpt_categories.DataBind();
}
protected void lb_newsCat_Command1(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "naviTo":
//Redirect user to selected category
Response.Redirect("~/" + e.CommandArgument.ToString());
break;
}
}
}
I'm converting a PHP website to an ASP.net website. Turns out I had another HTML form tag on a search box that I haven't started working on yet.
Unfortunately the debugger doesn't throw an error if you have another form tag so anybody else who's just spent hours of their lives searching, double check you don't have more than 1 form tag!
In my site.master.aspx file, I had a search box as follows:
<form method="post" action="?">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-primary my-2 my-sm-0" type="submit">Search</button>
</form>
Once I took the extra form tag out, everything worked fine.

Open asp LinkButton in a new tab

I have a button in my gridview and when the users click on it, it goes to the page. However, if you right click on it and "open link in a new tab" it goes to a blank page. I want it so when the user right click on it and "open link in a new tab" to go to the page. This is the code that I have so far:
aspx
<asp:LinkButton ID="lnkEditbtn" data-toggle="tooltip" title="View Request" OnClick="lnkEditbtn_Click" runat="server" class="btn btn-primary btn-sm" Text="<%# bind('ticketID')%>"></asp:LinkButton>
c#
protected void lnkEditbtn_Click(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
Label lblid = (Label)gvr.FindControl("lblMovie");
int id = Convert.ToInt32(lblid.Text.ToString());
SecureQueryString qs = new SecureQueryString();
qs["ID"] = id.ToString();
Response.Redirect("viewMovie.aspx?qs=" + qs.ToString());
}
you cannot do this with linkbutton because it redirects to the desired view after you click on it but you can use asp:HyperLink and set its value like
<asp:HyperLink ID="lnkEditbtn" data-toggle="tooltip" Text="View Request" runat="server" NavigateUrl='<%# Eval("ticketID", "~/viewMovie.aspx?qs={0}") %>' class="btn btn-primary btn-sm" ></asp:HyperLink >
Edit
if you want the URL to be encrypted first create a class
public static class encrypt
{
public static string encvalue(int id)
{
SecureQueryString qs = new SecureQueryString();
qs["ID"] = id.ToString();
return qs.ToString()
}
}
and your hyperlink will be
<asp:HyperLink ID="lnkEditbtn" data-toggle="tooltip" Text="View Request" runat="server" NavigateUrl='<%# String.Format("~/viewMovie.aspx?qs={0}",encrypt.encvalue(Convert.ToInt32(Eval("ticketID")))) %>' class="btn btn-primary btn-sm" ></asp:HyperLink >
Link button in server side gets rendered to Hyperlink in client side with 'href' as href="javascript:__doPostBack('lnkEditbtn',''), which is nothing but a postback to the server from the link button. So when you right click and open the link in a new tab, it posts to the server and hence it comes up as blank page in new tab.
What you can do is use code similar to the below code:
<style>
.hide {
display:none;
}
</style>
<script>
function postBack() {
__doPostBack('lnkEditbtn', '');
return false;
}
</script>
<asp:LinkButton ID="lnkEditbtn" runat="server" OnClick="lnkEditbtn_Click" Text="Link" CssClass="hide"></asp:LinkButton>
Link
protected void lnkEditbtn_Click(object sender, EventArgs e)
{
var linkButton = (Control)sender as LinkButton;
}
With this code, you are hiding the link button and using the Anchor tag instead.
Href in Anchor tag will be invoked when you right click. And when you click the link, "postBack" JS method will be triggered that invokes the server side event handler for the Link Button.
And there by both right click and left click works.

How to remove html code at the time of page loads ck editor loading the text in asp.net

In my wed application, I have one page it contains ck editor when i load that page ck editor showing some fraction of seconds HTML code after it will display text in ck editor. I need to stop that showing fraction of seconds HTML code how can i do this can anyone tell me.
My code is:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetTemplate();
}
}
Public void GetTemplate()
{
ShowTemplate();
}
Public void ShowTemplate()
{
//showTemplate code
CKEditor1.Text = html;
}
Thank you.
CKEditorYou can use a simple javascript trick to "hide" the CKEditor until it has been initialized.
<div style="height: 400px; width: 600px;">
<div style="display: none" id="CKPlaceHolder">
<asp:TextBox ID="CKEditorTextBox" runat="server" TextMode="MultiLine"></asp:TextBox>
</div>
</div>
I use 2 <div> elements. One to actually hide the editor and the first one as a placeholder to prevent the jumping of the content once the editor will be shown.
And then somewhere in JavaScript:
setTimeout(function () { document.getElementById('CKPlaceHolder').style.display = "block"; }, 500);

Dynamically add the menu based on session value in asp.net

I am working on a project where i have to show different menu based on username
i have added the menu in master page
The code is
<ul id="ul_myLst" runat="server">
<li>Testimonial</li>
<li>About Us</li>
<li>Registartion</li>
<li id="student" runat="server" visible="false">
profile
<ul>
<li>
View profile
</li>
<li>
Edit profile
</li>
</ul>
</li>
<li id="abc" runat="server" visible="false" >Admin</li>
<li id="Li1" runat="server" visible="false" >Reports</li>
</ul>
where i want to show three hidden value when user logged in as "admin"
here is my .cs code
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Session["UserName"] as string))
{
admin();
}
}
private void admin()
{
if (Session["UserName"].ToString() == "admin")
{
HtmlGenericControl ul = (HtmlGenericControl)(this.FindControl("abc"));
ul.Style["visibility"] = "visible";
}
}
This code is not giving me any error but it's also not showing me the desired output..
You can hide and show it this way:
ul.Style.Add("visibility","visible"); // for showing
ul.Style.Add("display","block");
and for hiding:
ul.Style.Add("visibilty","hidden"); // for hiding
ul.Style.Add("display","none");
Try using display property :
ul.Style.Add("display", "none");
Since your control has an id and runat="server", you may also use :
abc.Visible = false;
visible="false" does not touch an element's style at all. This is setting the Control.Visible property of the control you are manipulating. When this property is set to false, the control is not rendered to the page at all - it is just absent on the final markup. So on the server side you actually want this in admin method:
ul.Visible = true;
Use this code
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Session["UserName"] as string))
{
admin();
}
}
private void admin()
{
if (Session["UserName"].ToString() == "admin")
{
this.Page.FindControl("abc").Visible = true;
}
}
If you use in Master page then use this code
private void admin()
{
if (Session["UserName"].ToString() == "admin")
{
this.Master.FindControl("abc").Visible = true;
}
}
thanks

Categories