I want to ask how to pass a variable from a page to another page.
example.
in (page1.aspx.cs) there is button click and textbox
protected void Button1_Click(object sender, EventArgs e)
{
textbox1.text = ;
}
in (page2.aspx.cs)
A = "hello"
// A is variable that can be change, A variable is coming from microC
What I want is show "hello" from page2 in textbox1.text when I click button1 in page1.aspx
You can pass the value as a querystring parameter.
So if you are using Response.Redirect you could do something like
protected void Button1_Click(object sender, EventArgs e){
Response.Redirect("Page2.aspx?value=" + taxtbox1.text);
}
On Page 2 you can get the value using Request["value"].ToString()
Notice that the querystring parameter name is what you request. So if you have ?something=else you will Request["something"]
One way is to place the value into some form of temporary storage: Cookie, Session, etc. And then redirect.
Another would be to redirect with a query string value. It really depends on your situation.
I'd recommend setting a session if this is necessary.
Session["sessionname"] = "";
Though it isn't ideal, is it possible to have everything on page1? You can switch with a panel control.
Without a Postback it is not possible!
With a Postback, yes it is (see Cross Page Postback). Also see in the link what you can have access to! (Access possibilities are page controls & public members).
Other options, Session variables, cookies, query string, etc!
u can use one of this ways:
1- Query string
page.aspx?ID=111&&Name=ahmed
2- Session
Session["session1"] = "your value";
3- Public property
public String prop1
{
get
{
return txt_Name.Text;
}
}
4- Controls Data
5- HttpPost
Related
I have a textbox in a user control uc1. I have embedded this uc1 in a page called default.aspx. My issue is after running the application and entering some data in the textbox, when refresh the page i would like to show the values that i have entered in the textbox and not clear the textbox. I would like help with code on how to achive this. Thanks in advance for your help.
Create a global variable at the top of your aspx.cs page:
public string textboxValue
{
get
{
if (ViewState["textboxValue"] != null)
return ViewState["textboxValue"].toString();
else
return "";
}
set
{
ViewState["textboxValue"] = value;
}
}
Then, in PageLoad(), assign textboxValue a value:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
textboxValue = MyTextBox.Value;
else
MyTextBox.Value = textboxValue;
}
You can also use textboxValue to assign the value of MyTextBox at any time, or use it in any other way that might be useful to you.
The default behavior for all asp.net server side controls (runat="server") is to maintain their state. If your textbox is being cleared when your page refreshes, you are likely clearing that value yourself in code.
Are you dynamically adding the textbox or user control? If so, are you doing that during PageInit? Adding them later may cause them to lose state.
I was able to refresh the page without clearing the value in textbox. I did it as below:
I created a public property in the UC1.vb as below:
Public Property textbox_value() As String
Get
If Session("textbox1") IsNot Nothing Then
Return Session("textbox1").ToString()
Else
Return ""
End If
End Get
Set(value As String)
Session("textbox1") = value
End Set
End Property
And in the page_load event of the user control i added the code below:
If IsPostBack Then
textbox_value= textbox1.Text
ElseIf Not IsPostBack Then ' First time the page is loaded or when the page is refreshed
textbox1.Text = textbox_value
End If
Hope it helps.
Scenario:
I am doing project in C# ASP.NET 4.
I have a page of question. When somebody clicks on question (ie a Link Button) he is redirected to page where user can give answer but first he needs to login. So I put Login to Answer button that redirects user to GuestLogin.aspx with question id like this :
protected void LoginToAnswwer_Click(object sender, EventArgs e)
{
int qidrequest = int.Parse(Request.QueryString["qid"]);
Response.Redirect("~/GuestLogin.aspx?qid=" + qidrequest);
//This is working OK
}
And then when I am redirected to GuestLogin.aspx, I am putting below code in LoginButton of built in Login Control.
protected void LoginButton_Click(object sender, EventArgs e)
{
int qidrequest = int.Parse(Request.QueryString["qid"]);
Response.Redirect("QDisplay.aspx?qid=" + qidrequest);
}
Which is not working.
Question:
How to pass querystring with login button of built login control in asp.net 4 ?
You could pass a return URL to the login page, like this:
Response.Redirect(String.Format("/auth/login.aspx?return={0}", Server.UrlEncode(Request.Url.AbsoluteUri)));
In the login page, after authenticating the user:
Response.Redirect(Request.QueryString["return"]);
Pass Parameters from One Page to Another Page using QueryString :
//Set the Querystring parameters
Note: Maximum length of the
string that can be passed through QueryString is 255.
string URL =“QueryString.aspx?Name=” + txtFirstName.Text + “&Address=” + txtAddress.Text + “&City=” + txtCity.Text ;
//After Setting the Querystring Paramter values Use Response.Redirect to navigate the page
Response.Redirect(URL);
In the Page Load Event of the Navigated
Page,You can access the querystring parameter values like below :
lblName.Text = Request.QueryString["Name"].ToString();
lblAddress.Text = Request.QueryString["Address"].ToString();
lblCity.Text= Request.QueryString["City"].ToString();
That's how you have to use QueryString for passing parameters
I need to reference a control in my web app that was generated with the use of a master page. The name of the control in the HTML becomes something like this "ctl00$MainContent$ListBox1". Can I safely do this in the code?
string strName = "ctl00$MainContent$ListBox1";
if (Request.Form[strName] != null)
{
String selectedLanguage = Request.Form[strName];
}
PS. I cannot use ClientID property because this code is called from InitializeCulture() override.
You could, but what I do is set the MasterPage ID in my Init():
protected void Page_Init( object sender, EventArgs e )
{
// this must be done in Page_Init or the controls
// will still use "ctl00_xxx", instead of "Mstr_xxx"
this.ID = "Mstr";
}
ctl00 is the generated ID of your masterpage. In the code-behind, you can set this.ID to whatever you want and any sub-content will be prefixed with that ID instead.
The problem with the code you have above is you're relying on a magic string for a control ID - you need to be careful with this as controls get moved into user controls and master pages become nested. I'm not sure why you can't use ListBox1.SelectedValue?
Hi I have problem with store user name in Session. On log on page I store user name to session. User input credentials on logon page and then is redirect on default page. I need in class Default access to variable store in session.
Logon.aspx
<script runat="server">
void Login_Click(object sender, EventArgs e)
{
Session["user"] = tbUserName.Text;
//You can redirect now.
Response.Redirect(FormsAuthentication.GetRedirectUrl(tbUserName.Text,
false));
}
</script>
secod main page is here:
Default.aspx-Default.aspx.cs
public partial class Default : Page,
IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
string userName = (string) (Session["user"]);
}
but result is that userName is empty.
Web config is here:
<sessionState cookieless="true" regenerateExpiredSessionId="true" />
Thank you for advice.
If the snippet is on the module level (as alluded to by Bala R in the comments) the session state has probably not been set yet. Try getting it in a method that is part of the page life cycle (like page_load, etc.).
Why not get it directly from HttpContext HttpContext.Current.User.Identity.Name
Edit: I guess you are trying to get it at page level instead of in page_load or another place. That's why you are not getting a session value.
I hope below will work for you
protected void Page_Load(object sender, EventArgs e)
{
string userName = (string) (Session["user"]);
}
Update: This is incorrect.
You're losing the (cookieless) session when you redirect. Cookieless sessions are maintained in the URL, and are lost when you explicitly redirect, so there is nothing in the session on your second page.
So I have an asp:Login field on my login page.
However, I want to use a path for the create account url and the forgot password url. So I have to do it in Page_Load or maybe Page_Init. Regardless, neither option works, it simply refuses to modify the login form.
protected void Page_Load(object sender, EventArgs e)
{
string accountpath = Request.Url.AbsoluteUri + "/user/RequestAccount.aspx";
string forgotpath = Request.Url.AbsoluteUri + "/user/ForgotPassword.aspx";
lgnMain.CreateUserUrl = accountpath;
lgnMain.PasswordRecoveryUrl = forgotpath;
lgnMain.InstructionText = "test";
lgnMain.Focus();
}
protected void Page_Init(object sender, EventArgs e)
{
string accountpath = Request.Url.AbsoluteUri + "/user/RequestAccount.aspx";
string forgotpath = Request.Url.AbsoluteUri + "/user/ForgotPassword.aspx";
lgnMain.CreateUserUrl = accountpath;
lgnMain.UserName = "test";
lgnMain.InstructionText = "test";
lgnMain.PasswordRecoveryUrl = forgotpath;
}
The CreateUserUrl and the PasswordRecoveryUrl are ignored if you haven't set the CreateUserText and PasswordRecoveryText properties respectively. Since the Text properties probably don't need to be dynamic, just set them in the ASPX (although you could still set them in the code behind if required), and then the dynamic setting of the URL properties (in the Page_Load event) should work without problem.
Documentation here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login_members(v=vs.85).aspx
From the Documentation above:
If the CreateUserText property is
empty, the link to the registration
page is unavailable to the user.
If the PasswordRecoveryText property
is empty, the link to the password
recovery page is not available to the
user.
have you tried setting it in the markup?
<asp:Login id="lgnMain" runat="server"
CreateUserText="Register"
CreateUserUrl="~/user/RequestAccount.aspx"
PasswordRecoveryText = "Forgot Password"
PasswordRecoveryUrl = "~/user/ForgotPassword.aspx" >
</asp:Login>