I want to know if I could hide/show an asp user control when pressing a button in another user control on the same master page?
The Visible property will do this for you. The Click handler on your Button would be something like:
protected void ToggleButton_click(object sender, EventArgs e)
{
TargetControl.Visible = !TargetControl.Visible;
}
if you have got 2 users controls in same page . you can use this code :
in usercontrol 1
<asp:button Text="hide" runat="server" ID="B1" OnClick="HideOtherUserControl" />
and in code-behind
protected void HideOtherUserControl(object sender, EventArgs e)
{
Parent.FindControl("WebUserControl1").Visible = false;
}
Related
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
}
}
I have a usercontrol.ascx in a aspx page. And in my user control I have a button click event, and I need to open another user control from this button click. Please give me some suggestions on how to achieve this.
Thanks.
You can use Visible property. Let's call the user control with the button ucControl1 and the other ucControl2.
// code-behind of ucControl1
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
ucControl2.Visible = false;
}
protected void btn_Click(object sender, EventArgs e)
{
ucControl2.Visible = true;
}
I have a news page in witch comments can be added. If i click a comment i go to a comment page where i can choose to delete the comment. If i delete the comment i want to go back to the news page and refresh the page so that the comments disapears.
I tryed adding this but then the remove function was not reached
<asp:Button runat="server" ID="btnRemoveComment" Text="Ta bort" OnClientClick="JavaScript: window.history.back(1); return false;" />
Commentpage.aspx
<asp:Button runat="server" ID="btnRemoveComment" />
Commentpage.aspx.cs
protected override void OnLoad(EventArgs e)
{
btnRemoveComment.Click += btnRemoveComment_Click;
}
private void btnRemoveComment_Click(object sender, EventArgs e)
{
CommentFactory.RemoveComment(CurrentPage);
}
a button can't apply two events, it's javascript or code-behind. Try to redirect in code-behind and use Ajax for dynamic update.
I don't think it is ok going back and forth between the news page and comments page. Try this
private void btnRemoveComment_Click(object sender, EventArgs e)
{
CommentFactory.RemoveComment(CurrentPage);
Response.Redirect("news.aspx");
}
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");
}
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";
}