Can any one tell me how can I print the current data in this format (dd-mm-yyyy) ?
I am using this command, but it is not working:
protected void TextBoxStartDate_TextChanged(object sender, EventArgs e)
{
TextBoxStartDate.Text = DateTime.Now.ToString();
// TextBoxStartDate.Text = DateTime.Now();
}
I want autofill textbox with a current date
how can i print the current data in this format (dd-mm-yyyy)
Lower mm means minutes..!!
You can use this format:
DateTime.Now.ToString("dd-MM-yyyy");
Change your code like this:
protected void TextBoxStartDate_TextChanged(object sender, EventArgs e)
{
TextBoxStartDate.Text = DateTime.Now.ToString("dd-MM-yyyy");
// TextBoxStartDate.Text = DateTime.Now();
}
Put this TextBoxStartDate.Text = DateTime.Now.ToString(); on PageLoad
protected void Page_Load(object sender, System.EventArgs e)
{
TextBoxStartDate.Text = DateTime.Now.ToString();
}
here is my ASP.cs code:
<asp:TextBox ID="TextBoxStartDate" runat="server" ontextchanged="TextBoxStartDate_TextChanged" Width="150px" Height="16px" ></asp:TextBox>
You can do it on you page Load event.
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = System.DateTime.Now.ToShortDateString();
}
You can write it on Page_Load event to get the Current Date Time
The Code will be:-
You must also have namespace included in your code,
aspx and cs:-
<asp:TextBox ID="TextBoxStartDate" runat="server" OnTextChanged="TextBoxStartDate_TextChanged" Width="150px" Height="16px" ></asp:TextBox>
protected void Page_Load(object sender, EventArgs e)
{
TextBoxStartDate.Text = DateTime.Now.ToString();
}
Also as per your code, you need to add onTextChanged event in your code-behind
protected void TextBoxStartDate_TextChanged(object sender, EventArgs e)
{
//do something here
}
UPDATE
You will get something like below in your textbox when you load your page
Related
I have a GridView with the select button turned on.
<asp:CommandField ShowSelectButton="True" />
I want to pass the row values to TextBoxes that are on another page. So I am just working with on to test with the moment.
I am currently using the following code.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string Property_Name;
Property_Name = GridView1.SelectedRow.Cells[4].Text;
Session["Property_Name"] = Property_Name;
CreateSurvey CS = new CreateSurvey();
CS.PropDetails();
Response.Redirect("CreateSurvey.aspx");
This is my code from the second page (CreateSurvey.aspx)
public void PropDetails()
{
var Property_Name = Session["Property_Name"];
Create_PropName.Text = Property_Name.ToString();
}
The "CreateSurvey.aspx" page opens but the Create_PropName TextBox is empty.
Am I missing something?
try this
if you wan't to use session
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string Property_Name;
Property_Name = GridView1.SelectedRow.Cells[4].Text;
Session["Property_Name"] = Property_Name;
//you don't need this as you already set session above
//CreateSurvey CS = new CreateSurvey();
//CS.PropDetails();
Response.Redirect("CreateSurvey.aspx");
}
while receiving you just need to call below code in page load
if(Session["Property_Name"] != null)
Create_PropName.Text = Session["Property_Name"].ToString();
if you want to use query string
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("CreateSurvey.aspx?PropName="+GridView1.SelectedRow.Cells[4].Text);
}
in second page load
if(Request.QueryString["Property_Name"] != null)
Create_PropName.Text = Request.QueryString["Property_Name"];
I have an ASP.NET project with a simple webpage.When I load the page the textbox display "Hello". When I click the "btnUpload" the text goes away.
I tried !IsPostBack in the Load_Form function gut the just make the text stay the same.
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "Hello";
}
protected void btnUpload_Click(object sender, EventArgs e)
{
TextBox1.Text = "Good Bye";
}
This is because you have not used
if(!IsPostBack) { }
inside Page_load() method
so modify your code as:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
TextBox1.Text = "Hello";
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
TextBox1.Text = "Good Bye";
}
Because, you have not used this, thats why, on the postback of button it changes the value to TextBox1.Text = "Good Bye"; but it follows completing the postback with Page_Load() so it changes it again to TextBox1.Text = "Hello"; that's the reason default value of Page_Load() is shown after render.
You have to use the following condition in the Page_Load event handler, not in the Load_Form:
if (!IsPostBack) {
TextBox1.Text = "Hello";
}
Your button click event handler is ok.
Hope it helps!
Please see below code :
Default.aspx:
<asp:TextBox ID="text1" runat="server"></asp:TextBox>
<asp:Button ID="btn" runat="server" OnClick="btn_Click" />
Make sure aspx page contain same code for button as shown above.
Let say I have this code on Page load of ASP.NET Webform
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "123";
}
and this is my control in aspx file
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
If I change the textbox data from 123 to 12548 or any thing in the front end and then I click the button
Now in this is my code behind button click event
protected void Button1_Click(object sender, EventArgs e)
{
string s = TextBox1.Text;
}
Now in TextBox1.Text I should be getting 12548 or updated value instead I am getting 123 which I have already set in page load.
Now I want to get the updated value, how may I do it the right way.
Wrap it in a NOT is Postback
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
TextBox1.Text = "123";
}
}
or remove it completely:
protected void Page_Load(object sender, EventArgs e)
{
//not here
}
<asp:TextBox ID="TextBox1" Text="123" runat="server"></asp:TextBox>
Modify the Page_Load as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
TextBox1.Text = "123";
}
}
The problem was this "In ASP.Net every time you cause a postback of any kind, including handling events like button clicks, you're working with a brand new instance of your page class that must be rebuilt from scratch. Any work you've done previously to build the page on the server is gone. That means running the entire page life cycle, including your page load code, and not just the click code.
Every time you perform any event in the front end it will recreate the page and call the pageload method again and the page would be actually reset. To avoid it one should use the following code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
//default code
}
}
OK so i am a bit lost and could do with some help.
I am creating a program that a user inputs data into a form on the default page (I have that working).
Then i use session variables to get the data input from a text box on the default page and place that data into a drop down menu on Page2 (I have that working).
What im trying to do now is use the data selected from the drop down on the page2 and output it on to a label. any help would be appreciated.
Page2 code bellow session that populates drop down
public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyFruit = Session["Fruitname"] as List<string>;
//Create new, if null
if (MyFruit == null)
MyFruit = new List<string>();
DropDownList1.DataSource = MyFruit;
DropDownList1.DataBind();
}
You can use SelectedIndexChanged event of DropDownList to handle this.
your AutoPostBack property of DropDownBox should be set to True
sample code as below:
Design code: page.aspx
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>name1</asp:ListItem>
<asp:ListItem>name2</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
CodeBehind File: page.aspx.cs
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedValue.ToString();
}
Not sure if this is what you are looking for but what I am guessing is you want an event for your drop down list to get the info and place it into a session to pass onto the next page
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string item=DropDownList.SelectedItem;
Session["selectedItem"]=item;
Response.Redirect("TheNextPageURL")
}
public partial class TheNextPage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Session["selectedItem"]!=null)
{
Label1.Text=Session["selectedItem"].toString();
}
}
}
Hope that helps
I have an appplication in ASP.Net with C#.
In the masterpage in aspx file I have 3 image buttons with 3 languages: english, french, romanian:
<asp:ImageButton ID="ImgRo" ImageUrl="~/Images/Romania Flag.png" runat="server"
Height="25px" ToolTip="RO" style="border-color:black;
background-color:black" OnClick="ImgRo_CLick"/>|
<asp:ImageButton ID="ImgEn" ImageUrl="~/Images/United States Flag.png" runat="server"
Height="25px" ToolTip="EN" style="border-color:black;
background-color:black" OnClick="ImgEn_CLick"/>|
<asp:ImageButton ID="ImgFr" ImageUrl="~/Images/France Flag.png" runat="server"
Height="25px" ToolTip="FR" style="border-color:black;
background-color:black" OnClick="ImgFr_CLick"/>|
When I click one of each I have events to save chosen language on session :
protected void ImgRo_CLick(object sender, EventArgs e)
{
SaveLanguage("Ro");
}
protected void ImgEn_CLick(object sender, EventArgs e)
{
SaveLanguage("En");
}
protected void ImgFr_CLick(object sender, EventArgs e)
{
SaveLanguage("fr-Fr");
}
protected void SaveLanguage(string language)
{
Session[AppDefs.LANGUAGE] = language;
Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
}
public string GetLanguage()
{
if (Session[AppDefs.LANGUAGE] != null)
{
return Session[AppDefs.LANGUAGE].ToString();
}
else
{
return "en";
}
}
In the master page I open login page and then other pages. How can I change UICulture for all pages when I choose from master page ?
I try in login page on preinit event this, but doesn't work:
protected void Page_PreInit(object sender, EventArgs e)
{
UICulture = Master.GetLanguage();//"fr-FR"
}
Thanks
Storing the culture name in a session variable is a good start, but you have to change the threading cultures in CurrentThread for each request. Try the following code in PreInit:
var language = Master.GetLanguage();
Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);