I am not able to visible my button on another button click event.
.aspx
<asp:Button ID="btnActivate" runat="server" SkinID="skinLoginButton"
Text="Activate" ToolTip="Activate" CausesValidation="true"
ValidationGroup="UserAuthentication" onclick="btnActivate_Click" />
<asp:Button ID="btnhomepage" Visible="false" runat="server"
Text="Goto Homepage" CssClass="cssLoginButton" onclick="btnhomepage_Click"/>
.cs
#region btnActivate_Click
protected void btnActivate_Click(object sender, EventArgs e)
{
this.btnhomepage.Visible = true;
}
#endregion
I use this.btnhomepage.Visible = true; in .cs file.
what's wrong in my code or declearation?
<asp:Button ID="btnhomepage" Visible="false" runat="server"
Text="Goto Homepage" CssClass="cssLoginButton" onclick="btnhomepage_Click"/>
when using visible attribute in the mark-up you are forcing your control to be visible=false and stay false forever. asp.net engine render asp.net controls into html control in asp.net page life cycle at Render stage. even you had changed the control property in any code behind event
Solution: Don't use makup attribute when setting control behaviour dynamicllay
page life cycle link:
http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx
http://www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
Remove the visible property from the btnhomepage button and make it invisible from Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
this.btnhomepage.Visible = false;
}
}
Try this
btnhomepage.Visible = true;
btnhomepage.Enabled = true;
btnhomepage.Style.Add("display", "block");
Related
1st Page.
<asp:Button ID="Button1" runat="server" Text="Submit" CssClass="btn btn-primary" OnClick="Button1_Click" OnClientClick="return" />
Actually, I need to call this page from another 2nd.aspx.cs page.
public void Page_Load(object sender, EventArgs e){
//Do some stuff in the page load event handler.
}
public void Button1_Click(object sender, EventArgs e){
//Do some stuff in the button click event handler.
}
I change the button Access Modifiers property to public from the designer page.
public global::System.Web.UI.WebControls.Button Button1;
2nd Page
Create an Object in this page to access 1st page.
FirstPageName firstPage = new FirstPageName();
firstPage.Button1_Click();
This doesn't work!
1st Page.
FirstPageName.aspx page.
<asp:Button ID="Button1" runat="server" Text="Submit" CssClass="btn btn-primary" OnClick="Button1_Click" OnClientClick="return" />
FirstPageName.aspx.cs page.
public void Button1_Click(object sender, EventArgs e){
//Do some stuff in the button click event handler.
}
Change the button Access Modifiers property to public from the designer page.
public global::System.Web.UI.WebControls.Button Button1;
2nd Page
Here, we can use 2 methods.
1st Method
Create an Object in this page to access 1st page.
FirstPageName firstPage = new FirstPageName();
firstPage.Button1_Click(firstPage.Button1, EventArgs.Empty);
2nd Method
Create an Object in this page to access 1st page.
FirstPageName firstPage = new FirstPageName();
firstPage.Button1.Click += new EventHandler(firstPage.Button1_Click);
I'm also attaching another two methods. Try this, This works for me.
YourButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Button.PerformClick();
i have 3 pages
Login
Home
Add employeeNow i've a back button in "Add Employee" page which should redirect to the previous page! but when i click on it the required field validators will turn on!! how can i avoid that for back button? I tried using two types of code but both gets stuck in there for validation.
protected void Button2_Click(object sender, System.EventArgs e)
{
string prevPage = Request.UrlReferrer.ToString();
Response.Redirect(prevPage);
}
And this
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("home.aspx");
}
On aspx Page you need to set CausesValidation = false for back button. See below
<asp:Button ID="btnBack" runat="server" CausesValidation="false" Text="Back" />
By Adding CausesValidation = false. The Validation event will not fire for that button click and your redirect will work properly.
Hope that helps
CODE BEHIND:
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
ddlLanguage.SelectedValue = Thread.CurrentThread.CurrentCulture.Name;
}
}
protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlLanguage.SelectedValue == "es-ES")
{
mdlPopup.Show();
}
//Sets the cookie that is to be used by Global.asax
HttpCookie cookie = new HttpCookie("CultureInfo");
cookie.Value = ddlLanguage.SelectedValue;
Response.Cookies.Add(cookie);
//Set the culture and reload the page for immediate effect.
//Future effects are handled by Global.asax
Thread.CurrentThread.CurrentCulture = new CultureInfo(ddlLanguage.SelectedValue);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(ddlLanguage.SelectedValue);
//Server.Transfer(Request.Path);
}
protected void OKButton_Click(object sender, EventArgs e)
{
Server.Transfer(Request.Path);
}
ASPX PAGE:
<asp:DropDownList ID="ddlLanguage" class="langpnl" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlLanguage_SelectedIndexChanged">
<asp:ListItem Value="en-US">Eng</asp:ListItem>
<asp:ListItem Value="es-ES">Esp</asp:ListItem>
</asp:DropDownList>
<ajaxToolkit:ModalPopupExtender ID="mdlPopup" runat="server" TargetControlID="testhidden"
PopupControlID="pnlPopup" OkControlID="OKButton" />
<asp:Panel ID="pnlPopup" runat="server" Width="500px" Style="display: none">
All content may not be in Spanish.
<asp:Button ID="OKButton" runat="server" Text="OK" OnClick="OKButton_Click" />
</asp:Panel>
<asp:HiddenField ID="testhidden" runat="server" />
I am trying to set the language as per the selection in the Dropdown box. But if the user selects spanish I want to display a popup modal with a msg & once the button OK is pressed I want to postback the whole page.
Currently I am able to display the popup but the page never refreshed so the language still doesn't change. In the code behind if I remove the server.transfer from the OK button and put it in the SelectIndexChange then the page postback is working but there is no popup masg .I think the page gets postback after the popup executes so it never gets displayed...please need some help I am breaking my head since last 3 days.
Define another button in that panel... and do whatever you want in his onclick event. So you will have a postback.
The OKButton click event... OKButton_Click will not fire as long as you assigned him in you modalpopup...
if (ddlLanguage.SelectedValue == "es-ES")
{
mdlPopup.Show();
}
else
{
Server.Transfer(Request.Path);
}
& removed the OK button from Modalpopup..finally got to see what I was Expecting..
I have textbox and button controls in my page. For textbox I have enabled view state, page-load event I am setting text box value “Hello Mr!”. Now I want to change the view state value for text box to “Hello Mr Pradeep!” when post back occurs, how can I do that? And in which all page events I can do that.
<asp:TextBox ID="TextBox1" runat="server" EnableViewState= "true"/>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "Hello Mr!";
}
Thanks,
Pradeep
As far as i understand your question you can do this using javascript on page load event
Your page is retrieving viewstate between the init of the page and just before the page load event. So the more early attempt you can make to change the view State is in the page load.
Because if you were to modify it before its retrieving your changes would be lost.
protected override void OnInit(EventArgs e)
{
if (IsPostBack)
{
//on postback ViewSate["test"] is null
ViewState["test"] = "Valuepostback";
//Now ViewSate["test"] is Valuepostback
}
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
if (IsPostBack)
{
//on postback ViewState has been reloaded from the page sent and therefore the initial value set in the oninit does not exists anymore
//ViewState["test"] is MyValue
//if you want to cahnge specifically the view state do it here
}
if (!IsPostBack)
ViewState["test"] = "MyValue";
base.OnLoad(e);
}
I have the following code in my aspx page:
<asp:Button id="display_button" runat="server" Text="Display" OnClick="Button1_Click" />
<asp:Button id="edit_button" runat="server" Text="Edit" OnClick="Button2_Click" />
<asp:Button id="save_button" runat="server" Text="Save" OnClick="Button3_Click" Visible="false" />
<asp:MultiView id="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View id="View1" runat="server">
<asp:FormView id="view_program" runat="server">
<ItemTemplate>
<%# Eval("status").ToString().Trim() %>
</ItemTemplate>
</asp:FormView>
</asp:View>
<asp:View id="View2" runat="server">
<asp:FormView id="edit_program" runat="server">
<ItemTemplate>
<asp:DropDownList id="p_status" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:FormView>
</asp:View>
</asp:MultiView>
and the following functions attached to the buttons in the code-behind page:
protected void Button1_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(View1);
save_button.Visible = false;
}
protected void Button2_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(View2);
save_button.Visible = true;
}
protected void Button3_Click(object sender, EventArgs e)
{
DropDownList p_status = edit_program.FindControl("p_status") as DropDownList;
var status = p_status.SelectedValue;
Label1.Text = status;
//save_button.Visible = false;
//MultiView1.SetActiveView(View1);
}
The idea being, that there are two views, the first displays the information, if the user wants to edit the information, they click button 2 which changes the view to the edit mode, which has the controls (drop downs, text fields, etc). It also makes the 'save' button appear.
What I am trying to make happen is, when the save button is clicked, it will grab all of the values from the various fields, update the object and then update the database. Then it would flip back to view1 with the updated info.
Problem is, as you can see in void Button3_Click, I try grab the values from the control, p_status, but it only gets the original value. example, the menu has three values, 'Green', 'Yellow', and 'Red'. Green is the default value and is selected when view2 is displayed. However, if I select Yellow or Red, and click save, rather than the label being updated to display one of those two values, it always displays Green.
Any ideas?
edit: page load function per request below
protected void Page_Load(object sender, EventArgs e)
{
try
{
Person myPerson = new Person(userid);
TestProgram myProgram = new TestProgram(id);
List<TestProgram> program = new List<TestProgram> { myProgram };
view_program.DataSource = program;
view_program.DataBind();
edit_program.DataSource = program;
edit_program.DataBind();
DropDownList p_status = edit_program.FindControl("p_status") as DropDownList;
p_status.Items.Add(new ListItem("Green", "Green"));
p_status.Items.Add(new ListItem("Yellow", "Yellow"));
p_status.Items.Add(new ListItem("Red", "Red"));
//myProgram.Status = "Red";
p_status.SelectedValue = myProgram.Status;
}
catch (Exception ex)
{
Response.Write(ex);
Label1.Text = ex.ToString();
}
}
Whoops...missed a little someting.. my
bad
when asp.net is not behaving as expected this is your best friend: MSDN: ASP.NET PAGE LIFECYLE
Upon Further Review...
there are a couple of problems here. your drop down list control with an id of "p_status" is contained inside a multiview (I forgot about what that meant...) you need to move the code to populate p_status into pre-render after checking to see if Multiveiw1.ActiveView = View2. Since it will always be a post back you need to bind values late in the page cycle