I have a chunk of code that on page load with populates some of or all of the following labels. It should have two labels per line ( needs a line break after each xData label). The problem I am having is that since the number of labels with data and set to visable on page load changes, the br / tags cause spacing issues when not all labels are visible.
<div id="Status">
<asp:Label ID="1" runat="server" Text="1:" Width="125px" Visible="false" />
<asp:Label ID="1Data" runat="server" Text="" Visible="false" />
<asp:Label ID="2" runat="server" Text="2:" Width="125px" Visible="false" />
<asp:Label ID="2Data" runat="server" Text="" Visible="false" />
<asp:Label ID="3" runat="server" Text="3:" Width="125px" Visible="false" />
<asp:Label ID="3Data" runat="server" Text="" Visible="false" />
</div>
I would like to be able to add the line breaks after each "xData" label in the code behind when the labels are filled and set to visible.
I have tried adding "\n" to the label text and\or Environment.NewLine with no luck.
Thanks for any help
The easy way...
<div id="Status">
<div id="Status1" runat="server" Visible="false">
<asp:Label ID="1" runat="server" Text="1:" Width="125px" />
<asp:Label ID="1Data" runat="server" Text="" />
</div>
<div id="Status2" runat="server" Visible="false">
<asp:Label ID="2" runat="server" Text="2:" Width="125px" />
<asp:Label ID="2Data" runat="server" Text="" />
</div>
<div id="Status3" runat="server" Visible="false">
<asp:Label ID="3" runat="server" Text="2:" Width="125px" />
<asp:Label ID="3Data" runat="server" Text="" />
</div>
</div>
The right way...
<div id="Status">
<asp:Label CssClass="statusLabel" ID="1" runat="server" Text="1:" Width="125px" Visible="false" />
<asp:Label ID="1Data" runat="server" Text="" Visible="false" />
<asp:Label CssClass="statusLabel" ID="2" runat="server" Text="2:" Width="125px" Visible="false" />
<asp:Label ID="2Data" runat="server" Text="" Visible="false" />
<asp:Label CssClass="statusLabel" ID="3" runat="server" Text="3:" Width="125px" Visible="false" />
<asp:Label ID="3Data" runat="server" Text="" Visible="false" />
</div>
/* CSS */
#Status span {
display: block;
}
#Status .statusLabel {
clear: both;
float: left;
}
I think you could do it a couple of different ways.
One option would be what #Greg points out in his comment to your post.
Another possible option would be enclosing each label in its own <div> tag with runat="server" and then make these <div>s visible when needed. The <div> should create its own line break because of the nature of a <div>
asp:Label resolves to a span in html. If you want each one to have its own line, add the css style "display:block". Usually, you can do this by setting CssClass and put display:block in that class
If you want this way, you need to use Literal control for each BR tag so that you can set it to visible/invisible based on the visibility of corresponding Label control.
Why not just add a CSS class with a display: block rule to those labels?
This is presentational, after all.
Try wrapping the labels around a <div> instead of putting a <br /> at the end. In my experience, empty <div>'s don't create that empty space.
Related
im working on a project and im combining 3 tables the tables are already associated in the dbml but my problem is when i use the data list it diaplays my table names not the correct fields this is a college assignment but i have tried everything i know to do.
I've tried to combine them seperatly in sql manager in vs but im just not understanding why it isn't showing the correct thing
<form id="form1" runat="server">
<div>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="assign_6_final.DataClasses1DataContext" EntityTypeName="" Select="new (CustomerID, Name)" TableName="Customers">
</asp:LinqDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="LinqDataSource1" DataTextField="Name" DataValueField="CustomerID" OnSelectedIndexChanged="Page_Load">
</asp:DropDownList>
<asp:DataList ID="DataList1" runat="server" DataSourceID="LinqDataSource2" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" OnDataBinding="DataList1_SelectedIndexChanged">
<ItemTemplate>
IncidentID:
<asp:Label ID="IncidentIDLabel" runat="server" Text='<%# Eval("IncidentID") %>' />
<br />
DateOpened:
<asp:Label ID="DateOpenedLabel" runat="server" Text='<%# Eval("DateOpened") %>' />
<br />
DateClosed:
<asp:Label ID="DateClosedLabel" runat="server" Text='<%# Eval("DateClosed") %>' />
<br />
Title:
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
<br />
CustomerID:
<asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>' />
<br />
Technician:
<asp:Label ID="TechnicianLabel" runat="server" Text='<%# Eval("Technician") %>' />
<br />
Product:
<asp:Label ID="ProductLabel" runat="server" Text='<%# Eval("Product") %>' />
<br />
<br />
<br />
</ItemTemplate>
</asp:DataList>
</div>
<asp:LinqDataSource ID="LinqDataSource2" runat="server" ContextTypeName="assign_6_final.DataClasses1DataContext" EntityTypeName="" Select="new (IncidentID, DateOpened, DateClosed, Title, Description, CustomerID, Technician, Product)" TableName="Incidents" Where="CustomerID = #CustomerID">
<WhereParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="CustomerID" PropertyName="SelectedValue" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
<asp:EntityDataSource ID="EntityDataSource1" runat="server">
</asp:EntityDataSource>
</form>
</body>
the outcome i expected was that the last 2 items the product and technician would output correctly but they haven't and im kind of lost my teacher said this is a common problem in asp but ive never seen it
Try using the following and so forth:
<%# DataBinder.Eval(Container.DataItem,"CustomerID") %>
I am creating a shopping cart web app for my class. When the user clicks on the add to cart button, I want to pass the value of the ID of each specific product to a separate method in the code behind. Not sure if my syntax is off or if this just wont work. Here is my mark up:
<asp:Button ID="AddToCart" CommandName="Add"
OnClientClick ='<%# ListView1_AddToCart(Eval("ID"))%>'
CssClass ="Button" runat="server" Text="Add to Cart" />
Here is my code behind:
public void ListView1_AddToCart(string CatID)
{cart.AddToCart(CatID);}
I keep getting various issues, but this gives me the following error:
The best overloaded method match for 'OurCats_GrumpyCats.ListView1_AddToCart(string)'
has some invalid arguments.
How can I resolve this? Is there any better way?
EDIT: Here is my mark up
<%# Page Title="All Cats" Language="C#" MasterPageFile="~/MasterPage/Layout.master" AutoEventWireup="true" CodeFile="AllCats.aspx.cs" Inherits="OurCats_GrumpyCats" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<h1>Meet all of our kittens!</h1>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource2">
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<InsertItemTemplate>
<span style="">ID:
<asp:TextBox ID="IDTextBox" runat="server" Text='<%# Bind("ID") %>' />
<br />
Name:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
<br />
Price:
<asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>' />
<br />
Imgu:
<asp:TextBox ID="ImguTextBox" runat="server" Text='<%# Bind("Img") %>' />
<br />
Description:
<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' />
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
<br />
<br />
</span>
</InsertItemTemplate>
<ItemTemplate>
<table class ="Table" style="border-style: solid; ">
<tr>
<td >
<a href ="Details.aspx?ID=<%# Eval("ID")%>">
<img src="../Images/<%# Eval("Img") %>" width ="200" />
</a>
</td>
<td style="width:700px; margin-left: 100px">
<asp:Label ID="Label1" runat="server" Text="Name: "></asp:Label><asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<asp:Textbox ID="CatID" runat="server" Visible="false" Text='<%# Eval("ID")%>'></asp:Textbox>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Price: $"></asp:Label><asp:Label ID="PriceLabel" runat="server" Text= '<%# Eval("Price") %>'/>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Description: "></asp:Label><asp:Label ID="DescriptionLabel" runat="server" Text= '<%# Eval("Description") %>' />
<br />
<br />
<asp:Button ID="AddToCart" CommandName="Add" OnClientClick='<%# "AddToCart(" +Eval("ID") + " );" %>' CssClass ="Button" runat="server" Text="Add to Cart" />
</td>
</tr>
</table>
</ItemTemplate>
<LayoutTemplate>
<div id="itemPlaceholderContainer" runat="server" style =" margin-left :30px;">
<span runat="server" id="itemPlaceholder" />
</div>
<div style="">
</div>
</LayoutTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:420_Project_GLConnectionString1 %>" SelectCommand="SELECT * FROM [OurCats]">
</asp:SqlDataSource>
</asp:Content>
try below code
<asp:Button ID="AddToCart" CommandName="Add"
OnClientClick ='<%# ListView1_AddToCart((string)Eval("ID"))%>'
CssClass ="Button" runat="server" Text="Add to Cart" />
OnClientClick ='<%# "ListView1_AddToCart(" +Eval("ID") + " );" %>'
I have a repeater. In the past, each item in the repeater had an associated text box. But, now I added an attribute to the repeated item that needs to specify if a text box, larger text box, or check box is used for that item.
Here is what my aspx code looks like:
<%if (Eval("DisplayType") == "LargeBox") { %>
<asp:TextBox ID="largeBoxAnswer" Rows="8" runat="server" Width="200" MaxLength="2000" EvaluationQuestionID='<%# Eval("EvaluationQuestionId") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="largeBoxAnswer" Display="dynamic" Font-Bold="true" ErrorMessage="*" />
<%} %>
<%else if (Eval("DisplayType") == "CheckBox") { %>
<asp:TextBox ID="checkBoxAnswer" runat="server" Width="200" MaxLength="100" EvaluationQuestionID='<%# Eval("EvaluationQuestionId") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="checkBoxAnswer" Display="dynamic" Font-Bold="true" ErrorMessage="*" />
<%} %>
<%else { %>
<asp:TextBox ID="txtAnswer" runat="server" Width="200" MaxLength="100" EvaluationQuestionID='<%# Eval("EvaluationQuestionId") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtAnswer" Display="dynamic" Font-Bold="true" ErrorMessage="*" />
<%} %>
This is not working and I get the following error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
I found quite a bit about this error, but nothing that really helped with this particular issue.
Am I even going about doing something like this the correct way? I am not super experienced with asp.net, so I am open to a different approach to this issue. If this is the best way, how do I move the logic to the code behind so this works properly?
The error is telling you that your if statement is not actually in the databinding context, so even if your Eval did work, what it trys to Eval... "DisplayRule" ... doesnt actually exist at that line.
Have a look at this link; eval in if statement?
From what those guys are saying, your answer may lie in ElementIfTrue, or the Visible property.
So you might end up with something like this;
<asp:TextBox ID="largeBoxAnswer" ElementIfTrue='<%# Eval("DisplayRule") == "LargeBox" %>' Rows="8" runat="server" Width="200" MaxLength="2000" EvaluationQuestionID='<%# Eval("EvaluationQuestionId") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="largeBoxAnswer" Display="dynamic" Font-Bold="true" ErrorMessage="*" />
<asp:TextBox ID="checkBoxAnswer" ElementIfTrue='<%# Eval("DisplayRule") == "CheckBox" %>' runat="server" Width="200" MaxLength="100" EvaluationQuestionID='<%# Eval("EvaluationQuestionId") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="checkBoxAnswer" Display="dynamic" Font-Bold="true" ErrorMessage="*" />
<asp:TextBox ID="txtAnswer" ElementIfTrue='<%# Eval("DisplayRule") == "**notsure**" %>' runat="server" Width="200" MaxLength="100" EvaluationQuestionID='<%# Eval("EvaluationQuestionId") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtAnswer" Display="dynamic" Font-Bold="true" ErrorMessage="*" />
... I don't think that solves all of your problems, but it may get you along the way.
I feel almost stupid asking this because I think the answer is probably very basic. But, here it goes.
I am designing a website with a button that "rolls dice" then enables other buttons on the page (categories in my game), which ultimately, will use those buttons to go to the proper category. So, in effect, [click "roll ..."], the C# of the page (Game.aspx.cs) processes which buttons to enable, display/enable those buttons for click-ability.
My problem is this:
The page is
[domain]/UncommonSense/Game.aspx
but after I click the "roll ..." button the URL turns to
[domain]/Game.aspx#/Game.aspx
This causes a problem in the categories buttons because I need to redirect to
[domain]/UncommonSense/Question.aspx
but since Game.aspx#/ has taken the place of UncommonSense/ it fails.
CODE:
Okay, here is the code in my Game.aspx file. The button is the "ID=Roll" button listed right below "Label12." The code behind is literally right now a blank function so it is not doing anything on the C# side except posting back (apparently). However, when that button is clicked the URL goes from:
[domain]/UncommonSense/Game.aspx
to
[domain]/Game.aspx#/Game.aspx
NOTE: I just tested from a root directory and it did about the same thing:
[domain]/Game.aspx
became
[domain]/Game.aspx#/Game.aspx
Maybe this has something to do with the form?
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Game.aspx.cs" Inherits="Uncommon_Sense.Game" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Uncommon Sense</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
<form id="form1" runat="server">
<div data-role="page">
<div data-role="header" data-theme="b">
<h1><asp:Label ID="Label1" runat="server" Text="Uncommon Sense"></asp:Label></h1>
</div>
<div data-role="content">
<h4><asp:Label ID="Label4" runat="server" Text="Game Statistics"></asp:Label></h4>
<asp:Label ID="Label11" runat="server" data-mini="true" Text=""></asp:Label>
<asp:Label ID="Label10" runat="server" data-mini="true" Text=""></asp:Label>
<h4><asp:Label ID="Label2" runat="server" Text="Game Play"></asp:Label></h4>
<asp:Button ID="Roll" runat="server" Text="Roll Dice ..." data-mini="true"
onclick="Roll_Click" />
<hr width="80%" />
<div class="ui-grid-c">
<div class="ui-block-a">
<asp:Button ID="btnShortStuffChecked" runat="server" Text="Short Stuff" data-mini="true"
data-icon="check" Enabled="False" Visible="False" />
<asp:Button ID="btnShortStuffNotChecked" runat="server" Text="Short Stuff" data-mini="true"
Enabled="False" Visible="False" onclick="btnShortStuffNotChecked_Click" /></div>
<div class="ui-block-b">
<asp:Button ID="btnWhoSaidChecked" runat="server" Text="Who Said?" data-mini="true"
data-icon="check" Enabled="False" Visible="False" />
<asp:Button ID="btnWhoSaidNotChecked" runat="server" Text="Who Said?" data-mini="true"
Enabled="False" Visible="False" /></div>
<div class="ui-block-c">
<asp:Button ID="btnCommonQuipChecked" runat="server" Text="Common Quip" data-mini="true"
data-icon="check" Enabled="False" Visible="False" />
<asp:Button ID="btnCommonQuipNotChecked" runat="server" Text="Common Quip" data-mini="true"
Enabled="False" Visible="False" /></div>
<div class="ui-block-d">
<asp:Button ID="btnRhymeTimeChecked" runat="server" Text="Rhyme Time" data-mini="true"
data-icon="check" Enabled="False" Visible="False" />
<asp:Button ID="btnRhymeTimeNotChecked" runat="server" Text="Rhyme Time" data-mini="true"
Enabled="False" Visible="False" /></div>
<div class="ui-block-a">
<asp:Button ID="btnAnythingGoesChecked" runat="server" Text="Anything Goes" data-mini="true"
data-icon="check" Enabled="False" Visible="False" />
<asp:Button ID="btnAnythingGoesNotChecked" runat="server" Text="Anything Goes" data-mini="true"
Enabled="False" Visible="False" /></div>
<div class="ui-block-b">
<asp:Button ID="btnDefineItChecked" runat="server" Text="Define It" data-mini="true"
data-icon="check" Enabled="False" Visible="False" />
<asp:Button ID="btnDefineItNotChecked" runat="server" Text="Define It" data-mini="true"
Enabled="False" Visible="False" /></div>
<div class="ui-block-c">
<asp:Button ID="btnAllAbroadChecked" runat="server" Text="All Abroad" data-mini="true"
data-icon="check" Enabled="False" Visible="False" />
<asp:Button ID="btnAllAbroadNotChecked" runat="server" Text="All Abroad" data-mini="true"
Enabled="False" Visible="False" /></div>
<div class="ui-block-d">
<asp:Button ID="btnSpellItChecked" runat="server" Text="Spell It" data-mini="true"
data-icon="check" Enabled="False" Visible="False" />
<asp:Button ID="btnSpellItNotChecked" runat="server" Text="Spell It" data-mini="true"
Enabled="False" Visible="False" /></div>
</div>
<h4><asp:Label ID="Label3" runat="server" Text="Settings"></asp:Label>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Question.aspx">HyperLink</asp:HyperLink>
</h4>
<label for="slider-0">Computer Difficulty:</label>
<input type="range" name="slider" id="slider-0" value="2" min="1" max="4" />
<h3><asp:Label ID="Label5" runat="server" Text="Label"></asp:Label></h3>
<asp:Button ID="ChoiceA" runat="server" Text="Button" onclick="ChoiceA_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" Visible="False" />
<asp:Button ID="Button3" runat="server" Text="Button" Visible="False" />
</div>
</div>
</form>
</body>
</html>
UPDATE:
And the answer is ... JQuery Mobile!
By simply removing the reference to JQuery Mobile the problem is solved so apparently this is something in their library that changes URLs. That sucks because I liked using JQuery Mobile. Oh well, my comments will stop now on the annoyance because I am appreciative of the 99.9% of the stuff they do that helps. But, for the record...
ANY LINK will have:
linker.aspx
changed to:
linker#/linker.aspx
for some reason.
This UpdatePanel is contained by an UserControl. When the LinkButton is pressed arow should be added in another GridView. When an user is logged in this control is working well.
The problems appears when an user is not logged in and try to push that button. No event triggers.
Someone suggested me to give a permission for accessing this control in web.config. That didn't work.
Anyone has another idea?
<asp:UpdatePanel runat="server" UpdateMode="Conditional" EnableViewState="true" ID="IngredientsUpdatePanel">
<ContentTemplate>
<asp:ObjectDataSource ID="sourceIngredients" runat="server" SelectMethod="GetAll">
</asp:ObjectDataSource>
<asp:GridView ID="Ingredients" AllowPaging="true" runat="server" DataKeyNames="IngredientId"
EnableViewState="true" DataSourceID="sourceIngredients" PageSize="5"
AutoGenerateColumns="false" HorizontalAlign="Center" OnSelectedIndexChanged="Ingredients_SelectedIndexChanged">
<RowStyle HorizontalAlign="Center" />
<HeaderStyle Font-Bold="true" ForeColor="Black" />
<Columns>
<asp:TemplateField HeaderText="Ingrediente" ItemStyle-Font-Size="10">
<ItemTemplate>
<asp:Label ID="lblId" Text='<%# Bind("IngredientId") %>' Visible="false" runat="server"/>
<asp:Label ID="lblPrice" Text='<%# Bind("Price") %>' Visible="false" runat="server"/>
<asp:Label ID="lblDescr" Text='<%# Bind("Description") %>' Visible="false" runat="server"/>
<asp:Label ID="lblName" Text='<%# Bind("Name") %>' Visible="false" runat="server"/>
<asp:Label ID="lblPict" Text='<%# Bind("Picture") %>' Visible="false" runat="server"/>
<div style="text-align:left;">
<img id="img" style="float:right;" src='<%# Eval("Picture") %>'
height="75" runat="server" alt="Picture" />
<b>
<%# Eval("Name") %>
</b>
<br />
<br />
Price: <b><%# Eval("Price") %></b>
<br />
<br />
<br />
</div>
<hr />
<div style="text-align:left;">
<b>Description</b>
</div>
<div style="width:300px;">
<%# Eval("Description") %>
</div>
<br />
<asp:LinkButton Enabled="true" runat="server" Text="Add" CommandName="Select" ID="cmdAdd" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
I solved the problem in a tricky way.
I deleted the LinkButton and before TemplateField I put a ButtonField and all is working fine.
Now the code looks like:
<Columns>
<asp:ButtonField Text="Add" CommandName="Select" />
<asp:TemplateField>
......
</asp:TemplateField>
</Columns>
Still I'm not understanding why the control had that behavior.
Don't forget to give the webresource.axd enough rights in the web.config?