Adding attribute values for dynamically created controls

Rate It (1)

Last post 08-23-2007 8:13 AM by kipo. 13 replies.

Sort Posts:

  • Adding attribute values for dynamically created controls

    08-20-2007, 5:20 AM
    • Loading...
    • shivamray
    • Joined on 08-14-2007, 4:17 AM
    • Posts 13
    Hello,

    I've tried various methods but none seem to work for me. Any help would be greatly appreciated.

    I am trying to dynamically set the "onchange" attribute for dropdownlist to call another fucntion. Here's what I have:

     
    filter.Attributes.Add("OnChange", "getoptions");
     

    I've seen other people use the same code and it seems to be working for them, however for me it's doing nothing.

    "filter" is a dynamically created dropdownlist which is being created when a postback function is called, I need to set the "onchange" attribute and then add the control to a repeater item's collection.

    Please Help!

    Thanks,
    Shivam.
  • Re: Adding attribute values for dynamically created controls

    08-20-2007, 7:32 AM
    • Loading...
    • MatsL
    • Joined on 01-31-2007, 8:22 AM
    • Sweden
    • Posts 258

    Check your casing... you have OnChange in your attribute add. should be 'onchange' (small caps)

  • Re: Adding attribute values for dynamically created controls

    08-20-2007, 2:47 PM
    • Loading...
    • shivamray
    • Joined on 08-14-2007, 4:17 AM
    • Posts 13

    Hi I ran some test code and it doesn't seem to be the casing. It seems like some thing's just not working with dynamically modifying attributes of a drop down list, whether the control was created dynamically or not.

    Here's my code in the .aspx page:

      

    1    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="testcode.aspx.cs" Inherits="testcode" %>
    2 3 "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml" > 6 "server">
    7 Untitled Page
    8 9 10 "form1" runat="server">
    11
    12 "DropDownList1" runat="server" AutoPostBack="true">
    13 1 14 2 15 16
    17
    18 "DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="test">
    19 1 20 2 21
    22 23 24 25
     

     So I created two dropdownlists, for DropDownList1 I set all the desired attributes except for the onchange attribute statically. And for DropDownList2 I set all the attributes statically including the "OnSelectedIndexChanged".

    In my code behind I attempted to change the attributes for DropDownList1 like so:

     

    1    using System;
    2 using System.Data;
    3 using System.Configuration;
    4 using System.Collections;
    5 using System.Web;
    6 using System.Web.Security;
    7 using System.Web.UI;
    8 using System.Web.UI.WebControls;
    9 using System.Web.UI.WebControls.WebParts;
    10 using System.Web.UI.HtmlControls;
    11 12 public partial class testcode : System.Web.UI.Page
    13 {
    14 protected void Page_Load(object sender, EventArgs e)
    15 {
    16 DropDownList1.Attributes.Add("onchange", "");
    17 }
    18 19 protected void test(object sender, EventArgs e)
    20 {
    21 Response.Write("works!");
    22 }
    23 }

    When I ran this code, DropDownList2 fine worked because everything was declared statically, but DropDownList1 still does not work. DropDownList1 does cause a page refresh or a post back when I change the selected item, but does not call the "test" function.

    Please note, in line 16 of the code above, if I change ("onchange", ""); to ("onchange", "test"); that makes Dropdownlist1 not even cause a postback.

    Please help, I desperately need this done.

    Thank you,

    Shivam.
     

  • Re: Adding attribute values for dynamically created controls

    08-21-2007, 2:33 AM
    • Loading...
    • MatsL
    • Joined on 01-31-2007, 8:22 AM
    • Sweden
    • Posts 258

    Ahh, sorry. Now I see what's wrong.

    You can't just write 'test' since that is not valid javascript. The string in the second parameter is the exact javascript that will be executed, not like the asp.net parameter that will map it to an event handler.

    If you want to execute a method that is called 'getoptions' that has no parameters you would have to write 'getoptions();'

  • Re: Adding attribute values for dynamically created controls

    08-21-2007, 3:52 AM
    • Loading...
    • shivamray
    • Joined on 08-14-2007, 4:17 AM
    • Posts 13

     Hi,

     I've tried ("onchange", "test()") also but that doesn't seem to do anything. If I enter any thing for the second parameter, in this case "test()" it seems to not even cause a post back. If i just two empty quotes it caueses a post back but results in nothing. Are you able to get this working on your machine? If you can help I would greatly appreciate it.

     

    Thanks,

    Shivam.


     

  • Re: Adding attribute values for dynamically created controls

    08-21-2007, 4:23 AM
    Answer
    • Loading...
    • kipo
    • Joined on 07-20-2006, 3:10 AM
    • Croatia
    • Posts 1,450

    Try with this code:

     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(ddl1_SelectedIndexChanged);
     }

     void ddl1_SelectedIndexChanged(object sender, EventArgs e)
     {
      DropDownList ddl1 = (DropDownList)sender;
      Response.Write(ddl1.SelectedValue);
     }

  • Re: Adding attribute values for dynamically created controls

    08-21-2007, 7:21 PM
    • Loading...
    • shivamray
    • Joined on 08-14-2007, 4:17 AM
    • Posts 13

    WOW! You did it!

     It works perfect.. I truely appreciate your help, I've posted on many other boards and people were just not very helpful in resolving this issue! I owe you one..

     Thanks again!

     

    Shivam.

     

  • Re: Adding attribute values for dynamically created controls

    08-21-2007, 11:01 PM
    • Loading...
    • shivamray
    • Joined on 08-14-2007, 4:17 AM
    • Posts 13

     Hey I tested your code and it works if the dropdownlist is created under the page load event. But if it is created by another function that was called from a control on the page then it does not seem to work. It does cause a page refresh but it's not calling the correct method. Is there a way of working around this?

     

    Thanks,

    Shivam.
     

  • Re: Adding attribute values for dynamically created controls

    08-22-2007, 2:44 AM
    • Loading...
    • kipo
    • Joined on 07-20-2006, 3:10 AM
    • Croatia
    • Posts 1,450

    Can you post your code?

  • Re: Adding attribute values for dynamically created controls

    08-22-2007, 2:50 AM
    • Loading...
    • shivamray
    • Joined on 08-14-2007, 4:17 AM
    • Posts 13

    Hi,

     Here's my code behind:

     

    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 page:

     

     <%@ 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>
     
     
  • Re: Adding attribute values for dynamically created controls

    08-22-2007, 3:05 AM
    • Loading...
    • MatsL
    • Joined on 01-31-2007, 8:22 AM
    • Sweden
    • Posts 258

    After you've added a control dynamically you need to add it during the postback. Otherwise the event won't be triggered.

    Since the webpage is recreated on each postback the page needs to have the same controls on the page to know which control triggered the postback, and then trigger the correct event on that control.

    You could throw in a hidden field on your page and set its value to some value when you add the control dynamically. Then you could check the value of that field during page_load and recreate the drop down list on postback.

    Or you could have the drop down list added "statically" in your .aspx file and just toggle the visibility to show / hide it. Probably a more "clean" solution.

  • Re: Adding attribute values for dynamically created controls

    08-22-2007, 3:17 AM
    • Loading...
    • kipo
    • Joined on 07-20-2006, 3:10 AM
    • Croatia
    • Posts 1,450

    MatsL explained you why is your code not working and here is the code which summarizes his explanation:

     protected void Page_Load(object sender, EventArgs e)
     {
      if (ViewState["ddl2"]!=null && (bool)ViewState["ddl2"])
      {
       CreateDropDownList2();
      }
     }

     protected void Button1_Click(object sender, EventArgs e)
     {
      CreateDropDownList2();
     }

     void ddl1_SelectedIndexChanged(object sender, EventArgs e)
     {
      Response.Write("Works");
     }

     private void CreateDropDownList2()
     {
      DropDownList ddl2 = new DropDownList();
      ddl2.Items.Add("Item1");
      ddl2.Items.Add("Item2");
      ddl2.AutoPostBack = true;
      ddl2.SelectedIndexChanged += new EventHandler(ddl1_SelectedIndexChanged);
      form1.Controls.Add(ddl2);
      ViewState["ddl2"] = true;
     }

  • Re: Adding attribute values for dynamically created controls

    08-23-2007, 3:24 AM
    • Loading...
    • shivamray
    • Joined on 08-14-2007, 4:17 AM
    • Posts 13

    Thanks for your reply Kipo,

    The code you have provided works for recreating the dropdownlist and calling another method. My problem is that I will not know the exact name of the control that caused the postback, there will be multiple dynamically created dropdownlists which need to call the "CreateDropDownList" method to add another dropdown list, which in turn will call the the "CreateDropDownList" method again to add in more dropdowns. How can I maintian all the the dropdownlists w/o hardcoding in any names?

    To give you a better idea, here's what I am trying to do:

    The user will enter a search string in a text box and hit the search button to bring back results from the db.
    Two dropdownlists will be displayed after the search results are brought back, the first will display refinement options available, such as refine by price, color, size, brand etc. and the second will display the different values available for the refinement filter they have selected, for example if they selected "Refine by Brand" in the first drop down, the second list will display "Brand A", "Brand B", "Brand C" etc.

    Once they select a value from the second list, an automatic post back will call the "CreateDropDownList" method to create a new row of dropdowns, again displaying the remaining choices of refinement left after they have made the first choice such as price, color, size etc.

    If you can point me in the right direction with this problem I will greatly appreciate it.

    Thank you,

    Shivam. 

  • Re: Adding attribute values for dynamically created controls

    08-23-2007, 8:13 AM
    • Loading...
    • kipo
    • Joined on 07-20-2006, 3:10 AM
    • Croatia
    • Posts 1,450

    It will be much easier for me to understand your needs if I see your code, so can you post it here?

Page 1 of 1 (14 items)