Sunday, March 11, 2012

DropDownExtender - Dynamic Link Buttons wont fire server event.

hello.

from what event are you adding the buttons to the page?


Hi Luis,

I've tried the code just about everywhere (PreInit, Init, PreLoad, PreRender), it doesn't seem to make a difference.

I even tried putting a literal control on my page, and adding the raw HTML to create ASP:LINKBUTTON tags. Regardless of what page event I used for this, the ASP controls were never translated to plain HTML on the page render, so these tags were just ignored (in IE6 anyway).


can you build a simple demo page and put it here? and i'm not sure i've really understood the problem, so maybe it's better if you explaint it again (sorry, i'm a little slow...)


Hi Luis,

I had something else come up yesterday, so I'm just getting your example here. This is a very simple example, no style sheets so the format isn't too pretty, but it does show my problem. The drop down lets you select from three vendors and should change the label text to the selected vendor. Vendor 1 and Vendor 2 are hard coded into the HTML; Vendor 3 is being added in code behind. Vendors 1 and 2 correctly fire the ddmDwVendorChanged event and change the label text. Vendor 3 performs a postback, but does not fire the ddmDwVendorChanged event.

Maybe I'm going about adding the control the wrong way, or maybe Attributes.Add is not the right way to add the server-side onClick event.

Thanks for your help!

<%@.PageLanguage="vb"AutoEventWireup="false"CodeBehind="Default.aspx.vb"Inherits="Test._Default" %>

<%@.REGISTERassembly="AjaxControlToolkit"namespace="AjaxControlToolkit"tagprefix="cc1" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<asp:ScriptManagerID="ScriptManager1"runat="server"/>

<div>

<ASP:LABELid="lblDwVendor"runat="server"width="150px">Select A Vendor</ASP:LABEL>

<ASP:PANELid="pnlDwVendor"runat="server"style="display :none; visibility: hidden;">

<asp:LinkButtonrunat="server"ID="Vendor1"Text="Vendor 1"OnClick="ddmDwVendorChanged"/>

<asp:LinkButtonrunat="server"ID="Vendor2"Text="Vendor 2"OnClick="ddmDwVendorChanged"/>

</ASP:PANEL>

<CC1:DROPDOWNEXTENDERid="DropDownExtender1"dropdowncontrolid="pnlDwVendor"runat="server"targetcontrolid="lblDwVendor"></CC1:DROPDOWNEXTENDER>

</div>

</form>

</body>

</html>

PartialPublicClass _Default

Inherits System.Web.UI.Page

PrivateSub Page_PreInit(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.PreInit

Dim objLinkAsNew LinkButton

objLink.Attributes.Add("onClick","ddmDwVendorChanged")

objLink.ID ="Vendor3"

objLink.Text ="Vendor 3"

Me.pnlDwVendor.Controls.Add(objLink)

EndSub

ProtectedSub ddmDwVendorChanged(ByVal senderAsObject,ByVal eAs EventArgs)

Me.lblDwVendor.Text = sender.text

EndSub

EndClass


hello.

the attributes.Add method you're calling is not doing what you expect. if you write that code, you're saying that then someone clicks on the button, it should call ddmDwVendorChanged (which will eventually give you a js error which you're not seeing because you probably have it disabled on IE). So, replace that line with this one:

'remove it: objLink.Attributes.Add("onClick", "ddmDwVendorChanged")
AddHandler objLink.Click, AddressOf ddmDwVendorChanged


Hi Luis,

That's exactly what I needed. I've used the Attributes.Add in the past to add client-side events, but I didn't know if it would work for server-side (obviously not).

Thanks for all your help!

No comments:

Post a Comment