Eval in anchor href - c#

I have the following code behind:
protected string VcdControlPanelLink { get; set; }
protected void Page_Load(object sender, EventArgs e) {
VcdControlPanelLink = "http://www.mywebsite.com";
}
And the following markup:
VDC control panel
But when I run the page, the href tag is rendered as an empty string:
<a title="View the VDC control panel" target="_blank" href="">VDC control panel</a>
I have tried various combinations of markup etc, but cannot get it to work. What am i missing?

If you use data binding expressions <%# ... %> in templates (e.g. the ItemTemplate of a GridView), data binding is invoked automatically. However, if you use such expressions outside of templates, you need to invoke data binding yourself by calling Control.DataBind() (or this.DataBind() in your case):
protected string VcdControlPanelLink { get; set; }
protected void Page_Load(object sender, EventArgs e) {
VcdControlPanelLink = "http://www.mywebsite.com";
this.DataBind();
}
<!-- Note that you do *not* use Eval here! -->
<a href='<%# VcdControlPanelLink %>' target="_blank" ... />
However, I don't think data binding is the right tool here. Since you are using basic HTML elements (instead of web controls), you can just use an inline ASP.NET expression (no this.DataBind() required):
<a href='<%= VcdControlPanelLink %>' target="_blank" ... />
In any case, make sure your link does not contain quotes ', or you are in for some HTML injection. If the value is supplied by the user, don't forget to HtmlEncode and sanitize it.

Make your property public and access it like this,
public string VcdControlPanelLink { get; set; }
VDC control panel

Related

How to get text content between asp:LinkButton tag

I have no problem to get text content if it is declear inside the <asp:LinkButton runat="server" Text="Test Content">.
However, when I place the text content between a pair of open and close <asp:LinkButton> tags. Using the following code cannot get the text content.
ASP.NET WebForm
<li>
<asp:LinkButton ID="linkButtonLogout" runat="server" OnClick="menuItem_Click"><span class="glyphicon glyphicon-log-out"></span> Logout</asp:LinkButton>
</li>
C# Code
protected void menuItem_Click(object sender, EventArgs e)
{
LinkButton clickedMenuItem = (LinkButton)sender;
string menuItemStr = clickedMenuItem.Text;
Debug.WriteLine(">>> menuItem_Click item: " + clickedMenuItem.Text);
}
Is there a way to get text content between asp tags?
If you will check the page source in both cases i.e. if you specify the text property explicitly or write the text between tags, you can see that both are same so in either of the two cased you can use the Textproperty like this:-
protected void menuItem_Click(object sender, EventArgs e)
{
Response.Write(">>> menuItem_Click item: " + linkButtonLogout.Text);
}

C# - Reload tabs from Master page

I have a FromDate and ToDate textboxes and a submit button in my Master page. I have 4 tabs with links for 4 different URLs displaying various reports.
Now on change of Dates and click of submit button, can I update/reload the reports (tabs) based on date change?
Thanks a lot in advance :)
I would suggest moving your from-to dates and submit button to a user control. You can then put that on each report, expose and wire up changed events on your control and expose properties for your from-to date textboxes to pop into your report.
In Visual Studio create a user control. If you are unsure how to do this try this link.
Populate the user control with your text boxes. Something like this:
<div>
<asp:Label ID="FromDateLabel" Text="From:" AssociatedControlID="FromDateTextBox" runat="server" />
<asp:TextBox ID="FromDateTextBox" runat="server" />
<asp:Label ID="ToDateLabel" Text="To:" AssociatedControlID="ToDateTextBox" runat="server" />
<asp:TextBox ID="ToDateTextBox" runat="server" />
<asp:Button ID="UpdateButton" Text="Update" runat="server"
onclick="UpdateButton_Click" />
</div>
And the code behind for that control. You'll need to expose an event and the two properties, which might look like this:
public partial class ReportDateControl : System.Web.UI.UserControl
{
public event EventHandler UpdateReport;
public string FromDate
{
get { return this.FromDateTextBox.Text; }
set { this.FromDateTextBox.Text = value; }
}
public string ToDate
{
get { return this.ToDateTextBox.Text; }
set { this.ToDateTextBox.Text = value; }
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
if (UpdateReport != null)
{
UpdateReport(this, EventArgs.Empty);
}
}
}
In your .aspx page you'll need to register the control, which might go something like this:
<%# Register Src="~/Controls/ReportDateControl.ascx" TagPrefix="myapp" TagName="ReportDateControl" %>
And then actually put it on the page:
<myapp:ReportDateControl id="ReportDateControl"
runat="server"
OnUpdateReport="ReportDateControl_UpdateReport" />
And then wire up the code behind to handle the update events:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ReportDateControl_UpdateReport(object sender, EventArgs e)
{
Controls.ReportDateControl control = (Controls.ReportDateControl)sender;
string fromDate = control.FromDate;
string toDate = control.ToDate;
}
}
Change the names and formatting where appropriate, but this should give you a good idea.
Also, you can expose the Date controls from the master page and access them through the Page.Master property. You'll need to cast to the specific type of your master page to get at it's properties.

how to set contol property in asp.net

Very simple and stupid question.
i have a page class
public partial class ProtectWayItem : System.Web.UI.UserControl
{
public int Count { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Count = 10;
}
}
and how i can set div id equal Count.
I mean something like:
<div id='<%# Count %>' > </div>
You have to use = instead #
<div id='<%= Count %>' >
And if you want to call with the # sign then you need to call a DataBind() method..
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
DataBind();
}
here is what each expression means
The <%= expressions are evaluated at
render time
The <%# expressions are evaluated at
DataBind() time and are not evaluated
at all if DataBind() is not called.
<%# expressions can be used as
properties in server-side controls.
<%= expressions cannot.
For a better understanding, please check out this link: The difference between <%= and <%# in ASP.NET
<div id='<%= Count %>' > </div>
But you must remember this section must be in FORM section.

Anchor control in Literal

using System;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
public void Download(object obj, EventArgs e)
{
Response.Write("abc");
}
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Text = "<a id='anc' href ='#' runat='server' onserverclick='Download'>Click Me</a>";
Response.Write(Literal1.Text);
}
}
}
<div>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</div>
I have the above code sniplet. I am trying to bind the anchor within the literal to a function in a following manner: onserverclick = "Download"
But the event is not firing. The requirement is that anchor is rendered through literal only.
Please help.
While your Literal1 control is a server-side control, setting the .Text value to represent a server control will not work.
Perhaps use the asp LinkButton control instead of a literal
so your .aspx page would have:
<asp:LinkButton ID="abc" runat="server" OnClick="Download" Text="Download" />
and your code behind .aspx.cs something like:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Download(object sender, EventArgs e)
{
Response.Write("abc");
}
The attribute runat="server" is meaningless in the client-side code (which is what you're generating here). You may need to resort to calling a webservice.
We are using anchor tag outside literal. Then it works fine
If you do not wish to use a LinkButton, you could replace your Literal with the text which you are adding to it on Page_Load (not entirely sure why youre doing it this way anyway?).
And then run an AJAX web request to run the Download method.

How do I bind a database field to an HTML TITLE element in ASP.NET 2.0?

I know how to do this in MVC:
<title> <%= Model.Title %> </title>
I also know how to bind a SQLDataSource or ObjectDataSource to a control on a form or listview.
But how do I render a field from my SQLDataSource or ObjectDataSource directly into the HTML?
You can use the Page.Title property to set the title in code behind.
Once you've run your SelectCommand or equivalent on your data source, just simply assign Page.Title a value from the result set.
Alternately, you can use the aspx page itself, and just inside the html assign a text string, like this:
<title>
<%= dataSource.Select(...) %>
</title>
You can declare a property and set its value using your desired field's value.
Code-behind:
private string myTitle;
protected string MyTitle
{
get { return myTitle; }
set { myTitle = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
MyTitle = "Testing 123!";
}
Markup: <title><%= MyTitle %></title>
In WebForms you still need to use a WebControl that implements DataBinding as the "container" for your fields. For instance, a GridView, Repeater, ListView, FormView or DetailsView. Unfortunately there isn't a WebControl designed specifically for rending just one row or object. So, you have a choice:
Use a Repeater something like this:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="MyDataSource">
<ItemTemplate>
<%# Eval("MyProperty") %>
</ItemTemplate>
</asp:Repeater>
Another alernative is to not use a DataSource. Instead, add properties to your page and then bind your data to these. For example, in your page codebehind:
public string MyPageProperty
{
get { return _myPageProperty; }
set { _myPageProperty = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
MyPageProperty = "This is some data";
}
You can then do this in your page:
<div>The value is: <%= MyPageProperty %></div>
Hope that helps.

Categories

Resources