Developers, I am in need of providing different
<%# Eval("variable") %>
For my Grid. I have requirements that require me to either show StagingDB Data or ProdDB Data per Eval
Pseudocode
<%# If Eval("isDraft) ? Eval("tbl_staging_value") : Eval("tbl_value") %>
Stackoverflowers... You are my only hope... Thanks. Working on Sunday...
You can use ?: operator.
<%# Convert.ToBoolean(Eval("isDraft")) ? Eval("tbl_staging_value"):
Eval("tbl_value") %>
By creating a new property on your data source
public string tbl_val
{
get
{
return isDraft ? tbl_staging_value : tbl_value;
}
}
you can simplify the data binding
<%# Eval("tbl_val") %>
Related
Given this markup in an ascx file :
<div class="DocumentPara">
<%#Eval("Content1").ToString%>
<%#Eval("Content2").ToString%>
</div>
Is there a syntax I can use to chose the display of "Content1" and "Content2" depending of which masterpage is calling?. I.e. :
<div class="DocumentPara">
<%#Eval("Content1").ToString%>
<If masterpage1>
<%#Eval("Content2").ToString%>
</endIf>
<If masterpage2>
<%#Eval("Content3").ToString%>
</endIf>
</div>
Thanks for your help.
Page's masterpage can be accessed via Master property, so to check for a specific masterpage in use you can do something like
if (this.Master is Master1Type)
The if/else syntax is also possible, and will look like this:
<% if (this.Master is Master1Type) { %>
<%#Eval("Content2").ToString%>
<% }
else { %>
<%#Eval("Content3").ToString%>
<% } %>
However that looks dirty to me, and having conditionals likes this inside the page markup is not a common practice. I would suggest defining a function in code behind to deal with masterpages logic, and output necessary value from the data item:
<%# GetContent(Container.DataItem) %>
protected string GetContent(object dataItem)
{
if (this.Master is Master1Type)
{
return Eval("Content2");
}
// etc
}
I'd like have the following type of code fragment in a .aspx page:
<% foreach( int i in <some-expression> ) { %>
<asp:linkbutton CommandArgument="<value-of-i>" OnClick="<some-handler>" runat="server" />
<$ } %>
Basically, I want to generate the page sans data binding, but supply the value of CommandArgument to the linkbutton from a variable in the code behind, so that when the click handler activates, I can retrieve the value of the variable from the CommandArgument, that existed when the code ran.
I do not want to use data binding, for various reasons. Is this possible? I understand that the <%: ... %> syntax is equivalent to Response.Write, and that the syntax <%# %> retrieves values which are databound. I also know that I can't use the <%: %> syntax here to solve my problem. And because I am not using data binding, I can't use the <%# %> syntax.
It seems to me that something like this should be very basic.
So, to answer my own question, there seems to be no way to mix server side controls and state generated from the code around them. The reason being that server side controls are generated before the code runs, and the code, for the most part, runs to emit the response (think Response.Write).
However, one can "manually" cause the post back that server side controls, like asp:LinkButton, perform with a java script call to __doPostBack. So the solution to my problem is something like the following:
<% for( int i = 0; i < 10; i++ ) { %>
<a href="" onclick='javascript:__doPostBack( "MeClicked", "<%: i %>" ); return false; '>Click Me <%: i %></a>
<% } %>
Where Page_Load does something like::
if ("MeClicked".Equals( Request.Form[ "__EVENTTARGET" ] )) {
string arg = Request.Form[ "__EVENTARGUMENT" ];
Response.ClearContent();
Response.Write( arg + " Clicked" );
Response.End();
return;
}
I'm sure this is trivial, but why isn't the Windows Authentication user printing in my ASP.NET page inline?
Code behind function:
public string GetCurrentUserWindowsLogin()
{
string windowsLogin = Page.User.Identity.Name;
int hasDomain = windowsLogin.IndexOf(#"\");
if (hasDomain > 0)
{
windowsLogin = windowsLogin.Remove(0, hasDomain + 1);
}
return windowsLogin;
}
Inline code:
<div class="loginDisplay">[ <%#GetCurrentUserWindowsLogin() %> ]</div>
The <%#... %> is used for Binding Expressions like Eval and Bind.
So if you call Page.DataBind() in page_load it should work.
Another way that should work is to use code render blocks which run normal code:
<% GetCurrentUserWindowsLogin() %>
or the <%= %> construct used for small chunks of information:
<%= GetCurrentUserWindowsLogin() %>
Just a follow up on the above answer, the <%= is like response.write.
http://msdn.microsoft.com/en-us/library/vstudio/6dwsdcf5(v=vs.100).aspx
I am doing this:
<% Html.RenderPartial("SomePartial", Model); %>
My Model has a property UserID
In my partial I try this:
<%= Model.UserID %>
but I get this error:
CS1061: 'object' does not contain a definition for 'UserID' and no extension method 'UserID' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Blankman,
Make sure your partial ascx file defines the model as per the main view i.e:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Your.Model>" %>
[edit] - as Stefanvds mentions below, if you only need the id portion from the model, then define your partial as:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int>" %>
that would be the definitive i reckon :)
cheers
Make sure that at the top of your partial view you have Inherits attribute of your Control tag set to the type of the model you're passing in:
Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.MyStronglyTypedModel>"
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DB.Models.TheModelYouWantToSend>" %>
your partial should start with this.
also, if you want to send the whole model, you dont need to specify it.
<% Html.RenderPartial("SomePartial"); %>
or if you would only like to send the ID.
<% Html.RenderPartial("SomePartial", Model.UserID); %>
which would make the header
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int>" %>
The model type on your Partial View and the model you pass in to Html.RenderPartial method should match.
The way this page is laid out, all of the data is loaded at Page_Init. Well, I have a custom control that is having problems with this though.
I have it on the page like so:
<cc:SomeControl... />
And then I set the value at Page_Init using
MyControl.Value="blah";
Simple stuff..
The Value is an accessor and has something similar to this:
public string Value{
get...
set{
EnsureChildControls();
MyHiddenField.Value=value;
}
}
and it is here that I have a problem. It says that MyHiddenField is null. Is Page_Init just too early for this? Or is there some other function I need to call?
The fix for this was changing from using a namespace to reference the CustomControl to using a src with a filename
changing this:
<%# Register Assembly="MyProduct" Namespace="MyProduct.CustomControls" TagPrefix="cc" %>
to this:
<%# Register src="/CustomControls/MyControl.ascx" tagname="MyControl" tagprefix="uc2" %>