I have an ASP.Net page with a GridView. In one of the GridView cells there's a HyperLink control and its NavigateURL property is set like so:
NavigateUrl='<%# "~/telecom/SmartPhoneInventory.aspx?IMEI=" + Eval("IMEI") %>'
There's a RadioButtonList (rblDeviceType) on this page (not in the GridView) with four values. I want to add another querystring to the HyperLink's NavigateURL so that:
NavigateUrl='<%# "~/telecom/SmartPhoneInventory.aspx?IMEI=" + Eval("IMEI") + "&devicetype=" + rblDeviceType.SelectedValue %>'
This is of course not correct syntax. Is there a way to do this?
Try this:
In your html
<a href='<%= string.Format("~/telecom/SmartPhoneInventory.aspx?IMEI={0}&devicetype=", this.someValue, rblDeviceType.SelectedValue) %>'>
Hello World
</a>
or in your html:
<asp:HyperLink runat="server"
NavigateUrl='' ID="demoLink">
Hello World
</asp:HyperLink>
and then in your codebehind:
demoLink.NavigateUrl= string.Format("~/telecom/SmartPhoneInventory.aspx?IMEI={0}&devicetype=",this.someValue,rblDeviceType.SelectedValue)
Regarding
'someValue'
Which you present as Eval("IMEI") in your sample code since your code is not part of the Grid you will need to get this from either a control directly, session, viewstate or server side variable. Your code sample does not allow me to understand where is the original source of this value.
Try this in your code behind:
public partial class _Default : Page
{
public string someValue = "Hello World";
Using string.Format and <%= instead of <%#
Related
My code looks like this:
<asp:HyperLink ID="HyperLink1" NavigateUrl="Search.aspx?txtGraphicNumber=" target="_blank" runat="server">Search Related</asp:HyperLink>
My querystring starts with "txtGraphiNumber=". What I'd like to do is append to this querystring the information in a field on the screen. The field is called txtGraphicNumber.
Can I do something like:
<asp:HyperLink ID="HyperLink1" NavigateUrl="Search.aspx?txtGraphicNumber=" + txtGraphicNumber + " " target="_blank" runat="server">Search Related</asp:HyperLink>
You have two options to achieve the same:
Option 1# Code behind:
//assuming txtGraphicNumber is a textbox.
HyperLink1.NavigateUrl += txtGraphicNumber.text;
Option 2# InlineCode:
<asp:HyperLink ID="HyperLink1" NavigateUrl='<%="Search.aspx?txtGraphicNumber=" +
txtGraphicNumber.value %>' target="_blank" runat="server">Search Related
</asp:HyperLink>
Just do it from code-behind:
HyperLink1.NavigateUrl += txtGraphicNumber;
If txtGraphicNumber is a literal string value. You could use inline ASP.NET script for constructing primitive HTML controls and their values/attributes but this doesn't play nice with managed server-side controls.
I want to use a value that is pulled from the SQL within the if statement. Ideally i want to do the equivalent of
<% If DataBinder.Eval(Container, "DataItem.BookID") == 1 Then%>
Is there a way to do this with the correct syntax?
This is how you put conditions in aspx file. Just a rough sample base on what I understand:
<%# System.Convert.ToInt32((DataBinder.Eval(Container.DataItem, "BookID")!="") ? DataBinder.Eval(Container.DataItem, "BookID"):0) %>
Make sure you have int in BookID not any other type.
Explaining Further:
In case you want to have an if else condition:
<%# If DataBinder.Eval(Container.DataItem, "DATAFIELD") <> "" Then
Response.Write("something")
End If %> // This is invalid
The above statement can be properly written in aspx file as this:
<%# DataBinder.Eval(Container.DataItem, "DataField").Equals("")?"":"Something"%>
I'm not sure if this can be done or not the way you are requesting it.
As you may or may not know, the typical way to do this is to have a control in your markup, like so
<asp:listView ID="SophiesListView" ...
..
<ItemTemplate>
<asp:HyperLink ID="hlGlossary" title="click here for more information" target="_blank" runat="server" />
</ItemTemplate>
</asp:listView />
Then, in the codebehind, find your listview / repeater / datagrid or what have you and choose ItemDataBound. Inside this event, do something like this:
If e.Item.DataItem("vehicleType") IsNot DBNull.Value AndAlso e.Item.DataItem("vehicleType") = "JETSKI" Then
DirectCast(e.Item.FindControl("hlGlossary"), HyperLink).NavigateUrl = "Glossary.aspx#JETSKI"
DirectCast(e.Item.FindControl("hlGlossary"), HyperLink).Text = "?"
End If
To keep your page logic as simple as possible your best bet is to data bind to the Visible property of controls. For example, if you want to only show some html if the BookID == 1 then create a new property on your data source like this
public bool Show
{
get
{
return BookID == 1;
}
}
and in your page you'd have
<asp:Placeholder runat="server" Visible='<%# Eval("Show") %>'>
...html goes here...
</asp:Placeholder>
I have the following repeater control, which is bound to collection of DashboardPage objects. DashboardPage has the following properties: Name, Pagenumber .
<asp:Repeater ID="reportPages" runat="server">
<ItemTemplate>
<asp:ImageButton runat="server" onclick="ImageButton_Click"
ImageUrl="<%# GetImagePath(Container.DataItem) %>"
onmouseover="<%# GetMouseOverEventString(Container.DataItem) %>"
onmouseout="<%# GetMouseOutEventString(Container.DataItem) %>"
/>
</ItemTemplate>
<SeparatorTemplate>
</SeparatorTemplate>
</asp:Repeater>
I want to bind ImageButton's ID property to Name property of DashboardPage object, something like this
ID="<%# Eval('Name') %>"
But an exception is thrown:
The ID property of a control can only be set using the ID attribute in the tag and a simple value.
I need the ID property of the image, because I have a client side script which changes the image, using the it's ID. Is there a way around this?
Thanks for replies
Regards
Gagik Kyurkchyan
Since you have found you cannot dynamically set the ID of a server control you need to find an alternative way of referencing your image using JavaScript. There are three ways I can think of:
Set the CssClass property of the control and use that instead. It's a little more inefficient to find a control by class rather than ID using DOM, but that is probably negligible in the real world.
Wrap your image in a standard DIV. and set the ID on the container DIV. You can then use something like jQuery to go $("#divid img")...
Use a standard <img> tag (with no runat="server") rather than an asp:Image control. You can then set the ID on that without issue.
You can use ItemDataBound to set the ID.
CODEBEHIND:
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var textbox = e.Item.FindControl("myTextBox");
textbox.ID = "myTextBox" + (e.name).toString(); /*ToString() method can be unnecessary in some cases.*/
}
REPEATER:
<asp:Repeater ID="myRepeater" runat="server" OnItemDataBound="BehaviorCustomizeRepeater_ItemDataBound">
/*other stuffs*/
</asp:Repeater>
In my ASPX page I've this:
<h3>
<asp:HyperLink runat="server"
NavigateUrl="<%$ Resources:Path, Article%>"
Text='<%# Eval("title") %>' />
</h3>
For the NavigateUrl attribute I want to specify an ID like
NavigateUrl="<%$ Resources:Path, Article%>?id=4"
But when I do that the expression is not precessed by the ASP parser.
How can I do that?
Don't do this in markup. You have a server control here -- give it an ID (say, ID="NavigationLink", and then do something like this in your .cs file:
void Page_Load(object sender, EventArgs e)
{
// I'm guessing that "4" was just an example here, so fill that piece in with a function you can call to create the proper ID.
NavigationLink.NavigateUrl = Properties.Resources.Path + Properties.Resources.Article + "?id=4"
}
Edit: I'm assuming that when you say <%$ Resources:Path, Article%> that you're trying to reference the Path and Article entries in your resources file, but upon further reflection, it's hard to tell exactly what you're doing. Can you be more specific here?
You can define a protected function in the code behind class and call it from the markup. Like this:
<h3>
<asp:HyperLink runat="server"
NavigateUrl="<%# GetNavigateUrl(Eval("ID")) %>" <%-- Passing an ID as a parameter for example --%>
Text='<%# Eval("title") %>' />
</h3>
Code behind:
// Again, idObj is just an example. Any info from the data item can be passed here
protected string GetNavigateUrl(object idObj)
{
int id = (int)idObj;
string urlFromResources = // retrieving the url from resources
return urlFromResources + '?ID=' + id;
}
You could retrieve the resource values programmatically, in your code-behind, and set the NavigateUrl property from there.
Try String.Format...
<% =String.Format({0}?id={1}, Request.ApplicationPath, 4) %>
It looks like you're mixing some other language into this (javascript?).
Also, don't forget to give your server-side controls an ID.
I am trying to do this:
<asp:HyperLink NavigateUrl='<%= WebContext.RootUrl %><%= WebContext.CurrentUser.UserName %>' runat="server" Text='<%= GetProfileImage(WebContext.CurrentUser.AccountId) %>'></asp:HyperLink>
But am getting the error:
this is not scriptlet. will output as
plain text.
when I mouse over my declarative statements.
Any ideas? Thanks.
You cannot use <%= ... %> literals to set properties of server-side controls.
Instead, you can use a normal (client-side) <a> tag, like this:
<%= GetProfileImage(WebContext.CurrentUser.AccountId) %>
If GetProfileImage doesn't return HTML tags, make sure to escape it.
You can use data binding syntax <%# %>. Just be sure that your hyperlink is either in a databound control, such as a ListView item template, or that you explicitly call DataBind() on the control from code-behind.
You can still populate an <asp:HyperLink> if you provide the ID and runat="server" properties. You can then set any property of the HyperLink from code-behind.
ASP Code:
<asp:HyperLink ID="myLink" runat="server"/>
Code-behind:
public void Page_Init()
{
myLink.NavigateURL = WebContext.RootUrl + WebContext.CurrentUser.UserName;
myLink.Text = GetProfileImage(WebContext.CurrentUser.AccountId);
}
<a href='<%= WebContext.RootUrl %><%= WebContext.CurrentUser.UserName %>'><%= GetProfileImage(WebContext.CurrentUser.AccountId) %></a>