onclick second click - c#

New to asp.net, I am having a problem on a website I am creating, I am using a master page to build my pages. I am trying to change the css class of a li tag using the onclick event in linkbuttons:
<asp:LinkButton runat="server" id="AboutButton" OnClick="about_click" PostBackUrl="about.aspx"><span>About</span></asp:LinkButton>
This linkbutton calls a function in the master page's code behind:
protected void about_click(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
about.Attributes.Add`enter code here`("class", "current");
}
}
This only works when the page is loaded and the button is clicked again. Any help would be greatly appreciated.

By adding: if(Page.IsPostBack) you're specifically telling it not to execute that code the first time the page is loaded, but you want it to happen when the page is first loaded, by the sounds of the question.

Why did you add if(Page.IsPostBack). Try this
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
about.Attributes.Add("class", "current"); //initial setting here
}
}
protected void about_click(object sender, EventArgs e)
{
about.Attributes.Add("class", "current");
}

Related

ViewState value not being retained during postback

How can I make this piece of code work? I am dealing with a bigger issue but if I can make this work then I will know what to do.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Response.Write(ViewState["Value"].ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Value"] = "Button clicked";
}
Page_Load event happens before Button1_Click and hence you wont be able to access a value that is not already set.
You will need to use an event that happens after Button1_Click like Page_PreRender as you have used in the answer.
Please go through this link to understand Page Life Cycle, which is invaluable in Asp.Net Webforms development.
I was able to solve my problem by putting my logic in pageLoad method in the page_PreRender method like this:
protected void Page_PreRender(object sender,EventArgs e)
{
if (IsPostBack)
{
Response.Write(ViewState["Value"].ToString());
}
}

ASP.NET C# why cant I change the text in a textbox in a webpage

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.

Get Set values of TextBox Control from server side

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

Buttonclick and postback issue

Below is a code wherein I want to display a message if there is a postback.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
Response.Write("Hidden value :" + HiddenField1.Value);
}
}
protected void btn_Click(object sender, EventArgs e)
{
HiddenField1.Value = "test";
}
The issue is I get the message on clicking the button twice, that is clearly because Page_Load happens before btn_Click. Can anyone suggest me a way wherein I can get the message to be displayed in if (Page.IsPostBack) on page postback?
Kindly help
Yep, you're running into a page life cycle issue. Page_Load happens before any postback events.
If you're just doing Response.Write to the resposne stream, you could move this code to the overriden PreRender event on the page.
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (Page.IsPostBack)
{
Response.Write("Hidden value :" + HiddenField1.Value);
}
}

to click link button it will change the label description using asp.net

I am new to ASP.NET and C# and have one doubt.
I have one link button in master page.
When I click that button it will change the label description in the content page.
Can anyone help, it's very useful for me?
Thank you
on Link Button click event of master page you can find control of your content page using below mentioned code.
protected void lnk_Click(object sender, EventArgs e)
{
Label lbl = (Label)(this.ContentPlaceHolder1.FindControl("lbl"));
lbl.Text = "Test";
}
You find what your looking for here http://www.snippetbank.net/detail/snippetdetail/9-aspcsharp/8-miscellaneous/556-Update-Content-Label-Text-From-Master-Page.html
For this follow these steps:
Add an event in the master page class using below code:
public event EventHandler MasteridButton_Click;
Write this code on button click:
public void idButton_Click(object sender, EventArgs e)
{
if (MasteridButton_Click != null)
{
MasteridButton_Click(sender, e);
}
}
Add this page directive in the page which you want to get this event
<%# MasterType VirtualPath="~/[YourMasterPageName].Master" %>
Add this code to your page class
protected void Page_Init(object sender, EventArgs e)
{
this.Master.MasteridButton_Click +=new EventHandler(Master_MasteridButton_Click);
}
protected void Master_MasteridButton_Click(object sender, EventArgs e)
{
this.lblMyLable.Text = "My Text";
}

Categories