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.
Related
On Grid_RowDataBound I have added a table in the 8th cell of the current row. This table is like:
<table rules="all" border="0" style="width:100%;">
<tbody>
<tr>
<td align="center"><input name="studentgrd$ctl03$ID2VAL4" type="text" value="6" maxlength="2" id="studentgrd_ctl03_ID2VAL4" style="width:20px;"></td>
</tr>
</tbody>
</table>
On save button click, I tried to get the data using:
TextBox tb = (TextBox)studentgrd.FindControl("ID2VAL4");
TextBox tb = (TextBox)studentgrd.Rows[i].Cells[8].FindControl("ID2VAL4");//When looping through all rows
But the textboxes were null. So are the dynamic controls lost on postback? If yes, then how to retain it? If it is not, where am I going wrong?
You can assign a GridviewRow like this in your button click method:
GridViewRow gridViewRow = (GridViewRow)(((Control)sender).NamingContainer);
When assigning value you should change it to:
TextBox tb = ((TextBox)gridViewRow .FindControl("ID2VAL4"));
where gridViewRow is a GridViewRow that is used not the GridView itself.
I solved my problem: if it is a dynamically-generated control, and bounded to data source that is loaded for the first time, then on post back you have to again generate the same control and bind it, and then try to access them.
Even if the dynamic control is created and bound to a datasource just no,w it will have the changed value given by user (which surprises me, I think because of the view state of the dynamic control. Correct me if am wrong).
I am using UGC feature of Tridion 2011. I have done almost but stuck in one place. actually, i have to display one text when user have entered comments, it will display if comments are greater then 0. i am using for this condition but it does not go in the condition.
<ugc:ItemStats ItemURI="#PhysicalUri" runat="server">
<ugc:Choose runat="server">
<ugc:When test=" ugcItemStats.numberOfComments equals 0 " runat="server">
html1
</ugc:When>
<ugc:Otherwise runat="server">
html2
</ugc:Otherwise>
</ugc:Choose>
</ugc:ItemStats>
Could any one please help me to resolve his issue
Use this code, i hope will be work.
<%
HttpContext.Current.Item["variable"] = 0;
%>
<ugc:ItemStats ItemURI="#PhysicalUri" runat="server">
<ugc:Choose runat="server">
<ugc:When test="ugcItemStats.numberOfComments equals variable " runat="server">
html1
</ugc:When>
<ugc:Otherwise runat="server">
html2
</ugc:Otherwise>
</ugc:Choose>
</ugc:ItemStats>
The When statement accesses the ugcItemStates object from the HttpContext.Current.Items collection.
I suspect that by nesting the statement in the ItemStats control causes this object not to be available yet.
I 'm trying to make a "login information" on the top panel, like "Welcome Back XXX", so I use
<% Response.Write(Session["username"]); %>
inside the aspx page.
It works, but is there anyway to use the variable directly without Response.Write here? It seems unnecessary.
There is a simple "shortcut" in the ASP.NET page syntax to Response.Write.
<%= Session["username"] %>
is functionally equivalent to
<% Response.Write(Session["username"]); %>
Typically you want to encode your session variables as HTML using Html.Encode, in case they contain characters which are not in the accepted HTML range. If you're using ASP.NET 4, you can use <%: %>, which is equivalent to Response.Write(Html.Encode(string)).
You can do it like this:
<%= Session["username"] %>
And if you use ASP.NET 4.0 you can automatically HTML encode the value by using this syntax:
<%: Session["username"] %>
put a asp.net label on your page, like
<asp:Label id=lblUserName runat="server" />
and on your codebehind page, on page_load event or on proper event
lblUserName.Text = String.Format("welcome back {0}",Session["username"]);
use a label and assign user name to it
In aspx (html code)
<asp:Label id=lblUserName runat="server" />
In aspx.cs (Code behind)
lblUserName .Text = "Welcome back"+Session["username"].ToString();
The correct way.
First is to check if the value is null
Second because you write it on a page, use the HTMLEncode to be sure that you avoid any type of injection, or problems.
now, if you like to use a Literal or a Label, or just direct write it, is up to you. If you going to place it inside an UpdatePanel you must use a Literal.
Now, if you use Literal avoid to set the ViewState to gain space from it, ether way you need to set it on PageLoad. And it will be
<asp:Literal runat="server" id="txtUserName" EnableViewState="false" />
and on page load.
if(Session["username"] != null)
{
Debug.Assert(Session["username"].ToString.Length > 0 , "Check out why I have zero user name");
txtUserName.Text = Server.HTMLEncode(Session["username"].ToString);
}
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.
i have a series of rows that are generated using an asp:repeater:
<asp:repeater ID="itemsRepeater"
OnItemDataBound="itemsRepeater_ItemDataBound"
runat="Server">
<itemtemplate>
<tr>
<td>
<asp:HyperLink ID="linkView" runat="server"
Text="<%# GetItemText((Item)Container.DataItem) %>"
NavigateUrl="<%# GetViewItemUrl((Item)Container.DataItem) %>" />
</td>
<td>
<asp:HyperLink ID="linkDelete" runat="server"
Text="Delete"
NavigateUrl="<%# GetDeleteUrl((ActionItem)Container.DataItem) %>" />
</td>
</tr>
</itemtemplate>
</asp:repeater>
The repeater creates an HTML table, with each row containing a link to an item and (what is essentially) a "Delete" link. The above simplified example code generates HTML similar to:
<TR>
<TD>
<A href="ViewItem.aspx?ItemGuid={19a149db-5675-4eee-835d-3d78372ca6f9}">
AllisonAngle_SoccerGirl001.jpg
</A>
</TD>
<TD>
Delete
</TD>
</TR>
Now that all works, but i want to convert the "Delete" to client side. i want to be able click the link and it will, on the client javascript:
prompt an alert "Are you sure..."
have javascript issue server-hit to actually delete the item they want
remove the item from the client DOM tree
So there are four problems to be solved:
How to hook up javascript to the client-side click of the Delete link.
How to know what item the user clicked Delete
Prevent a post-back
Delete the row that the user clicked
That is my question.
From here on you will find my rambling attempts to solve it. Don't take anything below as relating in any way to any possible accepted solution. Just because i posted code below, doesn't mean any of it is useful. And it doesn't mean i'm anywhere within spitting distance of the best solution. And because i can't make anything below work - it must have gone down the wrong road.
My Attempts
Wiring Up Javascript
The first task is to convert the delete link HTML from something such as:
<A href="DeleteItem.aspx?ItemGuid={19a149db-5675-4eee-835d-3d78372ca6f9}">
Delete
</A>
into something more javascripty:
<A href="#"
onclick="DeleteItem('DeleteItem.aspx?ItemGuid={19a149db-5675-4eee-835d-3d78372ca6f9}')">
Delete
</A>
and the add the script:
<script type="text/javascript">
//<![CDATA[
function DeleteItem(deleteUrl)
{
//Ask the user if they really want to
if (!confirm("Are you sure you want to delete INSERT NAME HERE?"))
{
return false;
}
//Call returns false if the server returned anything other than OK
if (!DoAjaxHit(deleteUrl)
{
return false;
}
//Remove the table row from the browser
var tableRow = document.getElementById("TODO-WHAT ID");
if (row != null)
{
//TODO: how to delete the tableRow from the DOM tree?
//document.Delete(tableRow) ?
}
//return false to prevent a postback
return false;
}
//]]>
</script>
What combination of ASP code can be used to create the above? i hear that asp:LinkButton has an OnClientClick event, where you can wire up a javascript function:
<asp:LinkButton ID="linkDelete" runat="server"
Text="Delete"
OnClientClick="DeleteItem(<%# GetDeleteUrl((ActionItem)Container.DataItem) %>);"/>
Problem is that the rendered HTML is literally containing:
<a onclick="DeleteItem(<%# GetDeleteUrl((ActionItem)Container.DataItem)) %>);" ...>
Delete
</a>
If i change the client click event handler to:
OnClientClick="DeleteItem('todo - figure this out');"/>
it works - as well as "todo - figure this out" can work.
Preventing Postbacks
The dummied down above javascript call actually happens (i can see my alert), but there's the next problem: Returning false from the javascript function doesn't prevent a postback. In fact, i can see that the href on the generated html code isn't "#", but rather
javascript:__doPostBack('ctl0....
i tried changing the ASPX code to include the OnClick handler myself:
OnClick="#"
OnClientClick="DeleteItem('todo - figure this out');"
But the compiler thinks the pound side is a pragma, and whines:
Preprocessor directives must appear as
the first non-whitespace character on
a line
Table Row Identity
The table rows don't have an ID, they're generated by the asp:repeater.
How can the javascript function know what triggered the click event? The javascript needs to be able to find the element, and remove it from the tree.
Note: i would of course prefer fade+collapse animation.
Normally you get an element by using
var tr = document.getElementById("the table row's id");
But the table rows don't have an easily knowable ID. Since there are multiple rows, the server generates the ID as it builds the table. i realize some solution is going to have to involve changing:
<TR>
into
<TR runat="server">
so that there will be server generated identity for each table row, but how do i reference the generated name from javsscript?
Normally i would have thought that the scripting problem would be solved by using multiple paramters:
function DeleteItem(tableRowElement, deleteUrl)
{
//Do a web-hit of deleteUrl to delete the item
//remove tableRowElement DOM object from the document tree
}
But the problem is just pushed elsewhere: How do you populate tableRowElement and deleteUrl for the call to the javascript function?
Such a simple problem: convert a click from a postback to client-side.
The volume of problems involved is getting quite idiotic. Which seems to indicate that either
the idea solution is something completely different
there is no solution
References
Stackoverflow: How do I fade a row out before postback?
Stackoverflow: Javascript before asp:ButtonField click
asp.net: Accessing repeater elements from javascript.
Stackoverflow: How to access repeater generated elements?
jQuery can dig out the tags for you:
$("[id$=linkDelete]").click(function() {
DeleteItem(this.href);
});
That code says "find all the DOM elements whose ID ends with 'linkDelete' and wire up the following click event handler".
I would recommend against implementing the Delete function through links in this way. Delete links are a security risk.
Rather, it's better to require a post to that url instead. If you want to be doing ajax, I would strongly recommend using a javascript framework so you don't have to deal with the differences in how different browsers implement XmlHttpRequests.
For instance, in jQuery you could do it like this:
$.post('Delete.aspx',{itemGuid:'{19a149db-5675-4eee-835d-3d78372ca6f9}'},
function(data, textStatus) {
if (textStatus == 'success') {
$(this).parents('tr:eq(0)').fadeOut();
}
});
which would do both the ajax call and the fadeout effect you want.
You could then access the item guid in Delete.aspx from Request.Form["itemGuid"], and this wouldn't be vulnerable to link attacks.
Preventing Postbacks
The server is generating a postback wireup because you're using a server control. Use a plain <a> tag without a runat='server' directive.
Table Row Identity
I usually do this by databinding an ID column of some kind and putting this in the repeater template:
<tr id='<%#Eval("ID")%>'>
P.S. I hate to sound like a fanboy, but jQuery will make all of these things an order of magnitude easier. You should really consider it if you can. Otherwise, you're going to be in a world of hurt trying to implement these features in a consistent way across browsers.
P.P.S. If the Delete.aspx and similar urls are only going to be called from javascript, I would recommend using ashx http handlers instead. You can do all of the same server logic without the needless overhead of a full-blown page.
mine usually comes out looking like
<asp:LinkButton runat="server" OnClientClick='<%# Eval("ID", "DeleteItem(this, \"{0}\"); return false;") %>' Text="Delete" />
that will make html that looks like
<a id="blah_blah_ctl01" onclick='DeleteItem(this, "{19a149db-5675-4eee-835d-3d78372ca6f9}"); return false;'>Delete</a>
I include the "this" reference so that you can have access to the dom and delete the link...or it's parent or whatever. From there it's pretty straight forward to use jQuery to actually do the posting and DOM manipulation. The "return false;" disables the postback.
The other answerers found various bits related to different aspects of the question. i managed to cobble together a complete solution. i've copy/pasted the relavent snippits here.
The first important change is the use of an asp:LinkButton which allows as OnClientClick event, which gives you direct access to the javascript OnClick event:
<asp:repeater ID="itemsRepeater"
OnItemDataBound="itemsRepeater_ItemDataBound"
runat="Server">
<itemtemplate>
<tr>
<td>
<asp:HyperLink ID="linkView" runat="server"
Text="<%# GetItemText((Item)Container.DataItem) %>"
NavigateUrl="<%# GetViewItemUrl((Item)Container.DataItem) %>" />
</td>
<td>
<asp:LinkButton ID="linkImpregnate" runat="server"
Text="Impregnate"
OnClientClick="<%# GetImpregnateUrl((Item)Container.DataItem) %>" />
</td>
</tr>
</itemtemplate>
</asp:repeater>
The code-behind manually builds presentation code (yay for separation of controller and view!) that contains a javascript call.
protected string GetNodeAcknowledgementClientClick(Item item)
{
if (!(item is HotGirl))
return ""; //this shouldn't be called for non-acknowledgements, but i won't fail
HotGirl girl = (HotGirl)item;
String szOnClientClick =
"return ImpregnateGirl(this, "+
Toolkit.QuotedStr(girl.GirlGUID.ToString()) + ", "+
Toolkit.QuotedStr(GetItemName(item))+");";
return szOnClientClick;
}
And finally in the aspx, i find a random spot to mash in some javascript:
<script type="text/javascript" src="Javascript/jquery.js"></script>
<script type="text/javascript">
//<![CDATA[
function DeleteItem(sender, girlGuid, girlName)
{
if (!confirm("Are you sure you want to impregnate "+girlName+"?"))
{
return false;
}
$.post(
'ImpregnateGirl.aspx?GirlGUID='+nodeGuid,
function(data, textStatus) {
if (textStatus == 'success')
{
$(sender).parents('tr:eq(0)').hide();
}
else
{
return false;
}
}
);
//return false to suppress the postback
return false;
}
//]]>
</script>
i would have made the jQuery do a post, as a security measure as guy suggested, but jQuery would give an error, rather than posting. Rather than care i chose to not care.