Posts Tagged ‘asp.net’
Error:BC30456: ‘CreateResourceBasedLiteralControl’ is not a member of
Posted by: admin in .NET, ASPX Error on August 22nd, 2011
I’m getting this error after I’ve published an aspx application to the webserver. After a lot of research I’ve found a fix that works for me, this is to change the class visibility of the aspx.vb files from Private to Public.
How to refresh parent page
-
Dim reloadParentPage As String = "window.opener.location=window.opener.location;"
-
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "nothing", reloadParentPage, True)
How to select a row in GridView
-
<asp:GridView ID="GridViewCategory" runat="server" AutoGenerateColumns="False"
-
DataKeyNames="CategoryID"
-
EmptyDataText="There are no data records to display."
-
CssClass = "GridViewStyle"
-
AlternatingRowStyle-CssClass="alt"
-
PagerStyle-CssClass="pgr"
-
SelectedRowStyle-CssClass="selected"
-
Width="100%" onrowdatabound="GridViewCategory_RowDataBound"
-
>
-
<Columns>
-
<asp:TemplateField HeaderText="ID">
-
<ItemTemplate>
-
<asp:LinkButton ID="lnkSelect" runat="server" CommandName="Select" CommandArgument='<%# Bind("CategoryID") %>' Text='<%# Bind("CategoryID") %>'></asp:LinkButton>
-
</ItemTemplate>
-
<ItemStyle Width="50px" />
-
</asp:TemplateField>
-
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
-
</Columns>
-
<PagerStyle CssClass="pgr" />
-
<AlternatingRowStyle CssClass="alt" />
-
</asp:GridView>
Code behind:
-
protected void GridViewCategory_RowDataBound(object sender, GridViewRowEventArgs e)
-
{
-
if (e.Row.RowType == DataControlRowType.DataRow)
-
{
-
LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[1];
-
-
e.Row.Attributes.Add("onClick", ClientScript.GetPostBackClientHyperlink(lb, ""));
-
e.Row.Attributes.Add("onMouseClick", "javascript:this.style.cursor='pointer';");
-
e.Row.Attributes.Add("onMouseOut", "javascript:this.style.cursor='normal';");
-
}
-
}
or
if you did not put a SELECT command button in the gridview, you can use the following code to mimic the Select function
-
protected void GridViewCategory_RowDataBound(object sender, GridViewRowEventArgs e)
-
{
-
if (e.Row.RowType == DataControlRowType.DataRow)
-
{
-
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
-
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
-
-
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridViewCategory, "Select$" + e.Row.RowIndex);
-
}
-
}
Namespace or type specified in the project-level Imports ‘System.Xml.Linq’ doesn’t contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn’t use any aliases
There are 2 ways to remove this warning
First, you may need to convert the current .Net Framework used by the application to higher version, let’s say from .net framework 2 to 3.5
- Goto Property Pages of the web site project
- View Menu->Property Pages or press shift+F4
- Click the Build option from the left side
- Make sure the Target Framework is .Net Framework 3.5
Second, remove the namespace that shown in the warning in this case the “System.Xml.Linq” from the web.config
Session state can only be used when enableSessionState is set to true
Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration.
This error usually occurs when you have declared an object variable public to the page and it access a Session variable, example:
Partial Class _Default
Inherits System.Web.UI.Page
Private m As New DBAccess(Session(“dbname”))
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
End Sub
End Class
How to check if the ASP.Net Session has expired
-
protected void OnInit(EventArgs e)
-
{
-
base.OnInit(e);
-
//It appears from testing that the Request and Response both share the
-
// same cookie collection. If I set a cookie myself in the Reponse, it is
-
// also immediately visible to the Request collection. This just means that
-
// since the ASP.Net_SessionID is set in the Session HTTPModule (which
-
// has already run), thatwe can't use our own code to see if the cookie was
-
// actually sent by the agent with the request using the collection. Check if
-
// the given page supports session or not (this tested as reliable indicator
-
// if EnableSessionState is true), should not care about a page that does
-
// not need session
-
if (Context.Session !=null)
-
{
-
//Tested and the IsNewSession is more advanced then simply checking if
-
// a cookie is present, it does take into account a session timeout, because
-
// I tested a timeout and it did show as a new session
-
if (Session.IsNewSession)
-
{
-
// If it says it is a new session, but an existing cookie exists, then it must
-
// have timed out (can't use the cookie collection because even on first
-
// request it already contains the cookie (request and response
-
// seem to share the collection)
-
string</span> szCookieHeader <span>=</span> Request.Headers["Cookie"];
-
if((null!= szCookieHeader) (szCookieHeader.IndexOf("ASP.NET_SessionId")<= 0))
-
{
-
Response.Redirect("sessionTimeout.htm");
-
}
-
}
-
}
-
}
Source
Eval in ASP.net
Posted by: admin in .NET, Data Binding on June 24th, 2010
Dealing with complex Eval() expressions inside of ItemTemplate or other data containers always makes me do a double take. Here are a few observations and thoughts on how to handle and possibly improve handling of this feature in the future.
So, a typical scenario is embedding controls inside of an item template. The template loops through the datasource and you now want to embed values from the datasource into the child controls of the ItemTemplate.
For example:
<asp:repeater id=”rptSpecials” runat=”server”>
<itemtemplate>
<asp:HyperLink runat=”server”
NavigateUrl=’~/item.aspx?sku=<%# Eval(“sku”) %>‘
Text=’<%# Eval(“specialhd”) %>‘/>
</itemtemplate>
</asp:repeater>
Quick what does that give you?
Not what you might expect:
<a href=”item.aspx?sku=<%# Eval("sku") %>”>West Wind Html Help Builder 4.02</a>
It’s slightly inconsistent isn’t it? Text expands just fine with the Eval expression, but the NavigateUrl() doesn’t.
Now can anybody spot why this is happening?
If you change the NavigateUrl expression to:
NavigateUrl=’item.aspx?sku=<%# Eval(“sku”) %>‘
it turns out it actually works. The problem is that that ~/ is causing ASP.NET to call ResolveUrl() which takes the whole string and mucks up the string. Take the ~ out and ASP.NET no longer calls ResolveUrl and it actually works.
Now it seems to get this to work anyway is this:
<asp:HyperLink runat=”server”
NavigateUrl=’<%# Request.ApplicationPath %>/item.aspx?sku=<%# Eval(“sku”) %>‘
Text=’<%# Eval(“specialhd”) %>‘/>
But that results in the following error:
Compiler Error Message: CS1040: Preprocessor directives must appear as the first non-whitespace character on a line
Source Error:
|
|
|
Line 7: <tr> Line 8: <td valign=”top”> Line 9: <asp:HyperLink runat=”server” Line 10: NavigateUrl=’<%# Request.ApplicationPath %>/item.aspx?sku=<%# Eval(“sku”) %>’ Line 11: Text=’<%# Eval(“specialhd”) %>’/> |
Source File: c:\projects2005\wwstore\UserControls\SpecialsListing.ascx Line: 9
If you add a character (say a<%# …%> then it works). I have no idea why this would be a problem for ASP.NET to parse, all I know is it doesn’t work.
I suspect there’s a bug in the ASP.NET parser in this situation, especially for the first case where apparently ASP.NET is calling ResolveUrl before it’s doing the eval on the embedded string.
With all this sort of headache ultimately it’s easier to just write out the HREF manually:
<a href=”<%# Request.ApplicationPath %>“/item.aspx?sku=<%# Eval(“sku”) %>“>
<img src=”<%# Request.ApplicationPath %>/itemimages/sm_<%# Eval(“Itemimage”) %>“ />
</a>
I bring this up because this sort of thing seems to happen quite frequently. I really wish there was better support for assigning dynamic values. Maybe something using FormatStrings that could be replaced with some dynamic properties:
NavigateUrl=”~/MyPage/MyPage?Id={0}” Text=”SomeText {1}”
Parameter0=”<%# Eval(“Sku”) %>
Parameter1=”<%# Eval(“Company”) %>“
It may seem like this is overkill but I find myself running into situations quite frequently where the single string delimiter makes it near impossible to create a clean expression in script code. The above solution would solve that problem in all situations because you’d eliminate the need to nest the Eval expression with its string delimiter and you get back the use of two string delimiters.
There are workarounds today, they’re just not quite as declarative. The above behavior could be simulated with something like this:
NavigateUrl=’<%# this.ResolveUrl(“~/” + string.Format(“item.aspx?sku={0}”,Eval(“sku”)) %>‘
Still that doesn’t really get around the string delimiter issue. Try this:
NavigateUrl=’<%# this.ResolveUrl(“~/” + string.Format(“onclick=DoItem(‘{0}’);”,Eval(“sku”))%>‘
There’s the problem with the string nesting. Remove the Eval() completely from this expression would fix this problem.
Today my workaround for complex expressions that just don’t want to work is to create a method on the page or control that returns the value which is workable, but not a solution the average novice will think of.
<asp:HyperLink runat=”server”
NavigateUrl=’<%# this.GetItemUrl( Eval(“sku”) as string ) %>‘
Text=’<%# Eval(“specialhd”) %>‘/>
where GetItemUrl() simply does all of the formatting for the URL.
The event of the Dynamic creation of the control does not work
You need to recreate the created control and attach the events every page load.
How to set default document using web.config
To set the default document of the web site using web.config, add the following lines inside the system.webServer.
-
<defaultDocument>
-
<files>
-
<clear />
-
<add value="myhomepage.aspx" />
-
</files>
-
</defaultDocument>
