ASP.Net GridView TemplateField Inserting HTML - asp.net

I'm relatively new to ASP.Net so this may be a simple question. I'm using Visual Studio Express 2012 for Web and I have a GridView setup (which works fine) that I am trying to get HTML wrapped round one of the columns. My code at the moment:
<asp:TemplateField HeaderText="" SortExpression="teamviewer">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("teamviewer") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<a href="javascript:teamviewerclientconnect('999999999')">
<img src="Images/teamviewer_icon.png" /><asp:Label ID="Label1" runat="server" Text='<%# Bind("teamviewer") %>'></asp:Label>
</a>
</ItemTemplate>
</asp:TemplateField>
At the moment this puts my image in as the hyperlink and then outputs the SQL resulting field as text next to it. What I need to do is replace where I have put 999999999 with the actual SQL result. I don't want it to print the text at all, it should just be in the hyperlink. Thanks in advance.

Use a code-behind method to build the href string, like this:
protected string BuildHref(string clientId)
{
return "javascript:teamviewerclientconnect('" + clientId + "')";
}
<ItemTemplate>
<a href='<%# BuildHref(Eval("DATABASE_COLUMN_NAME_HERE")) %>'>
<img src="Images/teamviewer_icon.png" />
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("teamviewer") %>'>
</asp:Label>
</a>
</ItemTemplate>

Related

Getting value on the literal1.text in asp.net

I would like to ask if how can I get the value inside the <span> tag in literal1.text.
This is my code.
Literal1.Text = Literal1.Text & "<div class='chip' style='padding:8px;display:inline-block;border-radius: 25px;background:white;color:black;'><span>" & TextBox3.Text & "<i class='close fa fa-times'></i></span></div> "
I using it a tagbox in my project.
Here's a screenshot
Or if you have any suggestions on how can I create a tagbox in asp.net webforms that I can easily retrieve the data and save it in the database. I would really need and will appreciate the help!
Thanks in advance!
I would use a formview around the Textbox to insert the Tags into the database and then retrieve it and insert with a Repeater.
Add your query into the SqlDataSource and edit the Bind to the name of your column name.
Please note I haven't tested it but to my knowledge, it should work.
<asp:FormView runat="server" id="FormView1" DataSourceID="SqlDataSource1">
<InsertItemTemplate>
<asp:TextBox runat="server" id="TextBox1" Text='<%# Bind("TagName") %>'></asp:TextBox>
<asp:Button runat="server" id="InsertButton" CommandArgument="Insert"></asp:Button>
</InsertItemTemplate>
</asp:FormView>
<asp:Repeater runat="server" id="Repeater1" DataSourceID="SqlDataSource1">
<ItemTemplate>
<div class='chip' style='padding:8px;display:inline-block;border-radius: 25px;background:white;color:black;'>
<asp:Label Text='<%# Bind("TagName") %>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<!-- Add here your database querys for insert and select -->
<asp:SqlDataSource runat="server" id="SqlDataSource1"></asp:SqlDataSource>

Replace in ASP.net

i'm new to asp.net and i'm struggling with the replace function that i'm hoping someone can help with. When i use some test text it works fine (as in the example below) but as soon as i replace the test text with the value from the database (Eval("PContent")) i get a databinding error. The label separately works fine.
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
I've tried al-sorts but i cannot get around this.
Here's my code:
<asp:Label runat="server" ID="Label4" text='<%# Eval("PContent") %>' />
<%
Dim text1 As String = "Some text here [q]testing[/q]"
Dim output As String = text1.Replace("[q]", "<span class='quote'>")
Dim VS As String = output.Replace("[/q]", "</span>")
Response.Write(VS)
%>
Thanks for your time - sorry if this is a very n00b thing to ask! I did try search for an answer on here and google but i can't find anything...
**Update....
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label runat="server" ID="Label5" text='<%# Eval("PMonthName")%>' />
<asp:Label runat="server" ID="Label6" text='<%# Eval("PDay")%>' /></small>
</div><!--middlebartext -->
<div class="middlebartexttitle"><a href="/Details.aspx?ID=<%# Eval("BID")%>">
<asp:Label runat="server" ID="Label3" text='<%# Eval("Header")%>' /></a><br />
<asp:Label runat="server" ID="Label4" text='<%# Eval("PContent")%>' />
Permalink
<div class="ruler"></div>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource
ConnectionString="<%$ ConnectionStrings:Conn2 %>"
ID="SqlDataSource1" runat="server"
SelectCommand="SELECT * from tablename where Deleted = 'False' Order By DateAdded DESC"
onselected="SqlDataSource1_Selected">
</asp:SqlDataSource>
I've cut a chunk of code out so it's not as long :)
It's another way to do the replace more short:
C#
<%# ((string)Eval("PContent")).Replace("[/q]", "</span>") %>
VB.net
<%# (Eval("PContent").ToString().Replace("[/q]", "</span>") %>
I don't know a lot Vb.net but I think the code above works.
I hope that help you.
I don't see PContent defined in your question, but
it would be simpler to do something like,
Label4.Text = [value from db]
You could set the text after you have fetched the records from database
Try changing this:
<div class="middlebartexttitle"><a href="/Details.aspx?ID=<%# Eval("BID")%>">
<asp:Label runat="server" ID="Label3" text='<%# Eval("Header")%>' /></a><br />
<asp:Label runat="server" ID="Label4" text='<%# Eval("PContent")%>' />
Permalink
To:
<div class="middlebartexttitle"><a href='/Details.aspx?ID=<%# Eval("BID")%>'>
<asp:Label runat="server" ID="Label3" text='<%# Eval("Header")%>' /></a><br />
<asp:Label runat="server" ID="Label4" text='<%# Eval("PContent")%>' />
<a href='/Details.aspx?ID=<%# Eval("BID")%>'>Permalink</a>
Since Eval requires quotes for the field it's evaluating, my guess is that the quotes you have defining the href attributes are throwing it off. Change those to single quotes (like you have everywhere else) and see if that works.
Also, you can learn more about inline expressions (and when to use them) at http://support.microsoft.com/kb/976112

add text after dataGrid cell

This is dataGrid . How to add text(string) after <%# Bind("Value") %>
for example 123.432 and i want after any record to have "$" dolar sign
<asp:TemplateField HeaderText="Стойност">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Value") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelValue" runat="server" Text='<%# Bind("Value") %>'>
</asp:Label>
</ItemTemplate>
<ControlStyle Width="100px" />
</asp:TemplateField>
<% String.Format("{0}$",Eval("Value")); %>
You can take benefit of Standard Numeric Format String
<asp:Label ID="LabelValue" runat="server"
text='<%# Bind("Value").ToString("C", CultureInfo.CurrentCulture) %>'/>
which is a standard format available with C#.
But you want to show it in the last then you can simply add it in the last.
<ItemTemplate>
<asp:Label ID="LabelValue" runat="server" Text='<%# Bind("Value") %>'>
</asp:Label>
$
</ItemTemplate>
I suggest you to use MaskedEdit extender in ajax toolkit if you already using ajax toolkit in your project. MaskedEdit extender can be use in edit template and it will handle the mask on client side you don't want to worry about value and $ sign when you read the value back.
without using ajax toolkit you can use one label for $ sign and textbox for value field on edit template. In normal template also use two labels for both value and $ sign. then it will be easy to read the values.

ASP.net databinding a hyperlink from Access DB fields

I am helping my daughter build an asp.net website that has an Access database. We are using a DataList with a couple fields inside the ItemTemplate. The one that is giving me trouble is the Hyperlink. The code is below. When the page loads the hyperlink renders like this:
<a id="ContentPlaceHolder1_DataList1_lnk_6" href="#http://www.washingtonfaire.com/#" target="_blank">Washington Midsummer Renaissance Faire</a>
But when I click on the link, it tries to navigate to "http://localhost:1852/BOOMPiratesB/Raids.aspx#http://www.washingtonfaire.com/#"
What are we doing wrong?
Here is our code.
<asp:DataList ID="DataList1" runat="server" DataSourceID="AccessDataSource1">
<ItemTemplate>
<asp:HyperLink id="lnk" runat="server" NavigateUrl='<%# Eval("Link") %>' Target="_blank" Text='<%# DataBinder.Eval(Container.DataItem, "VenueName")%>'>
</asp:HyperLink>
<br />
<asp:Label ID="DateTextLabel" runat="server" Text='<%# Eval("DateText") %>' />
<br />
<asp:Label ID="CityStateLabel" runat="server" Text='<%# Eval("CityState") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/my.mdb"
SelectCommand="SELECT [VenueName], [Link], [DateText], [Season], [DateStart], [CityState] FROM [qpgRaid]">
</asp:AccessDataSource>
I cannot see where the extra # signs are coming from. They don't appear to be in the field in the table.
It's very puzzling. And insight would be most appreciated.
Have a look at this question
I guess you are using an access hyperlink column to store the link. If you convert it to a text column it should start working fine, or you may have to strip the '#'s manually.

<asp:TemplateField Boxes Disappearing

I've got a text box that just disappeared. When I add another templateField anywhere on the page, this one bizarelly disappears. Anyone know what might be up?
<asp:TemplateField HeaderText="Summary" SortExpression="summary">
<ItemTemplate>
<asp:Label ID="lblSummary" runat="server" Text='<%# Bind("summary") %>'></asp:Label>
</ItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtSummary" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("summary") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<AnotherEdit>
I hope you don't take this wrong, and I mean no offense, but it seems like you're missing some of the basic concepts and need a good place to learn them.
Please check out the following article so that you fully understand how to use TemplateFields as opposed to a normal BoundField or a Command field. I think once you "get" it, your disappearing item issues will clear up because you'll be able to see it on your own.
http://www.asp.net/learn/data-access/tutorial-12-cs.aspx
</AnotherEdit>
You can't have more than one ItemTemplate in a TemplateField. You can have an EditItemTemplate and an ItemTemplate, though...
Edit - Added
The ItemTemplate shows when you're in normal display mode.
The EditItemTemplate shows when you're in edit mode
InsertItemTemplate shows when you're in Insert mode.
For any column in a GridView (or field in a FormView or field in a DetailsView) there can only be one TemplateField.
Within that TemplateField, there can only be one ItemTemplate, one EditItemTemplate, and one InsertItemTemplate (and not all three are required but all three are recommended.)
If you want the TextBox to show up next to the label in the normal non-edit mode, you would put the text box within the existing ItemTemplate as follows:
<ItemTemplate>
<asp:Label ID="lblSummary" runat="server" Text='<%# Bind("summary") %>'>
</asp:Label>
<asp:TextBox ID="txtSummary" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("summary") %>'>
</asp:TextBox>
</ItemTemplate>
However, the norm is to have the label when in read mode, and a text box when in edit or update mode like shown below:
<asp:TemplateField HeaderText="Summary" SortExpression="summary">
<ItemTemplate>
<asp:Label ID="lblSummary" runat="server" Text='<%# Bind("summary") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSummary" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("summary") %>'>
</asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtSummary" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("summary") %>'>
</asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
You have multiple "ItemTemplate" declarations in there. There should be only one.

Resources