How to write event handler for dynamic control? - c#

Actually i am dynamicaly creating two textboxes and two buttons inside a table using for loop. Now i want to write event handlers for these 2 buttons so that upon clicking on the button the text inside the respective texbox should be displayed in a new label. Also tell me why upon clicking the button after postback all the dynamic controls disappear. Kindly explain with some good example.

protected void Page_Load(object sender, EventArgs e)
{
.
.
Button Button1= new Button();
Button1.ID = "button1";
Button1.Text = "Button";
Button1.Click+=new EventHandler(Button1_Click);
this.form1.Controls.Add(Button1);
.
.
}
and handler method goes like this
protected void Button1_Click(object sender, EventArgs e)
{
//
}
The reason why after postback all the buttons disappear is, these controls are not created again in Page_Load event.
These controls were not in page markup initially, and while postback, dynamically created markups will not be retained due to stateless transfer
Go through ASP.NET Page life cycle for more information

Related

How to change text box text by click on button

Hello I am beginner in asp.net webforms and i want to copy text of textbox in another textbox when i click in button
my code is :
protected void Button2_Click(object sender, EventArgs e)
{
TextBox15.Text = TextBox6.Text;
}
when i fill TextBox6 and click button nothing show in TextBox15
Try to use IsPostBack property in the pade_load event handler to prevent re-validation of the page every time you click the button.
if (!IsPostBack)
{
//do nothing
}
By the way code of your button click will remain as it is, dont change anything of it, just write the above code on the page load.
I hope it helps

ASP.NET Click event does not work for multi buttons what created in tab button control

I am new in ASP.NET and I have a problem..
In my code, btn.Click event does not work for multi buttons what created in tab button control. Can you guys advice me?
Thanks,
<asp:Button Text="TAB MENU" BorderStyle="None" ID="Tab" CssClass="Initial" runat="server" OnClick="Tab_Click" />
<asp:Panel ID="panel1" runat="server" Direction="LeftToRight" HorizontalAlign="Left"></asp:Panel>
protected void Tab_Click(object sender, EventArgs e) {
foreach (...) {
Button btn = new Button();
btn.Click += Button1_Click;
panel1.Controls.Add(btn);
}
}
protected void Button1_Click(object sender, EventArgs e) {
**some code here! but does not work.**
}
Try this
Button btn = new Button();
btn.Click += new RoutedEventHandler(Button1_Click);
panel1.Controls.Add(btn);
Your problem probably lies along the lines of the fact that the button does not exist when the Button1_Click callback is fired... Asp.net webforms is a somewhat leaky abstraction over HTTP, and therefore it's a bit tough to decipher sometimes, but I expect what is happening is that when the postback from your button click occurs, the button doesn't actually EXIST in the control tree, because that button only gets added to the control tree when a tab control is clicked. So because the button does not exist on the postback, webforms doesn't know what to do with the event, so it ignores it...
If your buttons must be created dynamically, consider making them do some javascript to edit some hidden field or something, the value of which you can inspect in the Page_Load method, and then do whatever you want to do functionality-wise with that value.
If they are NOT dynamic, and instead will be the same for each page load (but different for each tab - ie, when the page is loaded, you're drawing some info from a db to decide what buttons to display, but that will not change from postback to postback of the same page), then consider creating the buttons in the Page_Load event instead of inside the event that is raised when the tab is created.
ALTERNATIVELY, since in your comment you suggest that the buttons will be used to open a new window, why not just make the buttons do that client side, ie with javascript window.open commands. Then you don't even need to postback to the server at all...

Allocating and deallocating event on a button

I am busy having a problem with allocating a new event to an existing button that was created in the designer.
Now here is the button that was created prior to runtime which is inside the modalpopupexteder5 -
<asp:Button runat="server" ID="btnClose" Text="Close" OnClick="btnClose_Click" />
Here is the codebehind -
protected void btnAddAccount_Click(object sender, EventArgs e)
{
btnClose.Click -= new EventHandler(btnClose_Click);
btnClose.Click += new EventHandler(btnCancel_Click);
ModalPopupExtender5.Show();
}
protected void btnCancel_Click(object sender, EventArgs e)
{
ModalPopupExtender11.Hide();
}
protected void btnClose_Click(object sender, EventArgs e)
{
ModalPopupExtender5.Hide();
}
So the button in the beginning has the event btnClose_Click hooked up to it. But I want to change it to the btnCancel_Click
But it wont execute the btnCancel_Click. It executes the original btnClose_Click
Any Ideas what could of caused this?
Does this relate to the page life cycle?
--EDIT--
I should let you know that the btnAddAccount_Click does get executed.
Basically I don't to create the same modalpopupextender, I want to use the existing one but depending on user selection will determine which eventhandler should be called and in this case the btnAccount_Click has selected the btnCancel_Click to be assigned to the button.
ASP.NET is not assigning the Click events anywhere for the server side buttons. Those buttons are rendered as plain submit buttons, and the internal code behind is checking the submitted value then based on that it finds the "clicked" button and calls the proper handler.
This means your current logic is leading to a dead end.
Instead of messing with the events, have btnClose_Click as the handler, and in there check the currently active/visible popup and hide it.
I am not quite sure what exactly you are asking, but you can add a Handles to your event and so control the events on your buttons.
Something like:
protected void btnCancel_Click(object sender, EventArgs e) Handles btnCancel.Click, btnClose.Click
{
//do stuff
}

Creating an event for a textbox and AJAX Update Panel Control

I wanted to create a "Click" event for a textbox in C# (as there isn't any).
So, this way
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "txt1OnClick")
{
txt1_Click();
}
txt1.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(txt1, "txt1OnClick"));
}
private void txt1_Click()
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
Then I wanted to load the image without reloading the page.
So I used the AJAX UpdatePanel Control and this worked fine with
protected void Button1_Click(object sender, EventArgs e)
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
But not with the event I created, because the compiler doesn't identify my new events as
a real event or something I couldn't figure out.
I added the button1_click event according to Step 8 of "Refreshing an UpdatePanel Control with an External Button".
The click event of textbox is not shown in this option:
So my question is is there any way to add this event within System.Web.UI.WebControls.TextBox class or, to make this event visible within the above option?
So that I can include click event of the textbox within the Triggers of the update panel.
If you try to create a Click event for a TextBox, every time a user clicks your textbox you'll trigger a postback to the server (even to evaluate if you need to do something as part of handling the event). This is very inefficient - you should handle clicks in the browser, using JavaScript and then trigger the UpdatePanel using client-side logic.
This lets you trigger a call to the server if you need it but avoid it when you don't. If you have a server-side event handler, your code will post back to the server (reloading the page) every time the user clicks the TextBox.
You can read this link (and others) about using __doPostBack() on the client side to trigger an UpdatePanel to perform a postback.
http://encosia.com/easily-refresh-an-updatepanel-using-javascript/

Gridview Rowcommand event firing but show hide controls including Gridview not happening

Here is what I am trying to do in two simple steps:
1) New Row (trNewPost) which has table inside and controls in it to add new post or to update existing post.
Default Visible=false;
2) Add Button to make above row visible = true;
3) trMyPosts has Gridview in it and displays all the posts.
Default visible = true.
When user click on editing any row of the gridview (RowCommand event) I just want to hide this grid (trMyPosts) and show trNewPost.
That's all. events firing, but nothing happening.
I think you've got viewstate problem.
One of the things you can do is this :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// do things here
}
}
Because whenever anything happens, the page posts back. By encapsulating your Page_Load with ! Page.IsPostBack, you prevent those things from happening over and over again.
Now, if your variable is a global variable, you will have this same problem. Consider instead using a Session variable.
Also, I just wanted to show you this piece of code just in case :
protected void HideShowClicked(object sender, EventArgs e)
{
// toggle the visibility of the control
// (that is, if visible then hide, if hidden then show)
myControl.Visible = ! myControl.Visible;
}

Categories

Resources