Display some control with respect to its Path - asp.net

Guys I want to ask that I have a control naming page1.ascx it has a tool bar and i have there select and close button. on toolbar . I want that when I go from page2.ascx control to page.ascx control then show that button (Select and Close) but when i come from page3.ascx control to Page1.ascx then don't show that button(Select and close) How to do it?

When you are redirecting one web page to another, you are passing values stored in variables, so 4 ways you can do that
1) Session Variable Previous page/Viewstate Variable
2)QueryString http://mysite.com/currentpage?from=previousPage
3)Using Property of a class eg. By setting Property set while redirecting and get while printing.
Here is another way.
Try it
public string GetCurrentPageName()
{
string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
return sRet;
}
public string GetPreviousPageName()
{
string sPath = Page.PreviousPage.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
return sRet;
}
If you want Previouspage title then you can try this :
string PPagetitle= this.Page.PreviousPage.Title;
Or you can do little bit Javascript like this :
<asp:button id="m_BackButton" runat="server" onclientclick="goBack()" />
<script type="text/javascript">
function goBack()
{
history.go(-1);
}
</script>
Explanation of 3rd one : While moving from one web page to another you can pass data through class properties - but it works for Server.Transfer() only and destination page you need to define
<%# PreviousPageType VirtualPath="~/SourcePage.aspx" %>
.Here is the demo
// Source page
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div><div>
<asp:Label ID="lblUsername" runat="server" BorderStyle="None" Font-Bold="True" Font-Names="Garamond"
Font-Size="Large" Style="z-index: 100; left: 240px; position: absolute; top: 32px"
Text="Username" Width="73px"></asp:Label>
<br />
<asp:Label ID="lblPassword" runat="server" BorderStyle="None" Font-Bold="True" Font-Names="Garamond"
Font-Size="Large" Style="z-index: 101; left: 237px; position: absolute; top: 80px"
Text="Password" Width="80px"></asp:Label>
<br />
<br />
<asp:TextBox ID="txtPassword" runat="server" Style="z-index: 102; left: 355px; position: absolute;
top: 80px" TextMode="Password" Width="151px"></asp:TextBox>
<asp:TextBox ID="txtUsername" runat="server" Style="z-index: 103; left: 357px; position: absolute;
top: 30px" Width="153px"></asp:TextBox>
<asp:Label ID="lblMessage" runat="server" Font-Bold="False" Font-Names="Bookman Old Style"
Font-Size="Medium" Style="z-index: 104; left: 354px; position: absolute; top: 130px"
Text="Message :"></asp:Label>
<asp:Button ID="btnSubmit" runat="server" Font-Bold="True" Font-Names="Garamond"
Font-Size="Large" OnClick="btnSubmit_Click" Style="z-index: 106; left: 289px;
position: absolute; top: 160px" Text="Submit" />
</div>
</div>
</form>
</body>
</html>
Source page code behind :
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
private string myUserName;
/*
* Defining Properties in the source page to be Accessible on the destination page.
* means Exposing data to other pages using Properties
* To retrieve data from source page,Destination page must have
* <%# PreviousPageType VirtualPath="~/Default.aspx" %> Directive added below <%# Page %> Directive
*/
public string propUserName
{
get { return myUserName; }
set { myUserName = value; }
}
private string myPassword;
public string propPassword
{
get { return myPassword; }
set { myPassword = value; }
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if ((txtUsername.Text == "chandan") && (txtPassword.Text == "niit"))
{
myUserName = txtUsername.Text;
myPassword = txtPassword.Text;
}
Server.Transfer("Description.aspx");
}
}
Destination Page :
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Description.aspx.cs" Inherits="Description" %>
<%# PreviousPageType VirtualPath="~/Default.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label2" runat="server" Text="Password" style="z-index: 100; left: 336px; position: absolute; top: 69px" Font-Bold="True" Font-Size="Larger"></asp:Label>
<asp:Label ID="Label1" runat="server" Text="UserName" style="z-index: 102; left: 333px; position: absolute; top: 28px" Font-Bold="True" Font-Size="Larger"></asp:Label>
</div>
</form>
</body>
</html>
Destination Page Code Behind :
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Description : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = PreviousPage.propUserName;
Label2.Text = PreviousPage.propPassword;
}
}

Related

Fileupload is rendering in different way in different browsers in asp.net

FileUpload control is rendering differently in different browsers. In the Firefox, it's showing Browse/No file selected while in Chrome Choose File/No file chosen. Is there a way to display the File Upload in the same way irrespective of browser. My ASP.NET Code and screenshots are attached below:
<asp:Label runat="server" ID="lblFileName" AssociatedControlID="fileUploader"></asp:Label>
<asp:FileUpload ID="fileUploader" runat="server" Width="350" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" />
Firefox
Chrome
If you want consistent Button Look-And-Feel....you will need to apply the style you wish it to be.
You are using the Default Browser CSS styles. Check your Developer Tools for the associated browser to see how the Look-And-Feel of the controls is being rendered.
try following code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Uploader Demo</title>
<script src="Scripts/jquery-1.8.2.js"></script>
<script language="javascript" type="text/javascript">
function hookFileClick() {
// Initiate the File Upload Click Event
document.getElementById('fileUploader').click();
}
function fnOnChange(obj)
{
document.getElementById("txtUploadFile").value = obj.value;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" runat="server"
id="txtUploadFile" disabled="disabled" />
<input type="button" runat="server"
id="btnUpload" value="Browse"
onclick="hookFileClick()" />
<asp:Button runat="server"
ID="btnUploadFileToServer"
Text="Upload File To Server"
OnClick="btnUploadFileToServer_Click" />
<asp:FileUpload runat="server"
ID="fileUploader" Style="visibility: hidden;"
onchange="fnOnChange(this);" />
</div>
</form>
</body>
</html>
C#
protected void btnUploadFileToServer_Click(object sender, EventArgs e)
{
string strFileName = fileUploader.FileName;
fileUploader.SaveAs("d:\\Somepath\\ " + strFileName);
}
Thank you very much #GoldBishop for giving me some hint to write custom css and the following css worked for me.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.upload-btn-wrapper {
position: relative;
overflow: hidden;
display: inline-block;
}
.btn {
border: 2px solid gray;
color: gray;
background-color: white;
padding: 8px 20px;
border-radius: 8px;
font-size: 20px;
font-weight: bold;
}
.upload-btn-wrapper input[type=file] {
font-size: 100px;
position: absolute;
left: 0;
top: 0;
opacity: 0;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="upload-btn-wrapper">
<asp:Label runat="server" ID="lblFileName" AssociatedControlID="fileUploader"></asp:Label>
<asp:FileUpload ID="fileUploader" runat="server" Width="350" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" CssClass="btn" />
</div>
</form>
</body>
</html>

Doesn't recognize some labels

I read a tutorial about Asp.Net and copy some of the code in it it doesn't compile and I can't understand why.
Here's my code:
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="MyFonts" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style>
body
{
font-family: Verdana, Arial, Sans-Serif;
font-size: small;
color: yellowgreen;
}
.heading1
{
font-weight: bold;
font-size: large;
color: lime;
}
.heading2
{
font-weight: bold;
font-size: medium;
font-style: italic;
color: #C0BA72;
}
.blockText
{
padding: 10px;
background-color: #FFFFD9;
border-style: solid;
border-width: thin;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btSubmit" Text="Submit" runat="server" OnClick="Button_Click" />
<div>
<asp:Label ID="Label1" runat="server" />
</div>
<div>
<asp:Label CssClass="heading1" ID="Label2" runat="server" Text="This Label Uses heading1"/>
<br />
This is sample unformatted text.<br /><br />
<asp:Label CssClass="heading2" ID="Label3" runat="server" Text="This Label Uses heading2"/>
<br />
Here's more unformatted text.<br />
<br />
<div class="blockText" id="DIV1" runat="server">
This control uses the blockText style. This control uses the blockText style. This
control uses the blockText style. This control uses the blockText style.
</div>
<asp:Label ID="lblTime" runat="server" OnInit="lblTime_Init"/>
</div>
</div>
</form>
</body>
</html>
Default.aspx.cs:
public partial class MyFonts : System.Web.UI.Page
{
private void Page_Load(Object sender, EventArgs e)
{
}
public void Button_Click(Object sender, EventArgs e)
{
Response.Write(((Button)sender).Text);
Label1.Text = "You clicked <b>" + ((Button)sender).Text + "</b>";
}
protected void lblTime_Init(object sender, EventArgs e)
{
lblTime.Font.Name = "Verdana";
lblTime.Font.Size = 20;
lblTime.Font.Underline = true;
lblTime.Font.Bold = true;
lblTime.Font.Italic = true;
lblTime.Font.Overline = true;
lblTime.Font.Strikeout = true;
lblTime.Text = DateTime.Now.ToString() + ". Font Name: " + lblTime.Font.Name;
}
}
On default.aspx.cs it doesn't recognize lblTime and Label1.
Why does it happen?
Earlier, before I added LabelTime it runs just fine and I can't understand what's wrong with the code I added.
Edit: in Default.aspx.cs no control from Default.aspx recognized...
Thank you
It seems that the code was right but I used probably a wrong project, that's why it didn't compile...

Change Variable Value with Asp.Net Button Click event

I'am beginner in ASP.net. I recently created some Web Form Application with following code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="MainForm.aspx.cs" Inherits="TestAssignVariable.MainForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="CHECK" OnClick="Check_Data" />
<asp:Label ID="ketCekData" runat="server" Text="Label"></asp:Label>
</div>
<div>
<asp:Button ID="Button2" runat="server" Text="ASSIGN" OnClick="Assign_Data" />
<asp:Label ID="labelProcess" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
And code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestAssignVariable
{
public partial class MainForm : System.Web.UI.Page
{
private string file_path = "startx";
int a = 12;
private string FilePath
{
get
{
return file_path;
}
set
{
file_path = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Check_Data(object sender, EventArgs e)
{
ketCekData.Text = a.ToString() ;
}
protected void Assign_Data(object sender, EventArgs e)
{
// FilePath = "AWESOME";
a = 100;
labelProcess.Text = "Data Assigned";
}
}
}
So there is two button with ID Button1 and Button2. When Button2 was clicked, then it's firing an event to change the value of variable a from 12 to 100. Button1 then displaying the value of variable a on label ketChekData. So when I click Button2 followed Button1 there must be 100 displayed in label ketCekData. But I dont understand why this is not worked: there still 12 displayed on label ketCekData.
The Value of a is declared outside the page_load .So on Every Postback value is Resetting. So create a hidden Field in Aspx
In Aspx
<asp:HiddenField ID="hdna" value="12" runat="server" />
In Cs
Remove int a=12;
protected void Check_Data(object sender, EventArgs e)
{
ketCekData.Text = hdna.value ;
}
Take a look at my dotnetfiddle here : https://dotnetfiddle.net/L8lUSY
Html
#model HelloWorldMvcApp.ViewModelExample
#{
Layout = null;
}
<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<!-- CSS Includes -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<style type="text/css">
.field-validation-error {
color: #ff0000;
}
</style>
</head>
<body>
<div class="container">
<div class="col-md-6 col-md-offset-3">
<h1>Create item</h1>
<div class="form-group parentId">
#Html.LabelFor(m => m.ParentId)
#Html.TextBoxFor(m => m.ParentId)
</div>
<button class="button1">Button 1</button>
<button class="button2">Button 2</button>
</div>
</div>
<!-- JS includes -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
$(function(){
$('.parentId').hide();
$('.button1').on('click', function()
{
$('.parentId').toggle();
});
$('.button2').on('click', function()
{
$('#ParentId').val('100');
});
});
</script>
</body>
</html>
It may just be easier for you to use javascript / jQuery to change the value

ASP.net - Center Align Panel?

I need to center align a panel. I use the following:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="TestDefault.aspx.cs" Inherits="tregware.TestDefault" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="Root" runat="server" BackColor="Red" HorizontalAlign="Center" Style="left: 0px; top: 0px; width: 100%; height: 100%; position: absolute; z-index: 0;">
<asp:Panel ID="Body" runat="server" BackColor="Blue"
Style="left: 0px; top: 24px;width: 800px; height: 100%; position: absolute; z-index: 0;"
HorizontalAlign="Center">
</asp:Panel>
</asp:Panel>
</form>
</body>
</html>
However the panel ("Body") will not center. How would I do so?
You've set position: absolute, so a lot of the regular methods you would use to center elements probably won't work.
Since you've set a fixed width of 800px, you could use:
Style="left: 50%; margin-left: -400px; ..."
Panel has the property of HorizontalAlign
<asp:Panel id="reqId" runat="server" HorizontalAlign="center" >
Your Text Here
</asp:Panel>
Create a css called .panel { align:center }
and then add it to the panel CssClass = "panel"

text in textbox shows up as circles instead of regular characters?

When i type in either of the textboxes i get little circles appearing instead of text. Why is this happening and how do i stop it?
Code is as follows:
HTML:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="Foods.MainPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link id="Link1" rel="stylesheet" type="text/css" href="Styles/mainStyle.css"/>
</head>
<body>
<form id="form1" runat="server">
<div class = "userIDTboxDiv">
<div class = "userIDTboxText">
Enter User ID:
</div>
<asp:TextBox id="userIDBox" TextMode="password" runat="server" Height="52px"
Width="200px" />
</div>
<div class = "passwordTboxDiv">
<div class = "passwordTboxText">
Enter User Password:
</div>
<asp:TextBox id="TextBox1" TextMode="password" runat="server" Height="52px"
Width="200px" />
</div>
</form>
</body>
CSS:
body
{
text-decoration:none;
background: white;
}
input
{
margin-left: 0px;
margin-top: 7px;
}
.userIDTboxDiv
{
padding-top: 20%;
padding-left: 45%;
width: 15%;
height: 30%;
}
.userIDTboxText
{
padding-left: 17%;
height: auto;
width:203px;
}
.passwordTboxDiv
{
padding-top: 2%;
padding-left: 45%;
width: 15%;
height: 111px;
}
.passwordTboxText
{
padding-left: 10%;
height: auto;
width:203px;
}
I would guess this has something to do with it:
TextMode="password"
in your <asp:TextBox> elements. Remove the TextMode or change them to:
TextMode="SingleLine"
which happens to be the default.

Resources