Button event in DynamicDetailContainer
I have a module widget where I am calling out to a code file that handles some extra logic for the DynamicDetailContainer. I can access the containers databound events and even the controls within the LayoutTemplate just fine, but I am trying to figure out how one could get a click event for a button to fire.
I can successfully find the button within detailsContainer_DataBound, but any attempt to assign it a click event or command argument approach seems to be ignored. I am guessing I am too far into the page lifecycle at this point to do that?
I can move the button outside of the DynamicDetailsContainer and bind it that way just fine. Wondering how I could do this within the the control if possible?
Here is what I tried:
protected void detailContainer_DataBound(object sender, EventArgs e)
var container = sender as DynamicDetailContainer;
var button = (Button)container.Controls[0].FindControl("submitButton");
if (button != null) button.Click += button_Click; //This is not null
protected void button_Click(object sender, EventArgs e) //this is never reached
Hello Stacey,
The click event doesn't work in a ListView. Try using OnCommand and catch it when it bubble up to the ListView.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.command%28v=vs.100%29.aspx
All the best,
Randy Hodge
the Telerik team
I had already tried using OnCommand and that does not fire either. I am not sure what I am missing here. The below code runs except for the command event never triggers. The button is found it just never does anything after that point. That is why I wondered if it was already too late in the the page cycle to do this. I ended up moving the button outside of the DynamicDetailContainer to get on with life, but if you see something I missed I would love to know.
protected void detailContainer_DataBound(object sender, EventArgs e)
var container = sender as DynamicDetailContainer;
var repeater = (Repeater)container.Controls[0].FindControl("ImageListRepeater");
if (repeater != null)
repeater.ItemDataBound += repeater_ItemDataBound;
var button = (Button)container.Controls[0].FindControl("submitButton");
if (button != null)
button.Command += button_Command;
button.CommandName = "Test";
button.CommandArgument = "0";
Hello Stacey,
You can't get to the button event directly. You have to subscribe to the ListView's ItemCommand event.
This will fire when any button in the ListView is clicked.
I hope this helps you.
All the best,
Randy Hodge
the Telerik team
Randy,
Thanks for bearing with me here, but this is not a ListView...although I guess it probably is based off one. The control in the widget template is <sf:DynamicDetailContainer ID="detailContainer" runat="server">
I looked all around and even in the API documentation at DynamicDetailContainer and did not see that it has an ItemCommand event that I can tie into. I even tried to assign is in code behind, but intellisense was throwing errors and I could not build with it. Is there a specific namespace I need to access ItemCommand for DynamicDetailContainer?
Hi Stacey,
We have managed to come up with a workaround, which, although not the most elegant one, should do the job. What I'm doing is checking the Form Keys in the request passing through Page_Load on PostBack. If the request has been initiated by the button, it (its client Id, actually) will be present in the list of AllKeys:
protected
void
Page_Load(
object
sender, EventArgs e)
if
(IsPostBack)
if
(Request.Form.AllKeys.Contains(
"ctl19$ctl00$ctl00$Button1"
))
//execute code here