Repeater's child controls will loss data in the ViewState when set a breakpoint in overrided method OnInit in the authored web control - viewstate

When I click button "Button1" after the first time, the repeater's child controls will lose data in the viewstate. I added a watch on this.ChildControlsCreated. It is always true. This variable should initially be false.
The test code:
/////////////////////CustomRepeater.cs/////////////////////
public class CustomRepeater : Repeater
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e); **// Sets a breakpoint here**
}
}
/////////////////////TestCustomRepeater.aspx/////////////////////
<table>
<cc1:CustomRepeater id="Control1" runat="server">
<ItemTemplate>
<tr><td><%# DataBinder.Eval(Container.DataItem) %></td></tr>
</ItemTemplate>
</cc1:CustomRepeater>
</table>
<asp:Button id="Button1" runat="server" OnClick="Button1_OnClick"></asp:Button>
/////////////////////TestCustomRepeater.aspx.cs/////////////////////
Page_load()
{
if (!IsPostBack)
{
ArrayList dataSource = new ArrayList();
dataSource.Add("a");
dataSource.Add("b");
this.Control1.DataSource = dataSource;
this.Control1.DataBind();
}
}
protect void Button1_OnClick(object sender, EventArgs e)
{
// Nothing to do, just to trigger a POSTBACK
}

You will need to re-bind the Repeater after your postback. You can do it inside the method Button1_OnClick

Related

Can't seem to get the from <asp:Literal </asp:Literal> property in Web forms

Could someone please pin point me to what i'm doing wrong?
Basically all i need to is get the text from my property but for some reason it always returns as null.
protected void Page_Load(object sender, EventArgs e)
{
orderId.Text = "4567";
}
protected void UpdateOrder(object sender, EventArgs e)
{
var getOrderId = orderId.Text; //always returns null here
}
front end:
<p>
<asp:Label ID="label8" runat="server" AssociatedControlID="orderId"> Order Id: </asp:Label>
<asp:Literal ID="orderId" runat="server"></asp:Literal>
</p> << the orderId is displayed in the browser
<asp:Button ID="updateBtn" runat="server" Text="Update" OnClick="UpdateOrder"/>
What am i doing wrong?
Have you got a control somewhere else on your page called order?
Your literal is called orderId
You're pulling a value from something called order.
Try:-
protected void UpdateOrder(object sender, EventArgs e)
{
var getOrderId = orderId.Text; //always returns null here
}
The problem was that I had
EnableViewState="false"
at the top of my page. I removed it and boom it works

JSF + Primefaces + StreamedContent + documentViewer + ajax

I'm using Primefaces 5.2 with extensions 3.1.
I have my datatable and on click on each row I'd like to display modal and in this modal use documentViewer which will display document based on parameter passed from selected row.
With this I'm invoking my modal from within datatable
<p:ajax event="rowSelect" update=":previewDataForm" oncomplete="$('.previewDataModal').modal();" immediate="true">
<f:param name="pdfFile" value="#{row.dataPath}"/>
</p:ajax>
this is my modal:
<b:modal id="previewDataModal" title="Preview" styleClass="orderPreviewModalPseudoClass">
<h:form id="previewDataForm">
<pe:documentViewer height="550" value="#{contentStreamHelperBean.pdfFromFileSystem}" />
</h:form>
</b:modal>
and this is my stream helper
#Component("contentStreamHelperBean")
#Scope("request")
public class ContentStreamHelperBean extends BaseBean {
private static final Logger log = LoggerFactory.getLogger(ContentStreamHelperBean.class);
public StreamedContent getPdfFromFileSystem() {
String pdfFile = getRequestAttribute("pdfFile");
if (getFacesContext().getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
return new DefaultStreamedContent();
} else {
if (StringUtils.isNotEmpty(pdfFile))
try {
return new DefaultStreamedContent(new FileInputStream(new File(pdfFile)), "application/pdf");
} catch (FileNotFoundException e) {
log.error("Unable to get pdf", e);
}
}
return new DefaultStreamedContent();
}
}
Problem is, that when onclick is invoked I can't get pdfFile param in my ContentStreamHelper so:
how to pass parameter from dataTable rowSelect to my backingBean?
is this good approach how to display PDF with documentViewer?

Primefaces how to toggle a dashboard widget/panel visibility using ajax?

I have a dashboard with a considerable number of widgets / panels which are working just fine.
I'm looking for a way to toggle the visibility of a specific one using a commandButton ction listener without having to refresh the page, i.e. via AJAX.
<p:dashboard id="board" model="#{dashboardBean.model}">
<!-- iteration code begins -->
<p:panel id="#{wdgt.code}" header="#{wdgt.title}">
<h:outputText value="One of the dozens of widgets" />
</p:panel>
<!-- iteration code ends -->
<p:panel id="staticWdgtId" header="Static Widget Initially Invisible" visible="false">
Some static content
</p:panel>
</p:dashboard>
Then in the backing bean, at some point this action needs to be fired via a commandButton or an actionListener...
public void showTheWidget() {
DashboardColumn dbc = this.model.getColumn(1);
dbc.getWidget(2); // does not get a widget object with a visibility attribute :((
// so that I could manipulate such as dbc.getWidget(2).setVisible(true);
}
Any ideas?
STATIC APPROACH
You can associate the panel with a boolean.
Panel
<p:panel id="staticWdgtId" header="Static Widget Initially Invisible"
visible="#{bean.panelShow}">
Some static content
</p:panel>
Button
<p:commandButton actionListener="#{bean.actionListener()}"
value="Button"
update=":staticWdgtId" />
Bean
public void actionListener() {
setShowPanel(true);
}
DYNAMIC APPROACH
Render all the panel with display: none
Dashboard
<p:dashboard id="board" model="#{dashboardBean.model}">
<p:panel id="#{wdgt.code}" header="#{wdgt.title}" style="display: none">
<h:outputText value="One of the dozens of widgets" />
</p:panel>
</p:dashboard>
remoteCommand
<p:remoteCommand name="panelsToShow"
actionListener="#{bean.panelsToShowAction()}"
oncomplete="handleComplete(xhr, status, args)" />
bean.panelsToShowAction() you need Gson
public void panelsToShowAction() {
List<String> panels = new ArrayList<String>();
//iterate over the panels you want to show, and put #{wdgt.code} which is the id of the panel
panels.add("Code1");//id
panels.add("Code2");//id
RequestContext requestContext = RequestContext.getCurrentInstance();
requestContext.addCallbackParam("panels", new Gson().toJson(panels));
}
JS
$(document).ready(function() {
panelsToShow();
})
function handleComplete(xhr, status, args) {
var panels = eval('(' + args.panels + ')');
for (var i = 0, len = panels.length; i < len; i++) {
$('#'+panels[i]).show();
}
}

How to access a DIV that is inside a Telerik RadGrid

I have a div with its own id in the ItemTemplate of a GridTemplateColumn of a RadGrid.
How can I access this div from the code behind?
If it was a div in the web page I could access it just by typing its id but this method does not work now that it is nested inside the grid.
Please use this example.
in Aspx page
<telerik:GridTemplateColumn>
<ItemTemplate>
<div id="divlayer" runat="server">
Hello
</div>
</ItemTemplate>
</telerik:GridTemplateColumn>
In CS
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
HtmlGenericControl container = (HtmlGenericControl)item.FindControl("divlayer");
}
}
Hope it might help you..

Using Telerik RadComboBox within a repeater

I have a repeater that contains a Telerik RadComboBox:
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<telerik:RadComboBox ID="rcb" runat="server" EnableLoadOnDemand="true"
AllowCustomText="true" ItemRequestTimeout="1000"
NumberOfItems="10" MarkFirstMatch="false">
</telerik:RadComboBox>
</ItemTemplate>
</asp:Repeater>
In the ItemDataBound event of the Repeater, I am wiring up the ItemsRequested event like this:
private void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e) {
RadComboBox rcb = (RadComboBox)e.Item.FindControl("rcb");
rcb.ItemsRequested += rcb_ItemsRequested;
}
private void rcb_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e) {
// Database call to load items occurs here.
// As configured, this method is never called.
}
Currently, the server-side rcb_ItemsRequested method is never called. I suspect that wiring the ItemsRequested event in the ItemDataBound is problematic, but the problem may lie elsewhere.
Any ideas on how to use the Telerik RadComboBox within a repeater properly?
Have you tried putting the event handler wiring in the markup rather than adding it dynamically?
Also - you are probably aware, but just in case - ItemsRequested is an event that only fires under certain conditions. To quote the docs:
The ItemsRequested event occurs when the EnabledLoadOnDemand property is True and the user types text into the input field or clicks on the drop-down toggle image when the list is empty. - Reference
Does your scenario match the above?
EDIT:
I've tested some code. The following works (The ItemsRequested Event fires for the all ComboBoxes and adds the three test items to the dropdown on the fly..):
Markup:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">
<ItemTemplate>
<br />
<telerik:RadComboBox ID="rcb" runat="server" EnableLoadOnDemand="true" AllowCustomText="true"
ItemRequestTimeout="1000" NumberOfItems="10" MarkFirstMatch="false" />
</ItemTemplate>
</asp:Repeater>
</form>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
List<string> data = new List<string>();
data.Add("Item 1");
data.Add("Item 2");
//add some items to the repeater to force it to bind and repeat..
rpt.DataSource = data;
rpt.DataBind();
}
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//wire the event
RadComboBox rcb = (RadComboBox)e.Item.FindControl("rcb");
rcb.ItemsRequested += rcb_ItemsRequested;
}
protected void rcb_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
//add the items when requested.
(sender as RadComboBox).Items.Add(new RadComboBoxItem("Item1", "1"));
(sender as RadComboBox).Items.Add(new RadComboBoxItem("Item2", "2"));
(sender as RadComboBox).Items.Add(new RadComboBoxItem("Item3", "3"));
}

Resources