handling special character '/' in grid view TemplateField - asp.net

I am trying to concatenate two data fields in template field via '/'character but I am getting error as ::
The server tag is not well formed.
How should I handle the special character in TemplateField.
<asp:TemplateField HeaderText="FATHER NAME / MOTHER NAME">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("FATHER_NAME") + " '/' " + Eval("MOTHER_NAME")%>' />
</ItemTemplate>
</asp:TemplateField>

I haven't tested but this should work.
Text='<%# Eval("FATHER_NAME") + "/" + Eval("MOTHER_NAME")%>'

Define a method in code behind file
protected string GetParentsName(string fatherName, string montherName)
{
return Convert.ToString(fatherName) + "/" + Convert.ToString(montherName);
}
Call it for each row from aspx file like below:
<asp:Label runat="server" Text='<%# GetParentsName(Eval("FATHER_NAME") as string, Eval("MOTHER_NAME") as string) %>' />

Related

How to string concatenate two bindings in asp.net?

I would like to string concatenate two bindings into one string. How can I do this? I tried the below, but only LastName shows up when I run the application.
<asp:Label ID="txtFacultyName" runat="server" Text='<%#Bind("FirstName") + Bind("LastName") %>'/>
There are a couple of options.
Create a combined property in your code behind/model:
// cs
public string FullName
{
get { return $"{FirstName} {LastName}"; }
}
// aspx
<asp:Label ID="txtFacultyName" runat="server" Text='<%#Bind("FullName") %>'/>
Use Eval. Eval will allow one-way binding which is fine for displaying in an asp:Label
<asp:Label ID="txtFacultyName"
runat="server"
Text='<%# string.Format("{0} {1}", Eval("FirstName"), Eval("LastName")) %>'/>

The server tag is not well formed in gridview

I have the hyperlink control on the GridView and I want to call the javascript function with passing parameters. I am getting Server Tag is not well formed error. I tried changing double quotes to single quote etc, still the same issue.
Can anyone help me find the issue here .
Line 1946: <asp:TemplateField HeaderText="Transaction Id">
Line 1947: <ItemTemplate>
Line 1948: <asp:HyperLink ID="lbltransId"
runat="server"
Text="<%# "<a href=\"javascript:subViewBookingDetails('"+
Eval("transId") +
"','','','','',,'','','')\">" +
Eval("transId") + "</a>" %>"></asp:HyperLink>
Line 1949: </ItemTemplate>
Line 1950: <FooterTemplate>
This should work. Do not use Text to populate link inside, use NavigateUrl instead.
<asp:HyperLink id="hyperlink1"
NavigateUrl="<%# String.Format(
"javascript:subViewBookingDetails({0} ,,,,,,,,)", Eval("transId"))%>"
Text="<%#Eval("transId") %>"
runat="server"/>
Text='<%# "" + Eval("transId") + "" %>'

How to handle single quote in query string parameter values inside Eval for navigateurl property of hyperlink

Inside Gridview control
If my ID_Logon value has single quote characters in it, then the string gets terminated at the single quote.
for eg, if Id_Logon = O'connel
then only the O is being passed as a parameter. How to pass the whole string?
<asp:TemplateField HeaderText="LogonID" >
<ItemTemplate>
<asp:HyperLink ID="hyperlink1"
NavigateUrl='<%#"EditLogon.aspx?ID=" + Eval("ID_Logon")%>'
Text='<%# Bind("ID_Logon")%>' runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Try this
NavigateUrl='http://home/?<%# Eval("U_ID") %>'
or
NavigateUrl='<%# "http://home/?" + (string)Eval("U_ID") %>'
source asp:hyperLink NavigateURL and Eval functions

How do I conditionally display text in an ASP TemplateView?

I have a few fields that look like this in a website that uses ASP and VB (the data is displayed in a gridview):
<asp:TemplateField HeaderText ="Comp" SortExpression="NAM_CMPT" ItemStyle-Width="50%" ItemStyle-Wrap ="false" ItemStyle-HorizontalAlign ="left">
<ItemTemplate>
<asp:Label ID ="Label_Comp" runat="server"
Text='<%# Eval("CDE_CMPT") + " - " + Eval("NAM_CMPT")%>' />
</ItemTemplate>
</asp:TemplateField>
And what I'm trying to do is display nothing in the field if the data is empty, and display the string you see in the Text property if there is data. Currently it displays the hyphen used in the Text string when there is no data. I tried several methods of formatting the Eval that I found online but was unable to find a working solution. I also tried using the
EmptyDataText
property however this seemed to have no effect.
I am new to ASP so that could be user error. Any help is greatly appreciated.
You can also use eval for visible and check for data
<asp:TemplateField HeaderText ="Comp" SortExpression="NAM_CMPT" ItemStyle-Width="50%" ItemStyle-Wrap ="false" ItemStyle-HorizontalAlign ="left">
<ItemTemplate>
<asp:Label ID ="Label_Comp" runat="server" visible='<%# If(String.IsNullOrEmpty(Eval("CDE_CMPT")), false, true)'
Text='<%# Eval("CDE_CMPT") + " - " + Eval("NAM_CMPT")%>' />
</ItemTemplate>
</asp:TemplateField>
I haven't used VB.net is a while, so the syntax might be off.

ASPxGridView and Eval(string) method

I'm trying to add column to ASPxGridView which would have link to other page:
<Columns>
...
<dxwgv:GridViewDataColumn Caption=" " VisibleIndex="10">
<DataItemTemplate>
<dxe:ASPxHyperLink ID="lnkEdit" runat="server" Text="Edit" NavigateUrl="../Category/Elements/<%# Eval("Id").ToString() %>/Edit"/>
</DataItemTemplate>
</dxwgv:GridViewDataColumn>
</Columns>
But I get error:
Parser Error Message: The server tag is not well formed.
when I used ' ' instead " " the link href property is "../Category/Elements/<%# Eval("Id").ToString() %>/Edit"
I don't think you can put databinding elements into the middle of the attribute value. The entire value needs to be within the <% %>:
NavigateUrl='<%# "../Category/Elements/" + Eval("Id").ToString() + "/Edit" %>'
I'm not sure with UI library you're using, but usually the Eval() allows a string format parameter. This would be preferable to the string concatenation approach. You might be able to do:
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Id", "../Category/Elements/{0}/Edit") %>'

Resources