Button notification

  • Thread starter Thread starter barry
  • Start date Start date
B

barry

Hi There

For some reason better known to me i create a button on the webpage
with the following code in Page_Load method

Response.Write("<Input Type='Button' Value='Click Me!'>");

When clicked on the above button i need the following code to be
notified

private void OnBtnClick()
{

}

How do i achieve this.

TIA
Barry
 
Hi,

It is better to do this way (unless you specific about dynamically creating
buttons):

In your .aspx file, have:

<asp:Button RunAt=server ID="btnOK" Text="Click Me!" />

In your .aspx.cs file, go to the Page_Init event and add:
btnOK.Click += new EventHandler(OnBtnClick);

Then add your OnBtnClick method as

void OnBtnClick (object sender, EventArgs e)
{
// Your code...
}

Hi There

For some reason better known to me i create a button on the webpage
with the following code in Page_Load method

Response.Write("<Input Type='Button' Value='Click Me!'>");

When clicked on the above button i need the following code to be
notified

private void OnBtnClick()
{

}

How do i achieve this.

TIA
Barry
 
If you really need dynamically created buttons, don't Response.Write them
out.

Instead, create an object and add it to a table cell or the page itself:

Button objButton = new Button();
objButton.Text = "Click Me!";
SomeTableCell.Controls.Add(objButton);
objButton.Click+=new System.EventHandler(this.Button_Click);

Now that you've added a handler, write the sub, Button_Click. You can get
the .ID of the button by converting the Sender to Button.

Button btnTemp = (Button)sender;
string strButtonId = btnTemp.Id;

HTH
S.M. Altaf
[MVP - VB]
 
you need to add a name tag or the browser will not post it.

Response.Write("<Input Type='Button' name='submit' Value='Click
Me!'>");

then in onload:

if (IsPostback && Request.Form["submit"] != null)
OnBtnClick();

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top