Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 13th, 2005, 10:39 AM
Authorized User
 
Join Date: Jan 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Input button click event not working

I am unable to find information about how to put a server side HtmlButton into a form and have it use its defined click event.

How do I get this to work?
void save_click(Object sender, EventArgs e){
  workview.InnerText=Request.Form["page_name"].ToString();
}

C# v.2003 - code below.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace testbed{
public class form : System.Web.UI.Page{

    protected System.Web.UI.HtmlControls.HtmlTableCell workview;
    protected System.Web.UI.HtmlControls.HtmlTableCell page_control;

    if(Page.IsPostBack){
     if(Request.Form["page_name"]!=null){
        workview.InnerText=Request.Form["page_name"].ToString();
     }
    }

    private void Page_Load(object sender, System.EventArgs e){
     HtmlButton page_new=new HtmlButton();
     page_new.InnerText="New";
     page_new.ServerClick+=new System.EventHandler(this.page_new_click);
     page_control.Controls.Add(page_new);
    }

    void page_new_click(Object sender, EventArgs e){
     HtmlInputButton save=new HtmlInputButton();
     HtmlInputText page_name = new HtmlInputText();
     page_name.ID="page_name";
     save.Value="Save";
     save.ServerClick+=new System.EventHandler(this.save_click);
     workview.Controls.Add(save);
     workview.Controls.Add(page_name);
    }

    void save_click(Object sender, EventArgs e){
        
    }
}
}
 
Old August 13th, 2005, 11:45 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I think the problem with this code is that when the second button has been created and clicked, the page posts back but doesn't recreate the required control structure.
So, after you clicked the Save button, Page_Load will fire and the New button is recreated. However, you don't recreate the Save button so its Click event will never fire.
You'll need to recreate both buttons.....

Why are you creating so many buttons dynamically? Is there a reason you can't use static buttons that you hide or show depending on the phase the page is in??

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old August 13th, 2005, 12:09 PM
Authorized User
 
Join Date: Jan 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I would like to do as much as possible in code so I can keep the html page small. But your clarification has given me a route I haven't tried yet.

 
Old August 13th, 2005, 12:20 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

If you hide the button with server side code (e.g. myAspButton.Visible = false;) then its Html is *not* sent to the browser, so it doesn't add to the size of the page.

You can use asp:Panel or asp:PlaceHolder controls to hide or show entire portions of the page. Again, their contents are not sent to the browser.

If you try to keep the total size down, I don't see much point in moving the code from the ASPX portion of the page to the Code Behind. It's likely you need more code-behind code to create all this stuff yourself than it takes to do this in the markup....

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Button Click Event via XML,XSLT,javascript rhd110 XML 2 June 20th, 2007 08:16 AM
Link button click event sarah lee ASP.NET 2.0 Basics 5 May 18th, 2007 09:29 AM
click event for vb YesNo button mohiddin52 Access VBA 2 January 14th, 2007 02:25 AM
Duplicate Control Creation on button click event kapi.goel C# 1 December 21st, 2005 02:42 PM
Trigger event on button click speedyH ASP.NET 1.x and 2.0 Application Design 3 June 27th, 2004 10:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.