How do I set the value of primaryKey attribute in a gridview aspx markup bases on condition?
<%
string val=string.Empty;
if(Id=1){
%>
val="red";
<% else { %>
val="blue";
<%} %>
<GridView runat="server" id="someid" PrimaryKey=val />
Your code have issues, you need to fix that.
Issue 1
if(Id=1){ Not correct , it should be if(Id==1){
Issue 2:
Propriety should not be PrimaryKey it should be DataKeyNames
Issue 3:
<GridView runat="server" id="someid" PrimaryKey=val />
It should be
<asp:GridView runat="server" id="someid" />
Instead of using a variable and using that for setting, you can do it like following.
<%
string val = string.Empty;
if (Id == 1)
{
someid.PrimaryKey = "red";
}
else
{
someid.PrimaryKey = "blue";
}
%>
<asp:GridView runat="server" id="someid" />
From an aspx page, I am trying to display a user control for each item in a collection, but the C# seems to be ignored when tryign to set the UserControl parameter:
<%foreach (Fetus item in this.pregnancy.Fetus) {%>
//this returns a GUID:
"<%= item.Id.ToString() %>"
//this does not work, returns the characters between "" like < %= item.Id.ToString()%>:
<uc1:AntepartumCTGChart runat="server" ID="AntepartumCTGChart" FetusId="<%= item.Id.ToString()%>" />
<% } %>
I would expect this to work, what's wrong?
You have to use a data binding expression
<uc1:AntepartumCTGChart runat="server" ID="AntepartumCTGChart" FetusId='<%# item.Id.ToString()%>' />
But you have to call DataBind() in code behind for that to work.
You can also use a Repeater
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<uc1:AntepartumCTGChart runat="server" ID="AntepartumCTGChart" FetusId='<%# Eval("id").ToString()%>' />
</ItemTemplate>
</asp:Repeater>
And then bind data to it in code behind
Repeater1.DataSource = pregnancy.Fetus;
Repeater1.DataBind();
i got the following code on my ASP-Site
<asp:Repeater runat="server" ID="repFoo">
<ItemTemplate>
<asp:Button runat="server" ID="btnfoo" Visible='<%#!String.IsNullOrEmpty("FOOValue")%>' />
</ItemTemplate>
</asp:Repeater>
how is the correct syntax for the String.IsNullOrEmpty method?
create a c# method--->
public string CheckIfNull(object myValue)
{
if (myValue == null)
{
return "0 value";
}
return myValue.ToString();
}
.aspx--->
... Visible = <%# CheckIfNull(Eval("FOOValue")) %> ...
there are 2 ways :
declare function in your cs file that get the value and make your checks on it:
<asp:Button runat="server" ID="btnfoo" Visible='<%# CheckNull(Eval("FOOValue")) %>' />
public bool CheckNull(object value)
{
return string.IsNullOrEmpty(value) ? fale : true;
}
OR use the function IsNullOrEmpty inline :
<asp:Button runat="server" ID="btnfoo" Visible='<%# String.IsNullOrEmpty(Eval("FOOValue").ToString()) ? false : true %>' />
I'm having some trouble calling a property or a method on a databound string object inside of a ListView. See this example:
<asp:ListView runat="server" ID="FullInfoListView">
<LayoutTemplate>
<table class="tablestripe" width="100%">
<asp:Placeholder runat="server" ID="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr valign="top" runat="server" Visible='<%# !string.IsNullOrEmpty((string)Eval("Phone")) %>'>
<td><strong>Phone:</strong></td>
<td><span runat="server" Visible='<%# ((string)Eval("Phone")).Length == 4 %>'>x</span><%# Eval("Phone") %></td>
</tr>
</ItemTemplate>
</asp:ListView>
This line:
<%# ((string)Eval("Phone")).Length == 4 %>'>
is what is giving me trouble. If I remove the .Length everything works fine. If I leave it in there my code throws an "Object reference not set to an instance of an object." exception on the line where I call the DataBind() method on the ListView in my code behind. This same behavior occurs with .ToLower() as well.
EDIT
I think I got this figured out. Thanks for those of you who suggested moving this out to a method in the code behind to help with debugging. The problem was related to a null reference...go figure :) I thought that if the table row wasn't visible that none of the logic inside would get evaluated, but I think due to the fact that it is databound the logic is evaluated anyway. So simply changing the above line to the following fixed the problem:
<%# Eval("Phone") != null && ((string)Eval("Phone")).Length == 4 %>'>
I would consider using a Label instead of a <span>. Instead of casting it to string, just use the ToString() function instead, and wrap the entire expression in parentheses to ensure that it's evaluating as a boolean:
<asp:Label ID="Label1" runat="server" Visible='<%# (Eval("Phone").ToString().Length >= 4) %>' Text="X" />
If the above doesn't fix your problem, you can always add a method in the code behind to do this:
<asp:Label ID="Label1" runat="server" Visible='<%# CheckLength(Eval("Phone").ToString()) %>' Text="X" />
Code-behind:
public bool CheckLength(string value)
{
return value.Length >= 4;
}
Move the compound code to code-behind method. For example the
'<%# ((string)Eval("Phone")).Length == 4 %>'
becomes
'<%# IsPhoneSpanVisible( (string)Eval( "Phone" ) ) %>'
with
protected bool IsPhoneSpanVisible( string Phone )
{
// provide your logic here
}
This way you'll be easily able to debug your code.
If dataitem is Null I want to show 0
<asp:Label ID="Label18" Text='<%# Eval("item") %>' runat="server"></asp:Label>
How can I accomplish this?
You can also create a public method on the page then call that from the code-in-front.
e.g. if using C#:
public string ProcessMyDataItem(object myValue)
{
if (myValue == null)
{
return "0 value";
}
return myValue.ToString();
}
Then the label in the code-in-front will be something like:
<asp:Label ID="Label18" Text='<%# ProcessMyDataItem(Eval("item")) %>' runat="server"></asp:Label>
Sorry, haven't tested this code so can't guarantee I got the syntax of "<%# ProcessMyDataItem(Eval("item")) %>" entirely correct.
I'm using this for string values:
<%#(String.IsNullOrEmpty(Eval("Data").ToString()) ? "0" : Eval("Data"))%>
You can also use following for nullable values:
<%#(Eval("Data") == null ? "0" : Eval("Data"))%>
Also if you're using .net 4.5 and above I suggest you use strongly typed data binding:
<asp:Repeater runat="server" DataSourceID="odsUsers" ItemType="Entity.User">
<ItemTemplate>
<%# Item.Title %>
</ItemTemplate>
</asp:Repeater>
I use the following for VB.Net:
<%# If(Eval("item").ToString() Is DBNull.Value, "0 value", Eval("item")) %>
It should work as well
Eval("item") == null?"0": Eval("item");
Moreover, you can use (x = Eval("item") ?? 0) in this case.
http://msdn.microsoft.com/en-us/library/ms173224.aspx
I don't know ASP.NET very well, but can you use the ternary operator?
http://en.wikipedia.org/wiki/Ternary_operation
Something like:
(x=Eval("item")) == Null ? 0 : x
Use IIF.
<asp:Label ID="Label18" Text='<%# IIF(Eval("item") Is DBNull.Value,"0", Eval("item") %>'
runat="server"></asp:Label>
try this code it might be useful -
<%# ((DataBinder.Eval(Container.DataItem,"ImageFilename").ToString()=="") ? "" :"<a
href="+DataBinder.Eval(Container.DataItem, "link")+"><img
src='/Images/Products/"+DataBinder.Eval(Container.DataItem,
"ImageFilename")+"' border='0' /></a>")%>
Used a modified version of Jason's answer:
public string ProcessMyDataItem(object myValue)
{
if (myValue.ToString().Length < 1)
{
return "0 value";
}
return myValue.ToString();
}
Try replacing <%# Eval("item") %> with <%# If(Eval("item"), "0 value") %> (or <%# Eval("item") ?? "0 value" %>, when using C#).
I have tried this code and it works well for both null and empty situations :
'<%# (Eval("item")=="" || Eval("item")==null) ? "0" : Eval("item")%>'
You can use the following for VB.net, especially if the value is a boolean:
<%# IIf(Eval("mydata").Equals(DBNull.Value), 0, Eval("mydata"))%>
For example, use this to automatically check or uncheck a checkbox with inline IIF eval:
<asp:CheckBox ID="mycheckbox" runat="server" Checked='<%# IIf(Eval("mydata").Equals(DBNull.Value), 0, Eval("mydata"))%>' />
The other method with .ToString will cause an error trying to convert the DBnull to a boolean.