Notices

Great community. Great ideas.

Welcome to ASP/Free, a community dedicated to helping beginners and professionals alike in improving their knowledge of Microsoft's development and administration technologies. Sign up today to gain access to the combined insight of tens of thousands of members.

Thread: Dynamic Buttons and Click event in C#

Results 1 to 9 of 9
Share This Thread →
  1. #1
    LiveWire_530 is offline Registered User
    Join Date
    Dec 2003
    Location
    NY
    Posts
    6
    Rep Power
    0

    Dynamic Buttons and Click event in C#

    I am creating an aspx page using C# and would like to be able to dynamically create buttons that all run the same fuction on the click event. However, I would like the function to accept a single argument (in this case i) to identify which button was clicked.

    Is there an easy way to do this?

    // ON PAGE LOAD
    for (int i=0;i < 4;i++)
    {
    TableRow row = new TableRow();
    TableCell cell = new TableCell();
    cell.Controls.Add(new LiteralControl("cell -" + i ));
    LinkButton btn = new LinkButton();
    btn.Text="Button " + i;
    /*Would like to pass value of i as argument to myFunction*/
    btn.Click += new System.EventHandler(this.myFunction);
    cell.Controls.Add(btn);
    row.Cells.Add(cell);
    Table1.Rows.Add(row);
    }


    //I would like a function like this
    //myFunction
    void myFunction(int ID)
    {
    Response.Write(ID.ToString());
    }

    //However I know you need the following
    void myFunction(object sender, System.EventArgs e)
    Last edited by LiveWire_530; February 20th, 2004 at 09:43 AM.
    Faq

  2. #2
    LiveWire_530 is offline Registered User
    Join Date
    Dec 2003
    Location
    NY
    Posts
    6
    Rep Power
    0

    Solution

    Well I figured out a way to do this incas eanyone is interested
    I used the Command event handler,
    however I am still unclear on what the CommandName is for, because I can remove that line of code and it will still function proeperly??? Any ideas?


    private void Page_Load(object sender, System.EventArgs e)
    {
    for (int i=0;i < 4;i++)
    {
    TableRow row = new TableRow();
    TableCell cell = new TableCell();
    cell.Controls.Add(new LiteralControl("cell -" + i ));
    LinkButton btn = new LinkButton();
    btn.Text="Button " + i;
    /*Would like to pass value of i as argument to myFunction*/
    btn.CommandName = "ID";
    btn.CommandArgument = i.ToString() ;
    btn.Command += new CommandEventHandler(myFunction);
    cell.Controls.Add(btn);
    row.Cells.Add(cell);
    Table1.Rows.Add(row);
    }
    }

    //myFunction
    void myFunction(object sender, CommandEventArgs e)
    {
    int RecordId = Int32.Parse(e.CommandArgument.ToString());
    Response.Write(RecordId.ToString());
    }
    Faq

  3. #3
    papp is offline Registered User
    Join Date
    Feb 2004
    Posts
    2
    Rep Power
    0
    I have a very similar problem, but I want to be able to dynamically create buttons in a table which appears on a page after several other forms have already been completed, hence I can't add my for loop for creation of the buttons in the Page_Load method, since I need input from the user before I can create the buttons. What needs to be changed to be able to move this code outside of Page_Load?
    Faq

  4. #4
    LiveWire_530 is offline Registered User
    Join Date
    Dec 2003
    Location
    NY
    Posts
    6
    Rep Power
    0

    Dynamic Controls

    papp,

    Not sure what you mean.
    If you want to place them somewhere specific in the layout use a webform table with a place holder

    Table myTable = new Table();

    Then add rows and cells to those rows...
    Inside a cell you can add the button or any other control..

    Control.Add(myButton);

    If you want it in another function beside page load, just create a new function and call that function when you need to

    Hope this helps
    Last edited by LiveWire_530; February 25th, 2004 at 07:09 PM.
    Faq

  5. #5
    papp is offline Registered User
    Join Date
    Feb 2004
    Posts
    2
    Rep Power
    0
    In page_load, my code make calls to different based on user input. In these methods I want to be able to create various buttons dynamically and give the use the ability to click on them, which would call specific code that would depend on the button that was clicked. Basically, I want to have do the same thing in the above example, but I can't have the code to create the buttons in the page load function.
    Faq

  6. #6
    hitmanharkness is offline Registered User
    Join Date
    Jun 2004
    Posts
    1
    Rep Power
    0
    Heres something I was working on in the same lines:
    I was trying to dynamically created buttons in a table using a for loop but couldnt get the event handler to do its job


    //1) Initial page request called Page_Load. My function BindStudentList() is called from this and dynamically creates the buttons within the table within a for loop and displays the buttons and table containing them

    Button btnUpdateStudent = new Button();
    btnUpdateStudent.ID = "Update" + listindex;
    btnUpdateStudent.Text="Update";
    btnUpdateStudent.Width=55;
    btnUpdateStudent.DataBind();
    btnUpdateStudent.Click +=
    new EventHandler(this.btnUpdateStudent_Click);

    /*c2 being a cell of the table*/
    c2.Controls.Add(btnUpdateStudent);


    //2) Update button is clicked


    //3) Page is reposted calling Page_Load again. If BindStudentList() is not called because it is within the if(!Page.IsPostBack) the button is not recreated and therefore the page cannot find the event handler

    //4) Something is looking for the the Update button again and its event handler to call and either finds it or not but these dynamic controls do not seem to be reposted

    If I put the BindStudentList() function outside of the if(!Page.IsPostBack) so it is called every time the page loads the button is created every time and therefore finding its event handler. Worky's

    When that all works finding the specific button (because I created a ton in a for loop) in the event is nothing

    void btnUpdateStudent_Click(object sender, System.EventArgs e)
    {
    Response.Write("btnUpdateStudent <br>");
    //Seemed a pretty good way to get the index of which student to update
    Button b = (Button) sender;
    string vlistindex = Convert.ToString(b.ID).Substring(6);
    Faq

  7. #7
    knickerbocker is offline Registered User
    Join Date
    Jul 2004
    Posts
    1
    Rep Power
    0
    I have a similar situation...

    I'm working on a asp.net app, where the menu bar is the same throughout the app, so a javascript file was created to do this automatically on each page. This is where the problem lies. I'm trying to associate c# events to the menubar. When I try to code it, when I compile, it gives me an error saying the namespace "does not contain a definition for (object_name)" (b/c it is not created until the javascript file gets executed.)

    I guess my question is...how do I go about creating/associating events with html objects?
    any suggestions?

    thanks.
    Faq

  8. #8
    dritan is offline Registered User
    Join Date
    Oct 2004
    Posts
    1
    Rep Power
    0
    hi i have i similar problem.
    i have 2 problems and i'm stuck. can somebody please help me, because if somebody don't hepl me i don't
    know what to do. it's emergency!!! :-(
    Firt i thank all of you for your time!!!
    The code is in C# and the database is Oracle.
    I'll try to explain exactlly the problem.
    1-st Problem-Dynamic Links
    please look the database table "field" first.
    I need to create dynamiclink.
    first i call the DynamicLinks() function whitch create LinkButtons (they stay always the same on Pannel3) named from the database and
    their OnClick method does call an artikel from the database with the function GetArtikel (ArtikelName) and should creates
    other LinkButtons (on Panel3), but it does not, it does after i click twice (on the second click). the same problem is for
    the Linkbuttons on Panel3 (they call artikel from the database and create other linkbutton on Panel3 related to the clicked
    Button), i must click them twice to appear the changes.
    i hope that i explained good the problem, my english in not so good. i post the code, it's a bit long, i've put the unnecessary code away.
    The second Problem is described after the code. Please read it also!!!
    Here is the oracle table:

    Code:
    create table field (
    name varchar2(20),
    upperfield varchar2(20),
    primary key (name),
    foreign key (upperfield) references field(name),
    unique(name,upperfield));
    Here is the C# code:
    Code:
    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    Connection();
    DynamicLinks();
    DynamicLinks(Label6.Text);
    }
    else
    {
    DynamicLinks(); 
    DynamicLinks(Label6.Text);
    connection.Close();
    }
    }
    private void DynamicLinks(string field)
    {
    prm1 = cmd.Parameters.Add("pField",OracleDbType.Varchar2);
    prm1.Value = field;
    OracleDataReader reader;
    cmd.CommandText = "SELECT count(*) as Anzahl FROM field WHERE upperfield = :pField";
    Anzahl = Convert.ToInt32(reader["Anzahl"]);
     
    cmd.CommandText = "SELECT name FROM field WHERE upperfield = :pField";
     
    LinkButtons1 = new LinkButton[Anzahl];
    string [] fields = new string[Anzahl];
    while(reader.Read())
    {
    fields[i] = reader["name"].ToString();
    i++;
    }
     
     
    for (i = 0; i< Anzahl; i++)
    {
    LinkButtons1[i] = new LinkButton();
    LinkButtons1[i].Text = fields[i];
    LinkButtons1[i].Click += new EventHandler(this.LinkButtons1_Click);
    Panel1.Controls.Add(LinkButtons1[i]);
    Panel1.Controls.Add(new LiteralControl("<br>"));
    }
     
    }
     
    private void DynamicLinks()
    {
    cmd.CommandText = "SELECT count(*) as Anzahl FROM field WHERE upperfield IS NULL";
    Anzahl = Convert.ToInt32(reader["Anzahl"]);
     
    cmd.CommandText = "SELECT name FROM field WHERE upperfield IS NULL";
     
    LinkButtons = new LinkButton[Anzahl];
    string [] fields = new string[Anzahl];
     
    while(reader.Read())
    {
    fields[i] = reader["name"].ToString();
    i++;
    }
     
    for (int j = 0; j< Anzahl; j++)
    {
    LinkButtons[j] = new LinkButton();
    LinkButtons[j].Text = fields[j];
    LinkButtons[j].Click += new EventHandler(this.LinkButtons_Click);
    Panel3.Controls.Add(LinkButtons[j]);
    Panel3.Controls.Add(new LiteralControl("<br>"));
    }
     
    }
    private void LinkButtons_Click(object sender, System.EventArgs e)
    {
    for (int i = 0; i<LinkButtons.Length;i++)
    {
    if (sender.GetHashCode() == LinkButtons[i].GetHashCode())
    {
    Connection();
    Label1.Text = GetArtikel(LinkButtons[i].Text);
    Label2.Text = LinkButtons[i].Text;
    Label6.Text = LinkButtons[i].Text;
    }
    }
    DynamicLinks();
     
    }
     
    private void LinkButtons1_Click(object sender, System.EventArgs e)
    {
    for (int i = 0; i<LinkButtons1.Length;i++)
    {
    if (sender.GetHashCode() == LinkButtons1[i].GetHashCode())
    {
    Connection();
    Label1.Text = GetArtikel(LinkButtons1[i].Text);
    Label2.Text = LinkButtons1[i].Text;
    Label6.Text = LinkButtons1[i].Text;
    }
    }
    DynamicLinks(Label6.Text);
     
    }
    2-nd Problem

    I want to call an oracle stored procedure, but i don't know how.
    i call it like this but is does not work.
    Code:
    OracleCommand cmd1 = new OracleCommand("PATH",connection);
    cmd1.CommandType = CommandType.StoredProcedure;
    OracleParameter p1 = cmd1.Parameters.Add("field",OracleDbType.Varchar2);
    p1.Direction = ParameterDirection.Input;
    cmd1.Parameters["field"].Value = "example";
    cmd1.ExecuteNonQuery();
     
    OracleDataReader reader1 = (OracleDataReader) p1.Value;
    the procedure is declared like this:
    CREATE FUNCTION Path(field IN VARCHAR2) RETURN VARCHAR2

    THANKS TO ALL OF YOU.
    TONI
    Faq

  9. #9
    ras.ewu is offline Registered User
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    LiveWire chk dis out....

    http://bytes.com/forum/thread272589.html
    LIVEWIRE plz chk dis link...
    u will understand.........




    Quote Originally Posted by dritan
    hi i have i similar problem.
    i have 2 problems and i'm stuck. can somebody please help me, because if somebody don't hepl me i don't
    know what to do. it's emergency!!! :-(
    Firt i thank all of you for your time!!!
    The code is in C# and the database is Oracle.
    I'll try to explain exactlly the problem.
    1-st Problem-Dynamic Links
    please look the database table "field" first.
    I need to create dynamiclink.
    first i call the DynamicLinks() function whitch create LinkButtons (they stay always the same on Pannel3) named from the database and
    their OnClick method does call an artikel from the database with the function GetArtikel (ArtikelName) and should creates
    other LinkButtons (on Panel3), but it does not, it does after i click twice (on the second click). the same problem is for
    the Linkbuttons on Panel3 (they call artikel from the database and create other linkbutton on Panel3 related to the clicked
    Button), i must click them twice to appear the changes.
    i hope that i explained good the problem, my english in not so good. i post the code, it's a bit long, i've put the unnecessary code away.
    The second Problem is described after the code. Please read it also!!!
    Here is the oracle table:

    Code:
    create table field (
    name varchar2(20),
    upperfield varchar2(20),
    primary key (name),
    foreign key (upperfield) references field(name),
    unique(name,upperfield));
    Here is the C# code:
    Code:
    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    Connection();
    DynamicLinks();
    DynamicLinks(Label6.Text);
    }
    else
    {
    DynamicLinks(); 
    DynamicLinks(Label6.Text);
    connection.Close();
    }
    }
    private void DynamicLinks(string field)
    {
    prm1 = cmd.Parameters.Add("pField",OracleDbType.Varchar2);
    prm1.Value = field;
    OracleDataReader reader;
    cmd.CommandText = "SELECT count(*) as Anzahl FROM field WHERE upperfield = :pField";
    Anzahl = Convert.ToInt32(reader["Anzahl"]);
     
    cmd.CommandText = "SELECT name FROM field WHERE upperfield = :pField";
     
    LinkButtons1 = new LinkButton[Anzahl];
    string [] fields = new string[Anzahl];
    while(reader.Read())
    {
    fields[i] = reader["name"].ToString();
    i++;
    }
     
     
    for (i = 0; i< Anzahl; i++)
    {
    LinkButtons1[i] = new LinkButton();
    LinkButtons1[i].Text = fields[i];
    LinkButtons1[i].Click += new EventHandler(this.LinkButtons1_Click);
    Panel1.Controls.Add(LinkButtons1[i]);
    Panel1.Controls.Add(new LiteralControl("<br>"));
    }
     
    }
     
    private void DynamicLinks()
    {
    cmd.CommandText = "SELECT count(*) as Anzahl FROM field WHERE upperfield IS NULL";
    Anzahl = Convert.ToInt32(reader["Anzahl"]);
     
    cmd.CommandText = "SELECT name FROM field WHERE upperfield IS NULL";
     
    LinkButtons = new LinkButton[Anzahl];
    string [] fields = new string[Anzahl];
     
    while(reader.Read())
    {
    fields[i] = reader["name"].ToString();
    i++;
    }
     
    for (int j = 0; j< Anzahl; j++)
    {
    LinkButtons[j] = new LinkButton();
    LinkButtons[j].Text = fields[j];
    LinkButtons[j].Click += new EventHandler(this.LinkButtons_Click);
    Panel3.Controls.Add(LinkButtons[j]);
    Panel3.Controls.Add(new LiteralControl("<br>"));
    }
     
    }
    private void LinkButtons_Click(object sender, System.EventArgs e)
    {
    for (int i = 0; i<LinkButtons.Length;i++)
    {
    if (sender.GetHashCode() == LinkButtons[i].GetHashCode())
    {
    Connection();
    Label1.Text = GetArtikel(LinkButtons[i].Text);
    Label2.Text = LinkButtons[i].Text;
    Label6.Text = LinkButtons[i].Text;
    }
    }
    DynamicLinks();
     
    }
     
    private void LinkButtons1_Click(object sender, System.EventArgs e)
    {
    for (int i = 0; i<LinkButtons1.Length;i++)
    {
    if (sender.GetHashCode() == LinkButtons1[i].GetHashCode())
    {
    Connection();
    Label1.Text = GetArtikel(LinkButtons1[i].Text);
    Label2.Text = LinkButtons1[i].Text;
    Label6.Text = LinkButtons1[i].Text;
    }
    }
    DynamicLinks(Label6.Text);
     
    }
    2-nd Problem

    I want to call an oracle stored procedure, but i don't know how.
    i call it like this but is does not work.
    Code:
    OracleCommand cmd1 = new OracleCommand("PATH",connection);
    cmd1.CommandType = CommandType.StoredProcedure;
    OracleParameter p1 = cmd1.Parameters.Add("field",OracleDbType.Varchar2);
    p1.Direction = ParameterDirection.Input;
    cmd1.Parameters["field"].Value = "example";
    cmd1.ExecuteNonQuery();
     
    OracleDataReader reader1 = (OracleDataReader) p1.Value;
    the procedure is declared like this:
    CREATE FUNCTION Path(field IN VARCHAR2) RETURN VARCHAR2

    THANKS TO ALL OF YOU.
    TONI
    Faq

Share This Thread →

Become Part of This Conversation

Join NowFor Free!

Similar Threads

  1. re-using command buttons
    By Melissa_65 in forum Microsoft Access Help
    Replies: 3
    Last Post: February 3rd, 2004, 08:15 PM
  2. Events and dynamic web controls
    By korzaun in forum .NET Development
    Replies: 0
    Last Post: October 24th, 2003, 09:28 AM
  3. two buttons with ASP Page
    By Steve Schofield in forum ASP Development
    Replies: 0
    Last Post: July 31st, 2000, 06:05 AM
  4. On click event of submit button
    By Steve Schofield in forum ASP Development
    Replies: 1
    Last Post: July 10th, 2000, 03:28 PM
  5. Onclick event of submit button
    By Steve Schofield in forum ASP Development
    Replies: 0
    Last Post: May 24th, 2000, 03:27 AM

ASP Free Advertisers and Affiliates