My grid is 5 columns, an "Event" column followed by Q1 through Q4. One of the quarter columns is editable at any given time, the other three are labels. Here is the code I use to determine which to show (q defined elsewhere):
<telerik:GridTemplateColumn HeaderText="Q2" DataField="Q2" UniqueName="Q2">
<ItemTemplate>
<% If (q = 3) Then%>
<telerik:RadNumericTextBox ID="rntb_Q2" runat="server" Type="Number" ShowSpinButtons="true" Value='<%# CInt(Eval("Q2")) %>' Width="50px" CssClass="short" MinValue="0">
<IncrementSettings InterceptArrowKeys="false" InterceptMouseWheel="false" Step="1" />
<NumberFormat DecimalDigits="0" GroupSeparator="," />
</telerik:RadNumericTextBox>
<% Else%>
<asp:Label ID="lbl_Q2" runat="server" Text='<%# If(Eval("Q2") Is Nothing, "0", Eval("Q2")) %>'></asp:Label>
<% End If %>
</ItemTemplate>
</telerik:GridTemplateColumn>
This code all works fine; the problem is when I want to change one row to display a label all the time. I tried If ((q = 3) AndAlso (Not GetDataItem("EventDesc").ToString.ToLower = "total error doses")) as well as If ((q = 3) AndAlso (Not Eval("EventDesc").ToString.ToLower = "total error doses")) but with both I receive an error about not being able to use Eval, etc outside of data bound.
Thank you for your help.
So what I was trying to do was change whether a RadNumericTextBox or Label was displayed. This achieves the result without accessing the contents of another cell directly. It uses the .ItemIndex property to determine what controls should be visible.
<telerik:GridTemplateColumn HeaderText="Q2" DataField="Q2" UniqueName="Q2">
<ItemTemplate>
<% If (q = 3) Then %>
<telerik:RadNumericTextBox ID="rntb_Q2c" runat="server" Type="Number" ShowSpinButtons="true" Value='<%# Eval("Q2") %>' Width="75px" CssClass="short" Visible='<%#If(TryCast(Container, GridDataItem).ItemIndex < 9, True, False) %>'>
<IncrementSettings InterceptArrowKeys="false" InterceptMouseWheel="false" Step="1" />
<NumberFormat DecimalDigits="0" GroupSeparator="" />
</telerik:RadNumericTextBox>
<asp:Label ID="lbl_Q22" runat="server" Text='<%# Eval("Q2") %>' Visible='<%#If(TryCast(Container, GridDataItem).ItemIndex < 9, False, True) %>'></asp:Label>
<% Else %>
<asp:Label ID="lbl_Q2" runat="server" Text='<%# If(Eval("Q2") Is Nothing, "0", Eval("Q2")) %>'></asp:Label>
<% End If %>
</ItemTemplate>
</telerik:GridTemplateColumn>
Related
I have a repeater showing product stocks. I want show if no stock "Out of stock". If present show stock amount and unit.
I tried the following ways:
<%#Convert.ToInt32(Eval("AMOUNT")) == 0 ? "Out of stock" : %><%#Eval("AMOUNT") %> <%#Eval("UNIT") %>
and
<% if ( Convert.ToInt32(Eval("AMOUNT")) == 0) { %>
<asp:Label ID="Label1" runat="server" Text='Out of stock'></asp:Label>
<%} else { %>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("AMOUNT") %>'></asp:Label>
<% } %>
I am getting this error in this method:
System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Maybe there is a simple solution but I can't find. Thanks.
This if condition obviously doesn't support data binding, hence InvalidOperationException occurs:
<% if ( Convert.ToInt32(Eval("AMOUNT")) == 0) { ... } %>
Since the if condition has two blocks of markup (with else condition), you can use two asp:PlaceHolder controls as replacement with different visibility condition (one equals to zero and other is greater than zero):
<asp:PlaceHolder ID="AmountPlaceHolder1" runat="server" Visible='<%# Eval("AMOUNT") == 0 %>'>
<asp:Label ID="Label1" runat="server" Text='Out of stock'></asp:Label>
</asp:PlaceHolder>
<asp:PlaceHolder ID="AmountPlaceHolder2" runat="server" Visible='<%# Eval("AMOUNT") > 0 %>'>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("AMOUNT") %>'></asp:Label>
</asp:PlaceHolder>
Or use strongly-typed RepeaterItem.ItemType property instead of Eval:
<asp:PlaceHolder ID="AmountPlaceHolder1" runat="server" Visible='<%# Item.Amount == 0 %>'>
<asp:Label ID="Label1" runat="server" Text='Out of stock'></asp:Label>
</asp:PlaceHolder>
<asp:PlaceHolder ID="AmountPlaceHolder2" runat="server" Visible='<%# Item.Amount > 0 %>'>
<asp:Label ID="Label2" runat="server" Text='<%# Item.Amount %>'></asp:Label>
</asp:PlaceHolder>
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>
<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>
<%}%>
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 %>' />
Rate AnodeRate TotalRate
100 100 200
txtrate,txtanoderate n txttatalrate are added in ItemTemplate of gridview.
rate field is in database table while anoderate n totalrate not in database table
,also totalrate is readonly so that user can see only its addition,this addition will b passed to txtrate field n wil get bind that value in database.
also i took
<asp:HiddenField ID="hdnTotalRt" runat="server" /> as it is readonly..
sample code:
enter code here
<asp:TemplateField HeaderText="Standard Rate">
<ItemTemplate>
<asp:TextBox ID="txtRate" runat="server" BorderWidth="0.5pt" Text='<%# Bind("rate") %>' BorderColor="green" BackColor="#ffffcc" Width="40px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtRate" runat="server" ValidationGroup="ValAtSave" >*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ControlToValidate="txtRate" runat="server" ValidationExpression="[0-9]*.?[0-9]*" ValidationGroup="ValAtSave">
Nos Pls.
</asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AnodeRate">
<ItemTemplate>
<asp:TextBox ID="txtAnodeRate" runat="server" BorderWidth="0.5pt"
BorderColor="green" BackColor="#ffffcc" Width="40px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Rate" >
<ItemTemplate>
<asp:TextBox ID="txtTotalRate" runat="server" onchange="javascript:calculate_total_rate()" BorderWidth="0.5pt" Enabled="false"
BorderColor="green" BackColor="#ffffcc" Width="40px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
function calculate_total_rate()
{
document.getElementById('<%=txtTotalRate%>').value = ( document.getElementById('<%=txtRate.ClientID %>').value ) + (document.getElementById('<%=txtAnodeRate.ClientID %>').value );
document.getElementById('<%=hdnTotalRt.ClientID %>').value= document.getElementById('<%=txtTotalRate.ClientID %>').value;
}
It is showing me error as The name "txtRate"does not exist in d current context
can you try with this code, (I have
replaced '=' in
document.getElementById('<%=txtTotalRate%>')
and
document.getElementById('<%=txtAnodeRate.ClientID
%>' by '#')
function calculate_total_rate()
{
document.getElementById('<%#txtTotalRate.ClientID%>').value = ( document.getElementById('<%=txtRate.ClientID %>').value ) + (document.getElementById('<%#txtAnodeRate.ClientID %>').value );
document.getElementById('<%=hdnTotalRt.ClientID %>').value= document.getElementById('<%#txtTotalRate.ClientID %>').value;
}