How to find which control event got triggered, for example, if there are 5 columns in the repeater if the 2nd checkbox or 3rd drop-down list causes the event. How to find which control event got triggered, so that the particular control related logic alone will be executed without disturbing other column controls.
The sample repeater code is attached as follows,
<asp:Repeater ID="rptTest" runat="server">
<ItemTemplate>
<td class="repeater-col">
<div>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
</div>
<div>
<asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="ddl1_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
</div>
<div>
<asp:CheckBox ID="chk1" runat="server" OnCheckedChanged="chk1_CheckedChanged" AutoPostBack="true" />
</div>
</td>
</ItemTemplate>
</asp:Repeater>
There are lot more dependent controls presented inside the repeater. Based on the selection of the controls the data to the other controls are bound and processed. The logic bounded to the repeater will be handled on the respective events for example on chk1_CheckedChanged and ddl1_SelectedIndexChanged
Kindly help on this! Thanks in advance!
You can cast the sender back to the correct Control type. Then you can access it's properties. If you want to know which items the control was in, you can use the NamingContainer
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList drp = sender as DropDownList;
drp.BackColor = Color.Green;
RepeaterItem item = drp.NamingContainer as RepeaterItem;
int itemIndex = item.ItemIndex;
}
Related
I am newbie for asp.net,
I have a repeater which contains check box,whenever I check the check box I am firing an event on checkchanged,but the page postbacks.
I have an update panel for the entire content in my page,but still postback occurs.Is there anyway to avoid postback.
(Ps:To avoid Postback,I am meaning to avoid the flicker that occurs)
Thanks
<asp:Repeater ID="rptrDepartment" runat="server" OnItemCommand="rptrDepartment_ItemCommand"
OnItemDataBound="rptrdepartment_databound">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID ="chkRow" runat="server" OnCheckedChanged="ChkRow_ChkChanged" AutoPostback="true" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
And in my .cs page,
protected void ChkRow_ChkChanged(object sender, EventArgs e)
{
//some method
}
Just keep your repeater inside update panel rather than entire page, if you want some other controls also need partial postback then you can go for multiple update panels.
I have a repeater and I set the value of an html check box control with the
value of an enumeration instead of hard-coding a magic number. When I try to
access the html check box control in the repeater's ItemCreated event handler,
the value is an empty string. Why is this and how can I fix it?
C# Code
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var myObject = e.Item.DataItem as MyObject;
if (myObject != null)
{
var checkBox = e.Item.FindControl("checkbox1") as HtmlInputCheckBox
// The value is empty!
var value = checkBox.Value;
}
}
Not Working
<asp:Repeater ID="Repeater1" OnItemCreated="Repeater1_ItemCreated" runat="server">
<ItemTemplate>
<input type="checkbox" id="checkbox1" value='<%# SomeEnum.Value %>' />
</ItemTemplate>
</asp:Repeater>
Working
<asp:Repeater ID="Repeater1" OnItemCreated="Repeater1_ItemCreated" runat="server">
<ItemTemplate>
<input type="checkbox" id="checkbox1" value="1" />
</ItemTemplate>
</asp:Repeater>
ItemCreated is triggered before ItemDataBound and also on every postback to recreate he controls even when the Repater is not databound again. So i would not use ItemCreated if you need to access the DataSource of any databound WebControl like Repeater.
Apart from that, make the checkbox runat=server(or use a ASP.NET CheckBox) if you want to find it on the server.
I have a listview that loads dynamic controls from xml/xslt
<asp:ListView ID="DynamicFields" runat="server"
DataSourceID="CustomFields"
OnItemDataBound="DynamicFields_ItemDataBound"
GroupItemCount="2" ItemPlaceholderID="itemsGroup"
GroupPlaceholderID="itemsGroup">
<LayoutTemplate>
<table width="470" border="0" cellpadding="0" cellspacing="10">
<asp:PlaceHolder ID="itemsGroup" runat="server" />
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemsGroup"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<custom:CustomField ID="Field" runat="server"
FieldIndex='<%# Eval("index") %>' />
</ItemTemplate>
<AlternatingItemTemplate>
<custom:CustomField ID="Field" runat="server"
FieldIndex='<%# Eval("index") %>' />
</AlternatingItemTemplate>
</asp:ListView>
And in my page codebehind I'm binding user control properties and calling bind method that loads xml and creates controls
protected void DynamicFields_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ucCustomField uc = (ucCustomField)e.Item.FindControl("Field");
uc.FileName = FORM_PATH;
uc.FormName = FORM_NAME;
uc.LoadXMLFile(); //binding xml content here
}
}
I add this listview to an existing form which has static controls and save button. When I click button it causes validation and shows me validation summary popup messages[expected] and all my dynamic controls in listview disappear after i click ok in popup.
How can I keep these controls visible and also any values that may have been entered after postback?
And in my page codebehind i'm binding user control properties and calling bind method that loads xml and creates controls
Can you tell where are you calling the Bind/Databind method. Putting that in pageload will i guess solve the issue. The post back will remove anything that has been dynamically added to the page.
Regards,
SJ
make sure you are not inside of a Header not sure if this will help or not but try the following
if(e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.AlternatingItem)
{
}
fixed this by calling DynamicFields.DataBind() when Page.IsPostback is true
I know that this sounds like a number of other posts, all of which I have read but have not addressed my issue.
Here's the Scenario ...
I have BOTH a Repeater and a ListView bound to the same Data Source. Each control contains an ASP:LinkButton which, when clicked, should fire the OnItemCommand event. Although they are wired to the EXACT same data at the EXACT same places in the page life cycle and View State is enabled for the page and each individual control the Repeater appears to fire the event and the ListView does not.
I know that the event will not fire if the data is not bound BEFORE the assignment of the event handler. I am relying on ViewState to repopulate the controls when the page is posted back to. Looking at each control in debug mode while stepping through a request I can see that the Repeater DOES indeed appear to repopulated with the ViewState data but the ListView does not.
As these are both generated, populated, bound, and handled almost IDENTICALLY I am at a complete loss why this may be happening. I have also noticed a similar issue with the GridView control (it does NOT fire the event). I assume that these are related somehow.
The only thing that I can think of that the GridView and ListView have in common that the Repeater does not is the built-in paging capability. Whether implemented or not is there something with the paging that affects the loading of the ViewState?
OrderControl.ASCX is a control which exposes the ListView and Repeater as properties (OrderListLV & OrderListRPT) to the host page/application.
<asp:ListView runat="server" id="lvOrderList" OnItemDataBound="lstOrderList_OnItemDataBound" EnableViewState="true" >
<LayoutTemplate>
<table class="tblGrid">
<tr runat="server" id="itemPlaceholder" />
</table>
<ASP:DataPager runat="server" ID="dataPager1" PageSize="3">
<Fields>
<ASP:NextPreviousPagerField
ButtonType="Button"
ShowFirstPageButton="true"
ShowLastPageButton="true"
ShowNextPageButton="true"
ShowPreviousPageButton="true" />
</Fields>
</ASP:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr class="row-">
<td align="center"><ASP:LinkButton runat="server" id="lnkOrderId1" /></td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:Repeater runat="server" id="rptOrderList" OnItemDataBound="rptOrderList_ItemDataBound">
<HeaderTemplate>
<table class="tblGrid">
</HeaderTemplate>
<ItemTemplate>
<tr">
<td align="center"><ASP:LinkButton runat="server" id="lnkOrderId" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
OrderControl.ASCX.CS is where the controls are bound to the data source in the Pre-Render stage, well after ViewState has had the opportunity to reconstitute itself.
protected override void OnPreRender(EventArgs e)
{
this.lstOrderList.DataSource = this.OrderHeaders.OrderByDescending(x => x.OrderDate).ToList();
this.lstOrderList.DataBind();
this.rptOrderList.DataSource = this.OrderHeaders.OrderByDescending(x => x.OrderDate).ToList();
this.rptOrderList.DataBind();
}
Host.ASPX.CS is the page which consumes the control. It attaches the event handlers directly the controls in it's OnLoad handler.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.OrderControl.OrderListRPT.ItemCommand += new RepeaterCommandEventHandler(OrderList2_ItemCommand);
this.OrderControl.OrderListLV.ItemCommand += new EventHandler<ListViewCommandEventArgs>(OrderList_ItemCommand);
}
After all is said and done when I click on the LinkButton in each control the ItemCommand Handler for the Repeater fires and executes correctly but the process doesn't even enter the handler for the ListView handler.
I am crazy confused on this issue. I am hoping that someone might have some thoughts on this. Something I can try at least?
Thanks,
Gary
you should set the CommandName property of each LinkButton in the ListView, eventually also the CommandArgument.
<td align="center">
<asp:LinkButton runat="server" id="lnkOrderId1" CommandName="yourCommandName" CommandArgument="yourCommandArgument" />
</td>
also check asked questions here in SO before posting a new question ;-)
listview OnItemCommand dosen't fire up
I have a DropDownList on a repeater control, as well as a button.
The button is disabled until a valid item is selected on the DropDownList, when I want to enable the button. Unfortunately I can't seem to get to it.
Found the repeater by: (.As() method is an extension method for (object as T), just makes casting easier)
sender.As<Control>().NamingContainer.Parent.As<Repeater>()
However the Repeater I get back doesn't help me as the FindControl(string name) function isn't returning anything - and shows nothing useful in the watch window.
So, how can I get a sibling control (an ImageButton in this case) on a repeater from an event of another item on the repeater (DropDown_SelectedIndexChanged in this case)?
EDIT
I finally worked out
sender.As<ImageButton>().NamingContainer.As<RepeaterItem>().FindControl("ControlName")
I think i have the answer for your question:
1.-I create a repeater with the dropdownlist and button to do the tests:
<asp:Repeater ID="rp" runat="server">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
</asp:DropDownList>
<asp:ImageButton ID="Button1" runat="server" Enabled="False" />
</ItemTemplate>
</asp:Repeater>
I databind the repeater.
2.-I create the method DropDownList1_SelectedIndexChanged:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList control = (DropDownList)sender;
RepeaterItem rpItem = control.NamingContainer as RepeaterItem;
if (rpItem != null)
{
ImageButton btn = ((ImageButton)rpItem.FindControl("Button1"));
btn.Enabled = true;
}
}
The way to do it is to ask to the control, who is its Parent, that's to say, the RepeaterItem, or you can use NamingContainer (as I have written finally) and there you can ask about any control that is inside.