views:

2789

answers:

3

I'm creating a custom drop down list with AJAX dropdownextender. Inside my drop panel I have linkbuttons for my options.

<asp:Label ID="ddl_Remit" runat="server" Text="Select remit address."
    Style="display: block; width: 300px; padding:2px; padding-right: 50px; font-family: Tahoma; font-size: 11px;" />
<asp:Panel ID="DropPanel" runat="server" CssClass="ContextMenuPanel" Style="display :none; visibility: hidden;">
    <asp:LinkButton runat="server" ID="Option1z" Text="451 Stinky Place Drive <br/>North Nowhere, Nebraska 20503-2343 " OnClick="OnSelect" CssClass="ContextMenuItem" />
    <asp:LinkButton runat="server" ID="Option2z" Text="451 Stinky Place Drive <br/>North Nowhere, Nebraska 20503-2343 " OnClick="OnSelect" CssClass="ContextMenuItem" />
    <asp:LinkButton runat="server" ID="Option3z" Text="451 Stinky Place Drive <br/>North Nowhere, Nebraska 20503-2343 " OnClick="OnSelect" CssClass="ContextMenuItem" />-->
</asp:Panel>
<ajaxToolkit:DropDownExtender runat="server" ID="DDE"
    TargetControlID="ddl_Remit"
    DropDownControlID="DropPanel" />

And this works well. Now what I have to do is dynamically fill this dropdownlist. Here is my best attempt:

private void fillRemitDDL()
    {
        //LinkButton Text="451 Stinky Place Drive <br/>North Nowhere, Nebraska 20503-2343 " OnClick="OnSelect" CssClass="ContextMenuItem"

        DAL_ScanlineTableAdapters.SL_GetRemitByScanlineIDTableAdapter ta = new DAL_ScanlineTableAdapters.SL_GetRemitByScanlineIDTableAdapter();
        DataTable dt = (DataTable)ta.GetData(int.Parse(this.SLID));
        if (dt.Rows.Count > 0)
        {
            Panel ddl = this.FindControl("DropPanel") as Panel;
            ddl.Controls.Clear();
            for (int x = 0; x < dt.Rows.Count; x++)
            {
                LinkButton lb = new LinkButton();
                lb.Text = dt.Rows[x]["Remit3"].ToString().Trim() + "<br />" + dt.Rows[x]["Remit4"].ToString().Trim() + "<br />" + dt.Rows[x]["RemitZip"].ToString().Trim();
                lb.CssClass = "ContextMenuItem";
                lb.Attributes.Add("onclick", "setDDL(" + lb.Text + ")");
                ddl.Controls.Add(lb);
            }
        }
    }

My problem is that I cannot get the event to run script! I've tried the above code as well as replacing "lb.Attributes.Add("onclick", "setDDL(" + lb.Text + ")");" with "lb.Click += new EventHandler(OnSelect);" and also "lb.OnClientClick = "setDDL(" + lb.Text + ")");" I'm testing the the branches with Alerts on client-side and getting nothing.

Edit: I would like to try adding the generic anchor but I think I can add the element to an asp.net control. Nor can I access a client-side div from server code to add it that way. I'm going to have to use some sort of control with an event. My setDLL function goes as follows:

function setDDL(var)
    {
        alert(var);
        document.getElementById('ctl00_ContentPlaceHolder1_Scanline1_ddl_Remit').innerText = var;
    }

Also I just took out the string variable in the function call (i.e. from lb.Attributes.Add("onclick", "setDDL(" + lb.Text + ")"); to lb.Attributes.Add("onclick", "setDDL()");

+1  A: 

I'm not sure what your setDDL method does in your script but it should fire if one of the link buttons is clicked. I think you might be better off just inserting a generic html anchor though instead of a .net linkbutton as you will have no reference to the control on the server side. Then you can handle the data excahnge with your setDDL method. Furthermore you might want to quote the string you are placing inside the call to setDDL because will cause script issues (like not calling the method + page errors) given you are placing literal string data without quotes.

Quintin Robinson
A: 

the add should probably look like this (add the '' around the string and add a ; to the end of the javascript statement).

lb.Attributes.Add("onclick", "setDDL('" + lb.Text + "');");

OR!

set the OnClientClick property on the linkbutton.

pinkeerach
+1  A: 

Ok, I used Literals to create anchor tags with onclicks on them and that seems to be working great. Thanks alot.

Cptcecil