I have the following control in ASP.Net
<asp:Button ID="btnDownload" runat="server" Text="Run" CssClass="button" OnClientClick='Download( <%# Eval("ID") %> )' />
When I run the page, the following details are being outputted to the browser.
<input type="submit" name="gvMaster$ctl02$btnDownload" value="Run" onclick="DownloadReport( <%# Eval("ID") %> );" id="gvMaster_btnDownload_0" class="button" />
Why isn't the <%# Eval("ID") %> actually evaluating? To be clear, originally I had a small JS snippet within this statement. I had to make a minor modification, so I moved it to a JS method. After moving the code and leaving just the ID as a parameter, this result started showing up where, before, it was actually evaluating to the value in the associated record.
What could be going on? I've not seen this before.
Reposting comment as answer:
I don't think you can set properties on server side controls using data-binding like that (mixing static text and dynamic Eval values). You should use the OnItemDataBound event for the parent repeater/datagrid/etc and set it in the code behind, or use OnClientClick='<%# Eval("ID", "Download({0})") %>', where the entire value is evaluated and sent to the attribute all at once.
try OnClientClick='<%# "Download(" +Eval("ID") + " );" %>' i suspect you need the server code to evaluate the whole expression in javascript so when rendered by the browser it will correctly trigger the function.
Related
Does anyone know what is wrong with the following two lines of code? In both cases, there is no href in the anchor links when i view it in the browser:
<asp:HyperLink runat="server" NavigateUrl='<%# Eval(Request.QueryString["conferenceId"], "~/Cms/schedule-edit.aspx?conferenceId={0}&type=workshopStream") %>' Text="Create Workshop Stream"></asp:HyperLink>
<asp:HyperLink runat="server" NavigateUrl='<%# String.Format("~/Cms/schedule-edit.aspx?conferenceId={0}&type=scheduleItem", Request.QueryString["conferenceId"]) %>' Text="Create Schedule Item"></asp:HyperLink>
This exact same code seems to work fine when I put it into the ItemTemplate of a Listview though. But it doesn't work when used on it's own in an aspx file.
What's wrong with it?
Also if i replace the navigateUrl with a hardcoded string ~/cms/schedule-edit.aspx?conferenceId=2&type=stuff then the href shows up. It just doesn't work when i have the Eval or String.Format in there.
If those HyperLink server controls are located outside of DataBound controls like GridView, there are two problems in our code -
You want to use <%= %> instead of <%# %> which is used in DataBound controls.
You cannot use <%= %> to set property of a server control. Basically, you cannot mix runat="server" with <%= %>.
Solution
<a href='<%= String.Format("~/Cms/schedule-edit.aspx?conferenceId={0}&type=scheduleItem",
Request.QueryString["conferenceId"]) %>'>Create Workshop Stream</a>
The anchor syntax NavigateUrl='<%#...%> is only valid inside a GridView, ListView, etc. When it is not inside such controls, you can set its NavigateUrl property via code. Obviously, you also need to give an ID to your HyperLink.
The markup:
<asp:HyperLink ID="HyperLink1" runat="server" Text="Create Schedule Item"></asp:HyperLink>
The code behind:
HyperLink1.NavigateUrl = String.Format("~/Cms/schedule-edit.aspx?conferenceId={0}&type=scheduleItem", Request.QueryString["conferenceId"])
You are using a data-binding expression here. This is denoted in the following syntax:
<%# [code] %>
The code inside is evaluated only when the containing control, or any of its ancestors have their .DataBind() method called.
To work this around, you can:
Call Page.DataBind()
This could have some unwanted consequences if you have other data-bound controls on the page, as this method will cause all of them to have their data-binding events fired. Usually, this approach is applied if you have minimalistic code-behind and the entire page relies on data-binding expressions.
Give each HyperLink an ID and call HyperLinkID.DataBind();
Stick to the approach in codingstill's answer by setting the NavigateUrl property in the code behind of your page/user control.
Maybe I've forgotten how to use these, but I am going crazy trying to inject a server-side value into an HTML output. There are reasons why I am doing this inline, and not server-side, so please don't suggest that as a solution.
This code on the server side:
<asp:Label ID="Label1" runat="server" Text='<%= DateTime.Now.ToString() %>' />;
Renders as this in the client HTML sent to the browser:
<span id="Label1"> <%= DateTime.Now.ToString()></span>;
And it displays as big fat empty space, and nothing output to the interface.
If I change the ASP source to using the "#" character to define as data-binding syntax, then the rendered output to browser becomes:
<span id="Label1"></span>
EDIT:
Setting Label text was just a simplified object for the sake of asking the question. In real life, I am setting the CssClass attribute, which does not allow me to use the "wrapping" workaround some have suggested. I wanted to set a public property and have all the controls update from it dynamically on page load.
Ideally, since I already have all the controls laid out on the aspx page. Just looking to add an attribute. I wanted to have:
<asp:textbox ID='MyTxtBox1' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox2' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox3' CssClass='<% strOtherVal %>' />
<asp:textbox ID='MyTxtBox4' CssClass='<% strVal1 %>' />
Now what it looks like I need to do is repeat all my (250+) controls on the codebehind in a block of code that looks like:
MyTxtBox1.CssClass=strVal1
MyTxtBox2.CssClass=strVal1
MyTxtBox4.CssClass=strVal1
MyTxtBox3.CssClass=strOtherVal
I believe that may not work on a compiled Web Application as it's not interpreted at run-time like a C# "Web Site". However, I was able to get it to work wrapping the label around the value:
<asp:Label runat="server"><%= DateTime.Now.ToString() %></asp:Label>
Set the Label1.Text = value instead of trying to use server side attrs inside of the server control
Can anyone offer an explanation as to why the asp:imagebutton gives me a badly formed html error while the html input element does not? I know it's about the findcontrol() in the onclientclick
assignment. They're written in exactly the same format but maybe they shouldn't be?
<ItemTemplate>
<input type="image" src="Resources/info.png" onclick="toggle('<%# Container.FindControl("PresetUploadDescription").ClientID %>');return false;" />
<asp:ImageButton ImageUrl="Resources/info.png" OnClientClick="toggle('<%# Container.FindControl("PresetUploadDescription").ClientID %>');return false;" ToolTip="info" ID="Description" runat="server"/>
....
You cannot use a <%...%> construct in a control that is executed at the server. (runat="server")
<%#...%> is used for databinding or Eval type statements.
<%=...%> is equivalent to a Response.Write statement, which looks like what you are trying to do (write out the ClientID of a certain control). Unfortunately, this won't work either - you'll get a
Server tags cannot contain <% ... %> constructs. Error
To fix, you need to Add the OnClientClick attribute to the Imagebutton control via the Code Behind page:
Description.Attributes.Add("OnClientClick",
"toggle('" + FindControl("PresetUploadDescription").ClientID + "');return false;");
I am scratching my head over this, but have no idea what the problem is.
My actual code is
<asp:Label ID="Label1" runat="server" Text="abc"
Visible='<%#Request.QueryString["ListName"] == null %>' />
<asp:GridView ID="gvLists" runat="server"
Visible='<%#Request.QueryString["ListName"] == null %>' />
As you can see, i am trying to only make the visibility of the object be driven by the querystring. It works fine for the GridView, but doesn't work for a label. I also tried Panel and HyperLink with the same results.
I am sure I could get this working by putting my code in the code-behind, but it won't be as clean.
<%# %> works only on databound items.
you need to change it to <%= %> (Notice the "=")
<%= is uses to print to the page directly and the <%# is used for data binding elements. Here is a great explanation of all the inline code directives.
Thanks to Alison for pointing me in the right direction.
I needed to add Page.DataBind() to my Page_Load event in order for the expression to be evaluated.
I think it might have been asked before but i was unable to find the right answer, so i am asking here. i have added a data source which is working fine, i wanted a feature where i query the top n entries from the database and add it with a hyperlink. Think of it like Latest News! The markup for the hyperlink inside the ItemTemplate of DataList is this.
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Eval("News_Id") %>' NavigateUrl="~/News.aspx?NewsId=<%#Eval("News_Id") %> " runat="server" /> </asp:HyperLink>
however i get The error as "Error Creating Control, Server tag is not well formed". It reports the error where the quotes are placed.
I know i can use datanavigateurl property but i want to write it in this way. as written in the markup above. How can i?
Upon re writing it to
NavigateUrl='~/Product.aspx?DVDID=<%#Eval("Title") %> '
i get the following as the url
http://localhost:61221/Product.aspx?DVDID=<%#Eval("Title") %>
try this :
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Eval("News_Id") %>'
NavigateUrl='<%#Eval("News_Id", "~/News.aspx?NewsId={0}") %>'
runat="server" />
</asp:HyperLink>
<%# Eval() %> must be inside single quotes, otherwise it throws error.
To concatenate string in your binding tag, you can use this :
<%# "~/News.aspx?NewsId=" + Eval("News_Id").ToString() %>