Using Methods in Serverside ControlIDs - asp.net

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 %>' />

Related

Why is the content inside the invisible asp:PlaceHolder rendered?

Why is the content inside the placeholder rendered?
This code results in: "Object reference not set to an instance of an object."
For the MainGuard object!
How should one handle this situation?
<asp:PlaceHolder runat="server" Visible="<%# Model.MainGuard != null %>">
<asp:Image runat="server" ImageUrl="<%# Model.MainGuard.Image.RenderImage() %>" Height="50" />
<%# Model.MainGuard.Name %>
</asp:PlaceHolder>
It's not rendered -- but it still has to be parsed by the runtime, hence you still get the exception. Your only recourse is to check for null each time:
<asp:Image runat="server"
ImageUrl="<%# Model.MainGuard == null ? "" : Model.MainGuard.Image.RenderImage() %>" />
<%# Model.MainGuard == null ? "" : Model.MainGuard.Name %>
You might consider using an extension method to allow for cleaner syntax:
public static string StringOrEmpty(this MyClass self, Func<MyClass, string> selector)
{
if (self == null) return "";
return selector(self);
}
Then you can write:
<asp:Image runat="server"
ImageUrl="<%# Model.MainGuard.StringOrEmpty(mainGuard => mainGuard.Image.RenderImage()) %>" />
<%# Model.MainGuard.StringOrEmpty(mainGuard => mainGuard.Name) %>

Visible false of button in gridview while data binding? In Asp.net?

In web application, I am going to disable the button while binding data, is it possible visibling false like this?
<asp:Button ID ="btn" runat ="server" CommandArgument='<%# Eval("id").ToString() == "1"? visble false: Visible true %>' />
Try this:
<asp:Button ID="Button1" runat="server" Text="Button" Visible='<%#getVisibility()%>' OnClick="btn_Click" />
Code Behind:
public Boolean getVisibility()
{
Boolean b = false;
//get the boolean value based on your field condition
b = Convert.ToBoolean(Eval("FieldValue").ToString() != "MatchCondition" ? "true" : "false");
return b;
}
Do it in the DataGrid RowDataBound event
Either do that in the data bound event or do it like this...
<asp:Button ID ="btn" runat ="server" Visible='<%# Eval("id").ToString() == "1"? "false" : "true" %>' />

populate lable control inside a gridview based on inline Conditional statement

I am trying to display a plain text in the column(contains a label) of gridview based on a condition. Here is my erroneous code. Please correct.
<asp:Label ID="lblAsgn" runat="server" Text= '<%#Eval("StatusId") == 0 ? "NEW" : "OLD" %>' > </asp:Label>
Thanks in advance.
BB
<asp:Label
ID="lblAsgn"
runat="server"
Text='<%# FormatText(Eval("StatusId")) %>' />
where FormatText could be a method in your code behind:
protected string FormatText(object o)
{
int value;
if (int.Parse(o as string, out value) && value == 0)
{
return "NEW";
}
return "OLD";
}
Try this :
<asp:Label ID="lblAsgn" runat="server" Text= '<%# Eval("StatusId").Equals(0) ? "NEW" : "OLD" %>' > </asp:Label>

calling a function in repeater asp.net

I want to call a function to bind data to Repeater . Do I need to Set dataSource Property of this control or Repeater .DataBind() will work.
<asp:Repeater ID="RepeaterDays" runat="server">
<ItemTemplate>
<ul>
<asp:Label ID="lblDays" runat="server" Text='<%#ChandanIdiot()%>'></asp:Label>
</ul>
</ItemTemplate>
</asp:Repeater>
I have written RepeaterDays.Databind(), but the function is not called.
This is displaying nothing.
Is ChandanIdiot() a protected function that returns a string?
protected string ChandanIdiot() {
return "test";
}
If you want to actually do some data processing, you will have to include a parameter:
protected string ChandanIdiot(object obj) {
return "test " + obj;
}
And, assuming that there is a property called "Name" on the object that you are reapeating, you would have the following:
<asp:Label ID="lblDays" runat="server" Text='<%# ChandanIdiot(Eval("Name")) %>' />
Source:
<asp:TemplateField HeaderText="Unit Price">
<ItemTemplate>
<%# ChandanIdiot( Eval("product_unitprice"))%>
<!--product_unitprice is table colomn name -->
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
C#:
protected string ChandanIdiot(object ob) {
string typ = ob.ToString(); //selected value stored in ob
if (typ == "some function") {
//do somthing
}
return typ ; //value return to <%# ChandanIdiot( Eval("product_unitprice"))%>
}

gridview dynamic image change in imagebutton

I have a gridview containing some data from db, and after a check I want to see a small cross/tick image in each row, due to the result of the check.How can I change the image url dynamically?
You could either use inline statement like
<%#Eval("check").ToString() == "1" ? "images/checked.gif" : "images/unchceked.gif")%>
or use a function to get the result as follows:
<%# getImageUrl(Eval("value")) %>
Public Function getImageUrl(ByVal value As Integer) As String
If value = 0 Then
Return "images/unchceked.gif"
Else
Return "mages/checked.gif"
End If
End Function
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="check" runat="server" ImageUrl='<%#If(Eval("check") = 1,"images/checked.gif","images/unchceked.gif") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
in form:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="check" runat="server" ImageUrl='<%# GetImageUrl(Eval("Check")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
code-behind:
public string GetImageUrl(object checkObject)
{
if (checkObject!= null)
{
bool check;
bool parsable = bool.Parse(checkObject.ToString(), out check);
check= parsable ? check : false;
return check ? "~/Media/Images/tick.png" : "~/Media/Images/untick.png";
}
return "~/Media/Images/none.png";
}

Resources