What is wrong with my Hyperlink - asp.net

<asp:HyperLink ID="TestHyperLink" runat="server" NavigateUrl='<%# "javascript:updateApplet();" %>' Text="Send" />
It says "The server tag is not well formatted."
Please help.

Missing ID Before "TestHyperLink"
<asp:HyperLink ID="TestHyperLink" runat="server" NavigateUrl='<%# "javascript:updateApplet();" %>' Text="Send" />

There are so many wrong things, have you ever, at least, bother to look in Google ?
<asp:HyperLink
id="TestHyperLink"
runat="server"
NavigateUrl="#"
OnClientClick="updateApplet()"
Text="Send" />
but for simple things like this, you don't need an ASP.NET Control, unless you are doing something from your code file... a simple <a> will do the trick.
Just understand that ASP.NET Controls are useful when we want to tweak or use then in our application as variables so we can control them dynamically. If we simply need a link that will never change, there is no need for a control, a html tag will do.

Related

Updating Database with value from datepicker control ASP.NET VB

Apologies in advance, for what may be a newbie question. (new to asp.net, coding in VB, zero knowledge of best controls). Please respect that I am not interested in AJAX controls, or using MVC and trying to minimise javascript. What I need to do is very simple in terms of technology.
I am developing a form that allows users to Edit database data. I have chosen to use FormView so that I can format it like a legacy vba app. I am able to set the data source to my database table and the values from the database successfully show up if I allow VWD to automatically format the control. I can edit everything in the database using this form as well.
However, the boss hates having to type dates.
I haven't been able to find a datepicker control with a datasource feature that works like the text boxes that are automatically built in formview (with a bind to a datasource feature). So, I am assuming none exist. I need some assurances that what I think I need to do is the right way.
Instead of using the FormView control's datasource via the front-end automagical stuff, I should instead
place one of these datapicker controls that simply combine calendar with a text box for all date fields in the formview (no disrespect to those who built them, just can't believe they are not more feature-rich, this seems so needed giving the number of datepickers available)
declare my data source in Page_Load and load all the controls if they have existing data utilising FindControl
use Data_Bound block to retrieve the selected values from each control utilising FindControl and build a dynamic SQL string for Update
declare and update my database using one of the code behind blocks, perhaps in the DataBound
Am I on the right track? I have no experience to be confident in my assumption.
and please, if there is an easier way, I'll take it, but I have to make it "pretty".
And suggestions for controls of any sort are welcomed.
---Further into my issue
Here is some code to prove I've actually tried to resolve this...
In my FormView code block I have:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="MASTERID_Action"
DataSourceID="srcAction">
<EditItemTemplate>
MASTERID_Action:
<asp:Label ID="MASTERID_ActionLabel1" runat="server"
Text='<%# Eval("MASTERID_Action") %>' />
<br />
Action_Description:
<asp:TextBox ID="Action_DescriptionTextBox" runat="server"
Text='<%# Bind("Action_Description") %>' />
<br />
Action_Target:
<asp:TextBox ID="Action_TargetTextBox" runat="server"
Text='<%# Bind("Action_Target") %>' />
<br />
Action_Captured:
<asp:TextBox ID="Action_CapturedTextBox" runat="server"
Text='<%# Bind("Action_Captured") %>' />
<br />
Action_Declined:
<asp:TextBox ID="Action_DeclinedTextBox" runat="server"
Text='<%# Bind("Action_Declined") %>' />
<br />
Action_AgreedDate:
<ewc:CalendarPopup ID="CalendarPopup1" runat="server" Culture="en-AU"
PostedDate="" SelectedDate='<%# Bind("Action_AgreedDate") %>'
SelectedValue="01/14/2013 08:47:54" UpperBoundDate="12/31/9999 23:59:59"
VisibleDate="01/14/2013 08:47:54" />
<br />
...
</EditItemTemplate>
My database holds this Action_AgreedDate as nullable.
When I view the ItemTemplate (in the browser) the date shows up as 0.000 (because its a text field and bound to Action_AgreedDate, no error occurs) and when I click Edit to go to the EditItemTemplate I get this error:
Conversion from type 'DBNull' to type 'Date' is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'Date' is not valid.
Source Error:
Line 95:
Line 96: Action_AgreedDate:
Line 97: '
Line 99: SelectedValue="01/14/2013 08:47:54" UpperBoundDate="12/31/9999 23:59:59"
I can easily translate this into "the control found a null field and doesn't know what to do with it"; problem is, I don't know what to do. I have checked the properties of the field (the CalendarPOP field to see if there is a setting for handling nulls and nothing is obvious to me. I'm currently trying to find further documentation on the control online. (I've contacted eWorld and hope they will be able to respond.)
I should also add that if I request a record that already has an Action_AgreedDate I get no errors because there is a value present in the database for the control to display.
I think the best way for you to do this would be to use jQuery and jQuery UI, which has a date picker, which can be attached to a regular ASP.NET TextBox in your FormView. So if you had FormView code that looks something like this for example:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSource1">
<EditItemTemplate>
id:
<asp:Label ID="idLabel1" runat="server" Text='<%# Eval("solution_id") %>' />
<br />
date_modified:
<asp:TextBox ID="date_modifiedTextBox" runat="server" Text='<%# Bind("date_modified") %>' />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
id:
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("solution_id") %>' />
<br />
date_modified:
<asp:Label ID="date_modifiedLabel" runat="server" Text='<%# Bind("date_modified") %>' />
</ItemTemplate>
</asp:FormView>
You could run something like this using jQuery UI to get a datepicker on the date_modified text box:
$('[id$=date_modifiedTextBox]').datepicker()
The $('[id$=date_modifiedTextBox]') part is necessary to select the ASP.NET control using jQuery, if it were just a normal element you could just use $('.className') or $('#id') or something like that. Hope this helps.
Problem solved.
I ran across an article on 4GuysFromRolla.com about the RJS POP Calendar (http://preview.tinyurl.com/cerm9xv) and it ticked all the boxes. Successfully implemented it and, because the calendar is separate from the specified text box, it doesn't have to be bound ! It works very nicely. The 4GuysFromRolla save my bacon yet again...

The server tag is not well formed, ASP Repeater Datasource

I've been getting parser error with message The server tag is not well formed for the following line.
<asp:Repeater runat="server" DataSource="<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>">
<ItemTemplate>
<sc:FieldRenderer ID="FieldRenderer1" runat="server" FieldName="Tag name" Item="<%# Container.DataItem %>"/>
</ItemTemplate>
<SeparatorTemplate>
/
</SeparatorTemplate>
</asp:Repeater>
The syntax looks fine, but one thing I'm not sure about is whether you can use the ".Field["tags"] element in there.
I've tried looking it up, but couldn't find a similar problem. I'm hoping someone provide me with some insight to why the parser is complaining about this line.
Thanks
What comes into my mind right now is to use a single-quoted string instead:
<asp:Repeater runat="server" DataSource='<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>' >
You have double quotes within the attribute. This confuses the parser - it can't tell where the attribute ends.
Wrap the attribute in single quotes to fix it:
DataSource='<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>'
try ' instead of " it might work
else try binding from code behind
<asp:Repeater runat="server" DataSource='<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>' >
Do you have a closing tag? i.e.
</asp:Repeater>
Otherwise you are missing the / at the end of your tag declaration.
<asp:Repeater runat="server" DataSource="<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>" />

Adapting a jQuery slideshow to work with an ASP Repeater

I'm using a jquery slideshow and I want to fill in the pictures from a database. I'm trying to use an ASP repeater to create the images in a div with this code.
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="dsSelectedCategory" Visible="True">
<ItemTemplate>
<a><asp:Image ID="Image2" runat="server" ImageURL="~/img1/photos/<%#Eval("PictureFilePath") %>" class="slide" /></a>
</ItemTemplate>
</asp:Repeater>
I'm getting the error that the server tag is not well formed and I'm guessing it is this bit
ImageURL="~/img1/photos/<%#Eval("PictureFilePath") %>"
PictureFilePath is a field in the database that holds the filename and extension of the file. So I have to write out the path to the file then add on the name.
Try
ImageURL='<%# "~/img1/photos/" + Eval("PictureFilePath") %>'
I have tried your code and found the answer. Finally, it solved my problem as well. Just mark as answer if worked for you.
This is your old code
ImageURL="~/img1/photos/<%#Eval("PictureFilePath") %>"
change it to
ImageURL='~/img1/photos/<%#Eval("PictureFilePath") %>'
replace the double quotes "" with single quotes ''

Dynamically assigning properties on a non data bound ASP.NET control

For example, let's say I have a HyperLink:
<asp:HyperLink runat="server" Text="Foo" NavigateUrl="foo.aspx" />
How can I set the NavigateUrl on the server side, without having to go the code-behind?
This doesn't work of course:
<asp:HyperLink runat="server" Text="Foo" NavigateUrl="<%= urlString %>" />
(where urlString might be a string I created earlier in the page)
And this doesn't work because the HyperLink is not within a data bound control:
<asp:HyperLink runat="server" Text="Foo" NavigateUrl='<%# urlString %>' />
I guess I could just use a standard anchor element:
Foo
But I'd rather not mix up HTML and ASP.NET controls, and it would be handy to be able to do this for other controls.
Surely there must be a way?
Try setting the property in an inline code block:
<asp:HyperLink runat="server" ID="MyLink" Text="Foo" />
<% MyLink.NavigateUrl="foo.aspx"; %>
This doesn't work of course:
Of course it does.
And this doesn't work because the
HyperLink is not within a data bound
control:
Consider the Page as your data bound control.
You need to calls it's DataBind method.
Page.DataBind();
Maybe you need to add an ID attribute as well. If that does not work, try to display a property instead of a variable.

Override FormView Templates

By default the FormView control creates html like :
ID <asp:TextBox ID="IdTextBox" runat="server" Text='<%# Eval("ID") %>' />
<br />
Name <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Eval("Name") />
I prefer:
<ol class="form-layout">
<li><asp:Label AssociatedControl="IdTextBox" runat="server">ID:</aspLabel><asp
....
</ol>
My plan is to create a new control ( OrderedListFormView ) that inherits the FormView and overrides the method that generates the default "crap" html. I have been unable to find the method. Can anyone help? Do you have a better solution that costs $0 dollars?
I would prefer to change the default behavior at design time.
You sound like you have the ASP.NET form blues. Have you tried ASP.NET MVC? It gives you far better control of your rendered HTML, and you can mix it in with existing ASP.NET applications.
Try using Control Adapters to change the rendered HTML from a FormView, there is a tool kit and are pretty easy to code
http://weblogs.asp.net/scottgu/archive/2006/09/08/CSS-Control-Adapter-Toolkit-Update.aspx
http://msdn.microsoft.com/en-us/magazine/cc163543.aspx

Resources