I have a DataList which I am binding on load, which works perfectly fine. My question is how do I make this to show records that don't have a date in database, in different text colour? here is my code:
<asp:DataList ID="dlS" runat="server" EnableViewState="false">
<ItemTemplate>
<asp:Label ID="Label" runat="server" Text='<%# Eval("Name") %>' /><br />
</ItemTemplate>
</asp:DataList>
Guid ID = (Guid)Session["ID"];
lstL = Manager.Get_ByID(ID);
if (lstLetters != null)
{
dlS.DataSource = lstL;
dlS.DataBind();
}
I'm not sure what you really mean by "records that don't have a date in database",
but if it means that those records have NULL value as date then you can rewrite the Label as below:
<asp:Label ID="Label" runat="server" Text='<%# Eval("Name") %>' ForeColor='<%# Eval("DateValue") == System.DBNull ? System.Drawing.Color.Red : System.Drawing.Color.Blue %>' />
Related
My dataset return this table link.How can i use If else conditons in aspx page?I want if the File_path is Null or empty then image not show because if File_Path is empty then it give me error,I want if File_Path is not empty then image will be show.Following error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Here is my code
<asp:DataList ID="DataList2" runat="server" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" RepeatLayout="Table">
<ItemTemplate>
<div class="<%#GetStyleForMsgList(Eval("MsgSender").ToString()) %> MainChatListClass">
<asp:Label ID="Label1" runat="server" Text='<%# GetPerfactName(Eval("MsgSender").ToString()) %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("ChatMsg") %>'></asp:Label>
<asp:Label ID="lblfile" runat="server" Visible ="false" Text='<%# Eval("File_Path") %>'></asp:Label>
<% if (Eval("File_Path") != null)
{%>
<asp:Image ID ="img" runat ="server" ImageUrl ='<%# Eval("File_Path") %>' />
<% } %>
</div>
</ItemTemplate>
</asp:DataList>
I don't understand why I am getting alert "Empty" when I type some numbers or leave a empty? Can you help me?
My Code:
</script>
<script type="text/javascript">
function tell() {
var v = true;
if (document.getElementsByName("TextBox33").length == 0)
alert("Empty");
v = false;
return v;
}
</script>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert" OnClientClick="javaScript: return tell();"
<asp:TextBox ID="TextBox33" onkeypress="return isNumberKey(event)" runat="server" Text='<%# Bind("imei") %>'></asp:TextBox>
I am adding more html:
<asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Insert" AutoGenerateRows="False" DataKeyNames="id" DataSourceID="SqlDataSource1" Height="50px" Width="286px" OnItemInserting="DetailsView1_ItemInserting" OnDataBound="DetailsView1_DataBound" OnItemInserted="DetailsView1_ItemInserted">
<asp:TemplateField HeaderText="IMEI" SortExpression="imei">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("imei") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox33" onkeypress="return isNumberKey(event)" CssClass="TextBox33" runat="server" Text='<%# Bind("imei") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("imei") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
You're calling getElementsByName which returns al array of DOM elements. You're then taking the length of that, which will always be more than 0 because the element exists.
Try adding a [0] after the getElementsByName call, and then change .text to .value.
Also so you're aware, you need to put the alert and v = false inside brackets. You can only skip brackets if you're executing one statement.
Here's the revised code:
if (document.getElementById("TextBox33").value.length == 0){
alert("Oh no I'm empty!");
v = false;
}
Update: I also just noticed that you didn't actually give the textbox a name of TextBox33, you gave it an ID. I've updated the code.
I have some labels inside a DataList which are populated with text from as the DataList is binded with database. Now i want to place the value of Text of asp.net with Session variable.
<asp:DataList ID = "dl_cmt" runat="server">
<ItemStyle CssClass="coment" />
ItemTemplate>
<asp:Label ID="ll" runat="server" Text='<%# %>' />
<asp:Label ID="lblcmt" runat="server" Text='<%#Eval("ecomment")%>' />
<asp:Label ID="lblDate" style=" color:brown; font-family:Cursive; font-size:x-small; " runat="server" Text='<%#Eval("my_date","on {0}") %>' />
</ItemTemplate>
</asp:DataList>
I want to place the text of label ID="ll" with session["userid"]
You can get the value as follows
ll.Text = Session["userid"].ToString();
Another approach:
You can also hide the implementation details from the aspx code with a property
.cs file
public string userid { get { return Session["userid"]; } }
.aspx file
<asp:Label ID="ll" runat="server" Text='<%= userid %>' >
This will work.
<asp:Label ID="ll" runat="server" Text='<%#Session["yourvariable"]%>'/>
I am having an issue that I am certain is easy to resolve, I just don't know what to do.
Here is my code:
<asp:TemplateField>
<HeaderTemplate>
<asp:Literal ID="text_shipped" Text="Media Shipped" runat="server" />
<br />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl_shipped" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "shipped") %>' />--></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" Visible='<%# DataBinder.Eval(Container.DataItem, "shipped" ) == "Yes" ? true : false %>' />--></ItemTemplate>
</asp:TemplateField>
The label "lbl_shipped" is showing the correct value which is either "Yes" or "No"
but, i want to add a button "lnk_ship", based on whether
or not the value is "Yes" (show button), or "No" (do not show button).
My issue is that I am using conditional code on the Visible keyword and I am testing for the value, but it seem to be ignoring my value for "shipped"
here are the main two lines, the first one shows the value, the second line is conditional, the conditional is NOT working. it keeps showing false:
<asp:Label ID="lbl_shipped" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "shipped") %>' />
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" Visible='<%# DataBinder.Eval(Container.DataItem, "shipped" ) == "Yes" ? true : false %>' />
DataBinder.Eval(Container.DataItem, "shipped" ).ToString()
Add .ToString() ie:
<asp:Button ID="lnk_ship" runat="server" CssClass="btn-mini" Text="Ship Software" Visible='<%# DataBinder.Eval(Container.DataItem, "shipped" ).ToString() == "Yes" ? true : false %>' />
I have just mocked up something quickly and it is working for me (ASP.NET web forms targeting .NET 4, VS2012) , maybe have a look:
Default.aspx
Contains the following GridView definition which I stuck randomly in a new web forms project.
<asp:GridView runat="server" ID="gridMe" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Button runat="server" ID="btnName" Text="Hi" Visible='<%# DataBinder.Eval(Container.DataItem, "Name").ToString() == "Bob" %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Default.aspx.cs
Has the following class definitions
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var list = new List<Thing>
{
new Thing() {ID = 1, Name = "Bob"},
new Thing() {ID = 2, Name = "Geraldine"}
};
gridMe.DataSource = list;
gridMe.DataBind();
}
}
public class Thing
{
public int ID { get; set; }
public string Name { get; set; }
}
Result
My output is something like:
ID Name
1 [Hi]
2
<asp:Label ID="lblLeaveStatus" runat="server" Font-Bold="true" Text='<%# Eval("Leave_Status").ToString() == 'A' ? 'Approved' : 'Applied' %>'></asp:Label>
Condition to display a particular word in the GridView on some condition
Could some thing like this work on Gridview
I get an error Server Tag not well formed
Try this
<asp:Label ID="lblLeaveStatus" runat="server" Font-Bold="true" Text='<%# Eval("Leave_Status").ToString() .Equals ( "A") ? "Approved" : "Applied" %>'></asp:Label>
Use Below Code:
<% if(Eval("Leave_Status").ToString() == "A"{%>
<asp:Label ID="lblLeaveStatus" runat="server" Font-Bold="true" Text="Approved"></asp:Label>
<%}
else {
%>
<asp:Label ID="lblLeaveStatus" runat="server" Font-Bold="true" Text="Applied"></asp:Label>
<%}%>