ASP.Net Data Bind Environmental Issue - asp.net

Using a repeater to data bind a common control we are having issues where the data bound control is not being bound in different environments.
The old works on my machine issue.
We have the below repeater:
<web:Repeater ID="rptVehicles" runat="server" DataType="Entities.VehicleSummary" OnItemDataBound="rptItemList_OnItemDataBound">
<ItemTemplate>
<div style="display:none">
<p>Container Item = <%# (Container.DataItem == null) ? "null" : "not null" %></p>
<p>Container Item Id = <%# (Container.DataItem.ID == null) ? "null" : "not null" %></p>
<p>Container Item Id = <%# Container.DataItem.ID %></p>
</div>
<common:VehicleItem runat="server" ID="itmVehicle" DataSource="<%# Container.DataItem %>" ImageDefinition="<%# this.ImageDefinition %>" />
</ItemTemplate>
</web:Repeater>
And the contents of the Vehicle Item user control:
<div class="m-vitem">
<div style="display:none">
<p>DataSourceSet = <%# this.DataSourceSet %></p>
<p>Item DataSource = <%# (this.DataSource == null)? "null" : "not null" %></p>
</div>
On local the data source setter gets called passing in the Vehicle Summary entity.
DataSourceSet property is set to true in the DataSource property setter, on local its output is true on testing its false.
We are using different .Net versions on local and testing environments, does anyone know if there are any difference in the data bind handling between versions and what can be done, or am I barking up the wrong tree?
LOCAL - Microsoft .NET Framework Version:2.0.50727.4961; ASP.NET Version:2.0.50727.4955
TESTING- Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082

It looks like the code in your base property that was performing the set up was not being called since the overriding property on your vehicles entity. Marking a base property as virtual only makes the compiler choose which version of the property it needs to use at run-time. It does not mean that the base property setter's code is run before/after the sub class` property setter.
Also, it doesn't make sense to actually do any 'set up' in a property, you'd ideally want to move that to the base constructor, where it would be executed if you call your base constructor from your object's constructor. Alternatively but less ideally, you could perform the set up in your overriding property as well.
Moving the set up code to OnDataBinding works for you since the DataBind event is fired automatically whenever you set the DataSource property of the repeater.

Related

ASP.Net DataGrid selectively bound entries

I have less knowledge of ASP.Net. Currently, I am using ASP.Net DataGrid control and mapped all the source information in UI successfully.
myDataGrid.DataSource = MyCollectionOfObjects
myDataGrid.DataKeyField = "MyKey"
myDataGrid.DataBind()
Now, the problem is, I don't want all the objects to be mapped in the grid. Need to do some conditional filtering before mapping. Is there any feature in DataGrid that allow me to decide bind or not bind some objects?
1) One option is mapping the collection(MyCollectionOfObjects) to a new collection that contains only the required objects by applying required filtering but I am not expecting that at this moment.
For example,
foreach(var item in MyCollectionOfObjects)
{
if(item.InvalidEntry)
{
// This entry is not needed
}
else
{
// Okay with this entry
}
}
You could do something like this. Show/Hide a PlaceHolder based on the value of InvalidEntry.
<asp:TemplateColumn>
<ItemTemplate>
<asp:PlaceHolder ID="PlaceHolder2" Visible='<%# Convert.ToBoolean(Eval("InvalidEntry")) %>' runat="server">
<%# Eval("Column1") %>
</asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateColumn>
However, filtering the source data with Linq would be much easier.
myDataGrid.DataSource = MyCollectionOfObjects.Where(x => x.InvalidEntry == false);

__EVENTTARGET Contains Invalid Control ID

Googled variations of the title and everything was related to a null value.
I have an issue where the return value from Page.Request.Params["__EVENTTARGET"] duplicates the unique id of the control.
ctl00$MainContent$ActivityTabset$TabNewActivity$cbxActivityCode$ctl00$MainContent$ActivityTabset$TabNewActivity$cbxActivityCode
cbxActivityCode.UniqueID returns
ctl00$MainContent$ActivityTabset$TabNewActivity$cbxActivityCode
The following is the code that fails in the comparison. It is in the Page_Load event, and is the only code to execute right now if it is a postback.
string controlName = Page.Request.Params["__EVENTTARGET"];
if (cbxActivityCode.UniqueID == controlName)
{
ConfigureActivityUnits();
}
Here is the definition of the control
<obout:ComboBox ID="cbxActivityCode" runat="server"
DataSourceID="ObjectDataSourceDAOActivity"
FilterType="StartsWith" EmptyText="Select..."
AutoPostBack="true"
OnSelectedIndexChanged="cbxActivityCode_SelectedIndexChanged"
AllowCustomText="false" AutoValidate="true" DataValueField="Id"
DataTextField="Description" EnableViewState="true"
OpenOnFocus="true" MenuWidth="425" AllowEdit="False"
Width="300px">
</obout:ComboBox>
I am new to ASP.net and am wondering if it is possible one of the control attributes is causing this behavior?
Is it possibly a bug with the control?
Are there hooks in ASP.net where someone can manipulate a value which will effect Page.Request.Params["__EVENTTARGET"]? (This is a big messy legacy system, and I have no prior developers as a resource.)
If not any of the above, anyone have any ideas as to what could be causing this?

Setting local variable through repeater and anchor or label

I have a repeater which fetches data from a database and shows some labels (or anchors). I have a local variable on this class which must be updated by the value retrieved from database. To cut the matter short, there is a list of labels shown by repeater, when user clicks one of them, a variable on the form (which is called swfFilename) is fetched and is passed to the flash object on the page. My code is shown below:
<ItemTemplate>
<a onclick="<%#swfFileName = DataBinder.Eval(Container.DataItem, "MediaFile").ToString() %>" href="Index.aspx"> <%#DataBinder.Eval(Container.DataItem, "Text") %></a>
</ItemTemplate>
My code is not working properly and it seems that it is not assigning new value to the variable. Any help to assign the fetched value to the swfFilename? BTW, Index.aspx is the same page that we are currently over.
Would creating a javascript function ousdide of the repeater that takes the mediaFile string as a parameter and then manipulates the swf object work?
So something along these lines ...
<script>
function manipulateFlash(mediaFile) {
// set do stuff to flash object
// assign mediaFile to flash object etc.
}
</script>
<asp:Repeater>
...
<ItemTemplate>
<a onclick="manipulateFlash('<%#DataBinder.Eval(Container.DataItem, "MediaFile").ToString() %>')" href="Index.aspx"> <%#DataBinder.Eval(Container.DataItem, "Text") %></a>
</ItemTemplate>
...
</asp:Repeater>

Using codeblocks within usercontrols

I tried using a codeblock syntax within a property sent to a web user control:
<uc1:MyControl ID="MyControl1" runat="server" SomeProperty="<%= somevalue %>"/>
The user control has the public property SomeProperty declared and also uses code block to display the property value:
<p><% = SomeProperty %></p>
The output on my page is unfortunately
<p><%= somevalue %></p>
And not the actual value. Anyone know of some workaround for this?
You are trying to assign a server side value on a server side control - this is not possible.
You can use code blocks in client side code (that doesn't have a runat="server" attribute), this of course doesn't not apply to server side controls.
Set the attribute in the code behind (ascx), before OnRender:
// In onload, pre render or other event handler
MyControl1.SomeProperty = somevalue; // C#
MyControl1.SomeProperty = somevalue ' VB.NET
Try assigning the value of the property to a Label and call the .DataBind() method on the control.

In ASCX-control not doing IF/ELSE condition

There ASCX-controls that the program-loaded onto the page.
In a Repeater control in which, depending on the conditions displayed a different set of COLUMNS and DataTable with a different set of columns.
So, on the ASPX-page, this construction work Good.
<ItemTemplate>
<tr class="objectrow" href="<%# GetCompleteViewObjectLink(Convert.ToInt32(Eval("ID_Object")))%>">
<td align="center" class="c1">
<%# Eval("ID_Object") %>
</td>
<% if (GetObjectTypeName() == "Sot")
{ %>
<td align="center" class="c6">
<%# Eval("SOTName") != DBNull.Value ? Eval("SOTName") : ""%>
</td>
<% } %>
............................
But in program-loaded to page ASCX-control I have an Exception:
Error: DataBinding:
'System.Data.DataRowView' does not
contain a property named SOTName.
and another does not conform: in aspx-page my breakpoint on row
<% if (GetObjectTypeName() == "Sot")
was work off. But in ascx-control NOT.
Please, help! Why behaviour is so different? How to be?
Check your data for actual rows, i.e. row count? I would bet your DataSource is null.
I don't think you're getting data when you think you should be.
The Page Load event of the user control will execute before the aspx Page Load. If you are getting some type of parameter for your query in the .ascx in the Page Load of the .aspx, you ought to grab that in the Page_Init of the .aspx.
As TheGeekYouNeed points out, it's crucial to know when the DataSource of the Repeater is defined, as the control's events are processed before the page events.
You can add code like
<td>GetObjectTypeName='<%# GetObjectTypeName() %>'</td>
to find out whether the if() condition applies for your data.

Resources