I have a repeater control on my .aspx page. There are times where a product is unique, so you can't change it's quantity, but in other cases when there are lots of items of this product, you should be able to edit it's quantity using a textbox and a linkbutton. Both OnlyOne and Quantity are present in the binded collection classes. I need to check the OnlyOne condition, something like that:
<% if (OnlyOne) { %>
<%# Eval("Quantity") %>
<%} else { %>
<asp:TextBox ID="TextBox1" runat="server" />
<asp:LinkButton ID="LinkButton1" runat="server">OK</asp:LinkButton>
<% }%>
The problem is that ASP.NET doesn't find the OnlyOne field. I've tried also (bool)Eval("OnlyOne"), but that didn't work also. So how should I write the condition?
Done it like that:
<asp:Label Text='<%# Eval("Quantity") %>' runat="server" ID="QuantityLabel" Visible='<%# Eval("OnlyOne") %>' />
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Quantity") %>' Visible='<%# !(bool)Eval("OnlyOne") %>' />
<asp:LinkButton ID="LinkButton1" runat="server" Visible='<%# !(bool)Eval("OnlyOne") %>' Text="OK" />
But I am still interested to hear the answer :) .
Related
Currently I am using asp:Repeater like this:
<asp:Repeater ID="itemsRepeater" runat="server">
<ItemTemplate>
<my:Button runat="server" Title='<%# DataBinder.Eval(Container.DataItem, "Title") %>' />
</ItemTemplate>
</asp:Repeater>
But now, I would like to send whole model to my:Button control like:
<asp:Repeater ID="itemsRepeater" runat="server">
<ItemTemplate>
<my:TabListButton runat="server" Model='<%# this %>' />
</ItemTemplate>
</asp:Repeater>
Could you tell me how to handle that?
I have solved this problem... somehow.
Maybe it's the only way to handle that. I know it's not beautiful, but it works:
public ModelType Model
{
get
{
return this;
}
}
and then:
<asp:Repeater ID="itemsRepeater" runat="server">
<ItemTemplate>
<my:Item runat="server" Model='<%# DataBinder.Eval(Container.DataItem, "Model") %>' />
</ItemTemplate>
</asp:Repeater>
i'm new to asp.net and i'm struggling with the replace function that i'm hoping someone can help with. When i use some test text it works fine (as in the example below) but as soon as i replace the test text with the value from the database (Eval("PContent")) i get a databinding error. The label separately works fine.
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
I've tried al-sorts but i cannot get around this.
Here's my code:
<asp:Label runat="server" ID="Label4" text='<%# Eval("PContent") %>' />
<%
Dim text1 As String = "Some text here [q]testing[/q]"
Dim output As String = text1.Replace("[q]", "<span class='quote'>")
Dim VS As String = output.Replace("[/q]", "</span>")
Response.Write(VS)
%>
Thanks for your time - sorry if this is a very n00b thing to ask! I did try search for an answer on here and google but i can't find anything...
**Update....
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label runat="server" ID="Label5" text='<%# Eval("PMonthName")%>' />
<asp:Label runat="server" ID="Label6" text='<%# Eval("PDay")%>' /></small>
</div><!--middlebartext -->
<div class="middlebartexttitle"><a href="/Details.aspx?ID=<%# Eval("BID")%>">
<asp:Label runat="server" ID="Label3" text='<%# Eval("Header")%>' /></a><br />
<asp:Label runat="server" ID="Label4" text='<%# Eval("PContent")%>' />
Permalink
<div class="ruler"></div>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource
ConnectionString="<%$ ConnectionStrings:Conn2 %>"
ID="SqlDataSource1" runat="server"
SelectCommand="SELECT * from tablename where Deleted = 'False' Order By DateAdded DESC"
onselected="SqlDataSource1_Selected">
</asp:SqlDataSource>
I've cut a chunk of code out so it's not as long :)
It's another way to do the replace more short:
C#
<%# ((string)Eval("PContent")).Replace("[/q]", "</span>") %>
VB.net
<%# (Eval("PContent").ToString().Replace("[/q]", "</span>") %>
I don't know a lot Vb.net but I think the code above works.
I hope that help you.
I don't see PContent defined in your question, but
it would be simpler to do something like,
Label4.Text = [value from db]
You could set the text after you have fetched the records from database
Try changing this:
<div class="middlebartexttitle"><a href="/Details.aspx?ID=<%# Eval("BID")%>">
<asp:Label runat="server" ID="Label3" text='<%# Eval("Header")%>' /></a><br />
<asp:Label runat="server" ID="Label4" text='<%# Eval("PContent")%>' />
Permalink
To:
<div class="middlebartexttitle"><a href='/Details.aspx?ID=<%# Eval("BID")%>'>
<asp:Label runat="server" ID="Label3" text='<%# Eval("Header")%>' /></a><br />
<asp:Label runat="server" ID="Label4" text='<%# Eval("PContent")%>' />
<a href='/Details.aspx?ID=<%# Eval("BID")%>'>Permalink</a>
Since Eval requires quotes for the field it's evaluating, my guess is that the quotes you have defining the href attributes are throwing it off. Change those to single quotes (like you have everywhere else) and see if that works.
Also, you can learn more about inline expressions (and when to use them) at http://support.microsoft.com/kb/976112
I have a hyperlink inside a repeater control for the list of pdf. I want to set a download counter on each click on each hyperlink. The FileName must be a parameter. My code is basically like below. There is also code that calls stored procedure and bind it to the repeater on page_load.
<asp:Repeater ID="rptPDF" runat="server">
<ItemTemplate>
<div class="repeaterResources">
<b><%# Eval("Name") %></b><br />
<b>Description</b> <%# Eval("Description") %><br />
<asp:HyperLink ID="HyperLink2" runat="server" class="downloadLink" NavigateUrl='<%# "~/PDF/" & Eval("Filename") %>' Target="_blank">Download</asp:HyperLink><br /><br />
</div>
</ItemTemplate>
</asp:Repeater>
The mystery bit is how to get a button click event from here. Thanks.
You can use the OnCommand event and set the CommandArgument attribute with a value using
<%# Eval('myvalue') %>.
MSDN has an example minus the repeater: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.oncommand.aspx
Sample:
<asp:Repeater ID="repeater" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="button1" OnCommand="button1_command" CommandArgument='<%# Eval("myvalue") %>' />
</ItemTemplate>
</asp:Repeater>
I have a Repeater on one of my pages like so:
<asp:Repeater ID="rptrHalls" runat="server" OnItemCommand="Choose_Hall">
<ItemTemplate>
<asp:Button ID="btn<% Container.DataItem %>" runat="server"
CommandName="<% Container.DataItem %>" Text="<% Container.DataItem %>"
/>
</ItemTemplate>
</asp:Repeater>
But, when I run it it errors out with the message:
'btn<% Container.DataItem %>' is not a valid identifier.
I want to append btn to the Container.DataItem value so that I have dynamically assigned control names that are associated with the underlying data item. Any ideas?
It should be something like
<asp:Button ID='<%# "btn" + Container.DataItem %>' runat="server"
and depends on the type of Container.DataItem
but is there a reason why you want to set the ID and not use something like this ?
<asp:Button ID="btnSubmit" runat="server"
I have an asp page that renders controls based on a Request parameter. Simplified example:
<% if (Request.QueryString["personType"] == "D") { %>
<asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("first_name") %>' />
<asp:TextBox ID="TextBoxSurname" runat="server" Text='<%# Bind("surname") %>' />
<% } else { %>
<asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("first_name") %>' />
<% } %>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server"
ControlToValidate="TextBoxName" ErrorMessage="Please enter an name." />
However, the compiler and runtime are complaining that the TextBoxName is not a unque ID. But surely it is, if the output is conditional and exclusive?
I could rename them TextBoxNameA (if block) and TextBoxNameB (else block), but the odd thing is that the validator still seems to fire on TextBoxNameB, even if the validator is inside the else block and the code runs through the first block at runtime
How can I set the page to render one or the other boxes, without conflicting IDs and conflicting Validators?
Thanks for any help
Ryan
How about moving around your logic?
<asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("first_name") %>' />
<% if (Request.QueryString["personType"] == "D") { %>
<asp:TextBox ID="TextBoxSurname" runat="server" Text='<%# Bind("surname") %>' />
<% } %>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server"
ControlToValidate="TextBoxName" ErrorMessage="Please enter an name." />
But server side, how is the parser supposed to distinguish the two?
And while parsing it will not be able to determine that one should be displayed and one shouldn't.
You need to give each server side control a different ID, regardless of it being displayed to the client.
You can restructure you code like this, as TextBoxName will always display (and with the same text):
<asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("first_name") %>' />
<% if (Request.QueryString["personType"] == "D") { %>
<asp:TextBox ID="TextBoxSurname" runat="server" Text='<%# Bind("surname") %>' />
<% } %>
And no more problem.
Since the Name textbox will show up regardless of the condition, you can just take that out of your if block all together and then just conditionally show the SurName textbox and that should solve your problem.