how single onclick event of button can perform 2 operations - c#

How can i make a textbox visible and call a function from external javascript simultaneously,
by clicking one single button(on single onclick event)?
right now m using this ,but getting the value of textbox on double click.
protected void BtnCalculate_Click(object sender, EventArgs e)
{
LblRisk.Visible = true;
TxtRisk.Visible = true;
BtnCalculate.Attributes.Add("OnClick", "Javascript:CalculateRisk();");
}
the value of textbox (whose id is 'TxtRisk') comes from the function 'CalculateRisk()'

You are attaching javascript event when server side event for button is triggered. You will get he javascript event after first click. Attach the javascript event before button click could be in page_load event.
void My_Page_Load(object sender, EventArgs e)
{
BtnCalculate.Attributes.Add("OnClick", "Javascript:CalculateRisk();");
}
protected void BtnCalculate_Click(object sender, EventArgs e)
{
LblRisk.Visible = true;
TxtRisk.Visible = true;
}

have you tried
<asp:Button id="asd" runat="server" onClick="BtnCalculate_Click" onClientClick="CalculateRisk()">

Related

linkbuttons behaviour postbacks versus not postback

Can you help me understand why, when the page is first loaded by the example below, the buttons don't work as intended eg Button 2 doesn't call GetItems(int.Parse("2"), 3); but rather calls GetItems(int.Parse("4"), 3); however after the first postback all the buttons work correctly e.g. Buttonx calls GetItems(int.Parse("x"), 3);
Thanks
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
GetItems(1, 2); //default values (first time the page is loaded)
}
GenerateButtons(5);
}
private void GenerateButtons(int c)
{
LinkButton[] x = new LinkButton[c];
for(int i=0; i<c;i++)
{
x[i] = new LinkButton();
x[i].Text = (i+1).ToString();
Panel1.Controls.Add(x[i]);
x[i].OnClick += new EventHandler(Button_Click);
}
}
protected void Button_Click(object sender, EventArgs e)
{
Button button = (Button)sender; // Which button was clicked;
GetItems(int.Parse(button.Text), 3); //3 is a constant; first argument is index of button extracted from its caption
}
PS. when I refer to button 1 I have button[0] in mind. button2=button[1] and so on. after postback button1 is correctly attached to the event to trigger GetItems(1,3). Before the postback button1 causes GetItems(3,3) to run. Not as intended
Use the Page_Init so it will work on the first load
protected void Page_Init(object sender, EventArgs e)
{
GenerateButtons(5);
}
Source: https://msdn.microsoft.com/en-us/library/ms178472.aspx
Init Raised after all controls have been initialized and any skin
settings have been applied. The Init event of individual controls
occurs before the Init event of the page. Use this event to read or
initialize control properties
.
When you dynamically create controls you do so in Page_PreInit not Page_Load
protected void Page_PreInit(object sender, EventArgs e)
{
GenerateButtons(5);
}
This article explains and will help you
http://www.robertsindall.co.uk/blog/dynamically-adding-web-controls/

asp.net event handler for dynamicallycreated button

I have a button created in code behind like so:
some method {
Button btnExportToExcel = new Button();
btnExportToExcel.Text = "Export To Excel";
btnExportToExcel.Click += new EventHandler(btnExportToExcel_Click);
pnlListView.Controls.Add(btnExportToExcel);
}
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
//do something
}
The problem is that I can't ever get to execute the code from the event method.
Why is that?
are you adding controls to page in preinit event handler? Check
You must add the button to any controler .
protected void Page_Load(object sender, EventArgs e)
{
Button btnExportToExcel = new Button();
btnExportToExcel.Text = "Export To Excel";
btnExportToExcel.Click += new EventHandler(btnExportToExcel_Click);
//this is add the button to the form1
this.form1.Controls.Add(btnExportToExcel);
}
void btnExportToExcel_Click(object sender, EventArgs e)
{
//...
Response.Write("click me...");
}
Please add the dynamic controls in the Page's Init event handler so that the ViewState and Events are triggered appropriately.

Running Page Load multiple times

I have a user control on an aspx page that contains filter fields and return a WhereClause. The user conrol will raise an event called FilterApplied when Search is clicked.
On my main form, I add the control with:
<uc1:Filter runat="server" ID="Filter" />
<br />
In my code behind I have:
protected void Page_Load(object sender, EventArgs e)
{
//Register event when filter is changed.
this.Filter.FilterApplied += new EventHandler(this.FilterApplied);
if (Page.IsPostBack)
{ //Do some things that take long
}
}
protected void FilterApplied(object sender, EventArgs e)
{
//Reload the page to refresh the graphs.
Page_Load(sender, e);
}
Problem is:
When I click Search on my user control, the Form_Load runs twice. Once because it is reloaded and then another time because I call it from FilterApplied. If I don't call it from FilterApplied, then the whereclause is still empty.
How can I ensure the Page_Load only run once when Search is clicked?
Your problem lays in multiple registering for FilterApplied event. Each time you call the Page_Load method, you subscribe to this event again. Here is a really simple example of what you are doing, written in WinForms with one button on the form, just to point out your problem:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int numClicks = 0;
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += button1_Click;
this.Text = numClicks.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
numClicks++;
//try uncommenting and commenting next line of code, and observe the difference:
//button1.Click -= button1_Click;
Form1_Load(sender, e);
}
}

Can't call Click Event on dynamic button

I want to create a method in code behind that creates a button and places it in a PlaceHolder. I want this button to have a Click event.
After calling the "test" method button is placed correctly but the click event is not called.
private void test()
{
Button linkBtn1 = new Button();
linkBtn1.Text = "linkBtn1";
linkBtn1.OnClientClick = "return false;";
linkBtn1.Click += new EventHandler(linkBtn1_Click);
PagesPlaceHolder.Controls.Add(linkBtn1);
}
void linkBtn1_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
Removing the OnClientClick = "return false;" is necessary but not sufficient to get this to work. If you want the event handler on the dynamically added button to be triggered, you'll need to add this button every time the page loads.
One simple way would be to save the fact that the button has been added in ViewState, and then check that on PageLoad, and re-add the button if needed.
Here's a sample that works for me (and throws the exception when clicked)
protected void Page_Load(object sender, EventArgs e)
{
//if the button was added previously, add it again
if (ViewState["Added"] != null && (bool)ViewState["Added"])
addButton();
}
//this is the method that adds the button
protected void add_Click(object sender, EventArgs e) {
ViewState["Added"] = true;
addButton();
}
private void addButton() {
Button linkBtn1 = new Button();
linkBtn1.Text = "linkBtn1";
linkBtn1.Click += new EventHandler(linkBtn1_Click);
placeholder1.Controls.Add(linkBtn1);
}
void linkBtn1_Click(object sender, EventArgs e) {
throw new Exception("Button Click Event Triggered. Hello yellow screen!!!");
}
As #oleksii points out, have the clientside code is returning false so the form never gets submitted. You need to comment this out, then your event handler should fire.

How to add EventHandle _NOT_ at Page_Load

I'm making dynamic table at Page_LoadComplete, I cant do in at Page_Load becouse data can be changed during events process, so at Page_LoadComplete i make some buttons and want add them EventHandler:
protected void Page_LoadComplete(object sender, EventArgs e) {
btn.Click += new EventHandler(b_Click);
}
But it doesnt work, how to add events to button not at Page_Load?
simple, even not dynamic code:
void b_Click(object sender, EventArgs e) {
Label1.Text = "!!!";
}
protected void Page_Load(object sender, EventArgs e) {
Button1.Click += new EventHandler(b_Click);
}
on Page_Load works fine.
protected void Page_LoadComplete(object sender, EventArgs e) {
Button1.Click += new EventHandler(b_Click);
}
on Page_LoadComplete do nothing.
Ok here's the example:
Given this html snippet:
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat="server" Text="Click Me" OnClick="go_" />
</div>
</form>
</body>
public partial class WebForm1 : System.Web.UI.Page
{
public int ControlCount
{
get { return ViewState["Controls"] == null ? 0 : (int)ViewState["Controls"]; }
set { ViewState.Add("Controls", value); }
}
protected void Page_Load(object sender, EventArgs e)
{
for(int i = 0; i < ControlCount; i++)
{
Button b = new Button();
b.Click += btn_Click;
b.Text = "Hi";
form1.Controls.Add(b);
}
}
void btn_Click(object sender, EventArgs e)
{
((Button)sender).Text = "bye";
}
protected void go_(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "Hi";
form1.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
ControlCount++;
}
}
Every time you click the first button a new button will be added to the page with the text "Hi", and every time you click a "Hi" button the text for THAT button only will change to "Bye"
This works because I add the controls twice. Once in the event handler of the main button, where I determine that I need a new control, and then AGAIN in OnLoad where I have a new, empty page again, but now I know (via ControlCount) how many controls I added.
The problem is the control doesn't exist on postback (since you created it at run time). Creating the control in Init should solve this, and will allow the event to fire. You can change all of the properties in LoadComplete if you like, but the button needs to exist at that point.
What you're trying will never work because controls that are added dynamically must be REadded every time the page reloads.
Since your control population is based on the data being used in the form, you should put all your control creation logic in a single function, and in your event handlers, maniuplate the data then call your control creation logic function, while in Page Load you would JUST call the control creation function (presuming that you've already persisted the data between calls) so that all the controls that were created the first go around get recreated. Then, once they have been recreated, they can respond to the events that they fired.

Categories