calling methods outside of Page_Load not working from dropdownlist


Joined
Aug 20, 2007
Messages
3
Reaction score
0
Hello,

This is starting to really bug me. If I make a call to a function from the Page_Load it does what it supposed to do, but if I make the same call from a control on the page it does not work properly.

I am trying to create a function that dynamically creates drop down lists, bound to a db, and then sets the "onchange" property so that when the user changes the selected item it posts back and executes code on the server side. Here's my code behind:

Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
   
   public partial class testcode : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {
           DropDownList ddl1 = new DropDownList();
           ddl1.Items.Add("Item1");
           ddl1.Items.Add("Item2");
           ddl1.AutoPostBack = true;
           form1.Controls.Add(ddl1);
           ddl1.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);        
       }
       protected void x(object sender, EventArgs e)
       {
           DropDownList ddl2 = new DropDownList();
           ddl2.Items.Add("1");
           ddl2.Items.Add("2");
           ddl2.AutoPostBack = true;
           form1.Controls.Add(ddl2);
           ddl2.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
       }
       void ddl_SelectedIndexChanged(object sender, EventArgs e)
       {
           
           
           Response.Write("works");
       }
   }

And my .aspx:

Code:
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="testcode.aspx.cs" Inherits="testcode" EnableViewState="true" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
       <form id="form1" runat="server">
      <asp:Button runat="server" Text="Push Me" OnClick="x" />   
       </form>
   </body>
   </html>

The dropdownlist that is created on page load works fine, and upon changing the selected item from this dropdownlist it is able successfully call the correct function. However, the dropdownlist created by the button is not able to call the function properly, it just causes a page reload, and the second dropdownlist created by the button dissapeasrs. Any help would be greatly appreciated!

Thanks,

Shivam.
 
Last edited:
Ad

Advertisements


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

Top