Pass value to another page from image click in gridview - asp.net

I have a grid view template column that is a glyphicon in a hyperlink control, I am trying to pass an ID value that is in the SQL datasource to a page like so :
<ItemTemplate>
<asp:HyperLink ID="hlResident" CssClass="glyphicon glyphicon-refresh" NavigateUrl="Resident/<%#= Eval("ID") %>" runat="server"></asp:HyperLink>
</ItemTemplate>
I get the error "The Server Tag is not well formed".

Try this way
<asp:HyperLink ID="hlResident" CssClass="glyphicon glyphicon-refresh" NavigateUrl='<%# "Resident/" + Eval("ID") %>' runat="server"></asp:HyperLink>
Here the change is how you bind it
NavigateUrl='<%# "Resident/" + Eval("ID") %>'

<%#= Eval("ID") %>
Should be:
<%# Eval("ID") %>
There are some different types for asp tags. The accepted answer Here is very thorough on the subject.

NavigateUrl="Resident/<%#= Eval("ID") %>"
should be NavigateUrl='Resident/<%# Eval("ID") %>'

Related

Session variable in navigateurl of hyperlink on aspx page

I want add one more parameter i.e session["emp_no"] varible in NavigateUrl
How can i achieve this please help
<asp:HyperLink ID="lblJovid" runat="server" Class="link" NavigateUrl= '<%# Eval("pid", "frm_IAF.aspx?id={0}") %>' Text='<%# Bind("pid") %>' ></asp:HyperLink>
You can use string.Format for this:
NavigateUrl= '<%# string.Format("frm_IAF.aspx?id={0}&param2={1}", Eval("pid"), Session["emp_no"]) %>'

Hyperlink not displayed properly

I want to display data on a page dynamically from database.
I have added a news box and I am displaying events list in repeater from a database. Hyperlink and Marquee is also used. But hyperlink is not displayed properly.
The code is given below:
<asp:HyperLink ID = "HyperLink1" runat = "server" NavigateUrl = "/events/events.aspx?id=<%#Eval('event_id') %>">
<asp:Label ID = "Label1" runat = "server" text = '<%# Eval("event_title") %>' ></asp:Label></asp:HyperLink><br/>
ASP.NET HyperLink should be declared like this:
<asp:HyperLink
ID="HyperLink1"
runat="server"
NavigateUrl="/events/events.aspx?id=<%#Eval('event_id') %>"
Text='<%# Eval("event_title") %>' />
change your code as
<asp:HyperLink
ID="HyperLink1" runat=server
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "event_id", "/events/events.aspx?id={0}") %>'>
<%# DataBinder.Eval(Container.DataItem, "event_title") %>'
</asp:HyperLink>

Combining Strings in ASP.NET Attributes

I am trying to concat a string inside the attribute. I get an error. I think that it is related to my Eval. Is there a proper way to concat strings, or is this just not possible. The problem I believe is where I set the NavigateUrl.
<asp:HyperLink ID="lb"
runat="server"
Text='<%#Eval("Key.Id") %>'
NavigateUrl='ViewItem.aspx?id=' + '<%# Eval("Key.Id") %>'/>
Short answer: NavigateUrl='<%# Eval("Key.Id", "ViewItem.aspx?id={0}") %>'
Longer explanation:
The problem in your code is that you are use data binding expression only for part of your web control attribute. You need to move everything inside the data binding expression.
First of all, a data binding expression is this:
<%# EXPRESSION %>
Basically the rule for using a data binding expression for a web control attribute is that the expression must be the only thing in the attribute:
<asp:HyperLink ID="lb" runat="server"
Text='<%# EXPRESSION %>'
NavigateUrl='<%# EXPRESSION %>' />
So your first attribute, Text, is correct. But your second attribute, NavigateUrl is not correct. Because you put ViewItem.aspx?id= as the value for the attribute, leaving + '<%# Eval("Key.Id") %>' outside any attribute but inside the control tag.
Here is the correct syntax:
<asp:HyperLink ID="lb" runat="server"
Text='<%# Eval("Key.Id") %>'
NavigateUrl='<%# Eval("Key.Id", "ViewItem.aspx?id={0}") %>'/>
Notice that we are using a format string as the second parameter for Eval(). This is equivalent to the following, more explicit, syntax:
<asp:HyperLink ID="lb" runat="server"
Text='<%# Eval("Key.Id") %>'
NavigateUrl='<%# String.Format("ViewItem.aspx?id={0}", Eval("Key.Id")) %>'/>
Here's what I do when I have something in a gridview like this:
<img src='<%# GetDisImageLink(Eval("Disabilities").ToString()) %>'
alt="Disabilities" />
[CS code-behind]
public string GetDisImageLink(string dis)
{
return "../../Content/Images/CardContactInfo/" +
(dis.Trim() == "Y" ? "DIS.png" : "Blank.png");
}
Try this instead:
<asp:HyperLink ID="lb" runat="server" Text='<%#Eval("Key.Id") %>' NavigateUrl='ViewItem.aspx?id=<%# Eval("Key.Id") %>'/>
You don't need to concatenate

ASP.NET ItemTemplate Container.DataItem

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"

How do you concatenate text when using Bind expression in asp.net

What is the syntax to concatenate text into a binding expression for an asp.net webpage (aspx).
For example if I had a hyperlink that was being bound like this:
<asp:HyperLink id="lnkID" NavigateUrl='<%# Bind("Link") %>' Target="_blank"
Text="View" runat="server"/>
How do you change, say, the Text to concatenate a bound value with a string? Variations like this aren't quite right.
Text='<%# Bind("ID") + " View" %>'
neither does
Text='<%# String.Concat(Bind("ID"), " View") %>'
Use Eval instead.
Text='<%# Eval("ID", "{0} View") %>'
Eval is also better if the value is not going to be updated, where Bind allows two way data binding.
You can also place the "concatenation" in the text portion of a tag if using a template field:
<asp:TemplateField HeaderText="Name" SortExpression="sortName">
<ItemTemplate>
<asp:LinkButton ID="lbName" runat="server" OnClick="lbName_Click" CommandArgument='<%# Eval("ID") %>'>
<%--Enter any text / eval bindind you want between the tags--%>
<%# Eval("Name") %> (<%# Eval("ID") %>)
</asp:LinkButton>
</ItemTemplate>
This results in output like:
Name (ID)
inside of the template column.
I have used String.Format("{0}{1}"... before to good effect.
You could use the following:
CommandArgument='<%#String.Format("{0}|{1}", Eval("ArgZero"), Eval("ArgOn"))%>'

Resources