I have a WebDropDown with a footer template that has an image control in with an id of "addimage". I want to tie an event handler to the click event of the image button, how do I do this? I cannot access a control named "addimage" in my codebehind and adding an attribute of "OnClick" to the aspx does not work either. If it makes a difference, my webdropdown is located in a usercontrol.
Thank you
Hi,
Here is an example, I hope it works for you:
ASPX:
<form id="form1" runat="server">
<asp:ScriptManager runat="server"></asp:ScriptManager>
<div>
<ig:WebDropDown ID="WebDropDown1" runat="server" Width="200px" DropDownContainerHeight="200px" EnableClosingDropDownOnBlur="false" EnableClosingDropDownOnSelect="false">
<Items>
<ig:DropDownItem Text="Item 1" Value="1"></ig:DropDownItem>
<ig:DropDownItem Text="Item 2" Value="2"></ig:DropDownItem>
<ig:DropDownItem Text="Item 3" Value="3"></ig:DropDownItem>
</Items>
<FooterTemplate>
<asp:ImageButton runat="server" ID="addimage" ImageUrl="<URL TO SOME IMAGE>" OnClick="addimage_click"/>
</FooterTemplate>
</ig:WebDropDown>
</div>
</form>
Code behind:
protected void addimage_click(object sender, ImageClickEventArgs e)
{
WebDropDown1.Items.Add(new Infragistics.Web.UI.ListControls.DropDownItem("new item"));
}
By the way, you are right that it should be possible to edit the footer/header template contents directly at DesignTime in Visual Studio, there you would be also able to add all handlers declaratively in the design time. Currently it is possible to only edit the ItemTemplate in the design time, there is a known issue that currently footer and header templates cannot be seen at design time, which is already fixed and will be available in a future hotfix.
Thanks,
Angel
If I put my WebDropDown directly on a page this works fine. If I put my WebDropDown in a UserControl or an Infragistics WebTab (the two options I have tried) it does not work. A post back does occur but the OnClick routine does not get fired.
i have created a blank UserControl , and put the above code in it (as well as the code behind for addimage_click, in the WebUserControl.ascx.cs ) , and i can see that OnClick is firing. In the ASPX , i am using the user control like that :
<%@ Register TagName="test" TagPrefix="ig" Src="~/WebUserControl.ascx" %>
OK - found the difference. In my usercontrol I am databinding to an ObjectDatasource. Once I do that the OnClick does not fire. I added databinding to my test WebDropDown that was placed directly on a page (as opposed to a usercontrol) with the same results, a post back occurs but the OnClick event does not get fired.
Thanks very much for the feedback and the sample. This seems to be a bug in the WebDropDown control. I have submitted an issue for this, and it will probably be fixed for the following hotfix.
In the meantime, if it is urgent for you to get this scenario working, you can have a workaround by setting the OnClientClick property of the ImageButton to point to some javascript function which adds a DropDown item or does something else, on the client side. This will automatically perform an ajax request and invoke the ItemAdded DropDown event on the server, where you can write your logic, that you would normally do in the ImageButton's onClick event handler.
<asp:ImageButton ID="pagebutton" runat="server" OnClientClick="addItem(); return false;" ImageUrl="~/images/add.png" ImageAlign="Middle" OnClick="pagebutton_Click"/>Add New
function addItem()
var dropDown = $find("WebDropDown1");
var item = dropDown.get_items().createItem();
item.set_text('some initial text'); // this is needed as well
dropDown.get_items().add(item);
I added a small test website to my original post that has the problem I am seeing where databinding interferes with the imagebutton onclick call.
Here is the aspx:
/>
<ig:WebDropDown ID="WebDropDown1" runat="server"
DropDownContainerHeight="200px"
Width="200px" MultipleSelectionType="Checkbox" DropDownAnimationType="Linear"
DropDownContainerMaxHeight="0px" DropDownContainerWidth="0px"
EnableDropDownAsChild="True" PageSize="0" ValueField="CMEItemId"
TextField="LongName"
DataSourceID="ObjectDataSource1" >
<DropDownItemBinding TextField="LongName" ValueField="CMEListId" />
<asp:ImageButton ID="pagebutton" runat="server" ImageUrl="~/images/add.png" ImageAlign="Middle" OnClick="pagebutton_Click"/>Add New
Here is the code behind:
Protected Sub pagebutton_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
Debug.WriteLine(
"pagebutton_Click")
WebDropDown1.Items.Add(
New Infragistics.Web.UI.ListControls.DropDownItem("new item"))
End Sub
The objectdatasource returns a strongly typed datatable. A post back does occur (I have a debug.writeline in the page_prerender which writes to the output) but the debug.writeline in the pagebutton_Click never gets written out nor does my breakpoint ever get hit. If I remove the databinding it works.
I cannot tell for sure whether this is a bug or not- in my test sample i also tried binding to SqlDataSource and the event seems to be fired correctly . Could you attach more detailed code (ASPX and code behind) - and i can verify this behavior.
Any thoughts on this?? Is it a bug or am I doing something wrong? Databinding (either manually by setting the datasource in code or by binding directly to an objectdatasource) seems to change how the control reacts.