How can update usercontrol in masterpage - c#

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();
}

Related

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.

Show/Hide href link on click on a button

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;
}

Retrieve Selected Checkbox Value From Previous Page

I have an asp.net webform website which stores data in a session and on the 3rd page displays the entries entered on pg1 & pg2.
On the first page the first checkboxes is pre-selected by default but if the user selects/unselects a checkbox, when the user is on the second page, there is a back button but when it's clicked I don't know how to re-show the checkboxes selected/unselected as only the default one is checked.
I'm new to using ASP and session storing so I may be doing something completely wrong. How can I resolve my situation?
My code is:
HTML
<div class="form-group">
<div class="col-xs-offset-0 col-sm-offset-4 col-sm-3">All services</div>
<div class="col-sm-1">
<asp:CheckBox ID="Step02AllServices" runat="server" Checked="True" />
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-0 col-sm-offset-4 col-sm-3">Site content uploading only</div>
<div class="col-sm-1">
<asp:CheckBox ID="Step02ContentUploading" runat="server" />
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-0 col-sm-offset-4 col-sm-3">Site content & layout checking</div>
<div class="col-sm-1">
<asp:CheckBox ID="Step02ContentLayoutChecking" runat="server" Enabled="False" />
</div>
</div>
Code Behind
protected void Step02SubmitButton_Click(object sender, EventArgs e)
{
Session["Step02AllServices"] = Step02AllServices.Checked;
Session["Step02ContentUploading"] = Step02ContentUploading.Checked;
Session["Step02ContentLayoutChecking"] = Step02ContentLayoutChecking.Checked;
Response.Redirect("/Quotation/pg3.aspx");
}
I know that is needs to be in my Page_Load just not sure how to do it.
The below is what I have for radio buttons and test fields on another page
if (txtData2.Text == string.Empty && Session["pg2"] != null)
{
txtData2.Text = Session["pg2"].ToString();
if (Session["pg2Yes"].ToString() == "Yes")
{
pg2Yes.Checked = Session["pg2Yes"].Equals("Yes");
}
if (Session["pg2No"].ToString() == "No")
{
pg2No.Checked = Session["pg2No"].Equals("No");
}
}
Let's assume that you are on page 3 and you clicked the back button which redirects you to page 2.
On load of page 2, you need to check whether it's a new load (not postback) and if it contains the values on session. If both are true, you need to get the value from session and make the checkbox selected.
So, write the following code in Page2Load
//if you are loading the new page
if (!Page.IsPostBack)
{
if(Session["Step02AllServices"] != null)
{
Step02AllServices.Checked = (bool) Session["Step02AllServices"];
//similarly assign other checkbox values as they are in session already
}
else
{
//do normal assignment
}
}
check the below simple implementation and enhance it according to your requirement and can be done using single page
aspx code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="div1" runat="server">
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:CheckBox ID="CheckBox2" runat="server" /><asp:Button ID="Button1" runat="server" Text="Next" OnClick="Button1_Click" />
</div>
<div id="div2" runat="server" visible="false">
<asp:Button ID="Button2" runat="server" Text="Back" OnClick="Button2_Click"/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button3" runat="server" Text="Next" OnClick="Button3_Click"/>
</div>
<div id="div3" runat="server" visible="false">
<asp:Button ID="Button4" runat="server" Text="Back" OnClick="Button4_Click"/>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
cs code:
using System;
namespace WebApplication1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
div1.Visible = false;
div2.Visible = true;
div3.Visible = false;
}
protected void Button2_Click(object sender, EventArgs e)
{
div1.Visible = true;
div2.Visible = false;
div3.Visible = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
div1.Visible = false;
div2.Visible = false;
div3.Visible = true;
Label1.Text = CheckBox1.Checked.ToString();
Label2.Text = CheckBox2.Checked.ToString();
Label3.Text = TextBox1.Text;
Label4.Text = TextBox2.Text;
}
protected void Button4_Click(object sender, EventArgs e)
{
div1.Visible = false;
div2.Visible = true;
div3.Visible = false;
}
}
}

DropDownList selected value lost on postback

On my page, i'm losing my dropdownlist's selected value on postback.
This is my aspx code.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="ProjectSearch.aspx.cs" Inherits="Site.Templates.ProjectSearch" MasterPageFile="~/Site/Templates/Framework/Base.master"
EnableViewState="true"%>
<asp:Content ID="Content1" ContentPlaceHolderID="Content" runat="server">
<div>
<ul>
<li class="filter">
<asp:DropDownList runat="server" ID="m_Filter" CssClass="form-control"/>
</li>
<li class="button">
<asp:LinkButton class="button-dark-orange" ID="m_search" runat="server" OnClick="m_search_click" >
Click
</asp:LinkButton>
</li>
</ul>
</div>
And in my code-behind there's nothing other than button click.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDropdown();
}
}
public void BindDropdown()
{
try
{
List<int> ds = Filter.GetData()
m_filter.DataSource = ds;
m_Filter.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void m_search_click(object sender, EventArgs e)
{
Response.Write(m_Filter.SelectedIndex);
}
It always writes 0. Any help?
only add few things
<asp:DropDownList runat="server" ID="m_Filter" CssClass="form-control" AutoPostBack="true" />
write belove code in code behind file
ddlproduct.DataSource = your method;
ddlproduct.DataTextField = "text";
ddlproduct.DataValueField = "value";
ddlproduct.DataBind();

Creating custom cookies

Here is the problem, first i will copy the code then i will explain what problem is.
protected void Page_Load(object sender, EventArgs e)
{
CheckIfCookieExists();
}
bool CheckIfCookieExists()
{
HttpCookie cookie = new HttpCookie("accpetedCookie");
cookie.Values.Add("username", "user");
cookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(cookie);
HttpCookie myCookie = Request.Cookies["accpetedCookie"];
if (myCookie == null)
{
return false;
}
if (!string.IsNullOrEmpty(myCookie.Values["username"]))
{
return true;
}
else
{
return false;
}
}
protected void OKButton_Click(object sender, EventArgs e)
{
if (!CheckIfCookieExists())
{
pnlDialog.Visible = true;
}
else
{
pnlDialog.Visible = false;
}
}
so on page load there is panel and ask client do you accept cookies if click yes goes to OKButton, and after the cookies are stored in browser(i can see them in resources) and when i click on other page on the same site, then the panel appears againg and ask me for the cookies. This is HTML side.
<div id="dialog-content">
<asp:Panel ID="pnlDialog" title="Cookies policy" runat="server">
<div id="dialog">
<p>
If you agree to accept cookies click Yes, otherwise click No!</p>
<div class="popup-btn-content">
<div class="popup-ok-btn">
<asp:Button runat="server" Text="Yes" ID="OKButton" onclick="OKButton_Click"
style="height: 26px" /></div>
<div class="popup-exit-btn">
<asp:Button runat="server" Text="No" ID="ExitButton"
onclick="ExitButton_Click" OnClientClick="return hidePanel()" /></div>
</div> </div>
</asp:Panel>
</div>
Try putting a !IsPostBack() around your Page_Load code like this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack())
CheckIfCookieExists();
}
I think that if you set/check the cookie on non postbacks, the cookie should remain.

Categories