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.
Related
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);
}
New to webforms/c# I get a reference to the object in the onclick handler.
The reference to the object exists in the Repeater_ItemDataBound method.
After the curObj.CssClass = "XXXXX" has run the class object curObj is updated.
The page renders without the CSS class applied to the object.
I assume this is due to the LinkButton CSS does not apply to the Anchor tag that gets rendered in the end.
So how do I apply the CSS class to the actual rendered Anchor from the code behind?
// my aspx
<asp:Repeater ID="Repeater1" runat="server" onItemDataBound="Repeater_ItemDataBound">
<ItemTemplate>
<asp:LinkButton ID="my_btn" runat="server" OnCommand="cmdSelect_click" CommandArgument='<%# Eval("value") %>'><%# Eval("value") %></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
// my code behind
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (((MyObject)e.Item.DataItem).value == CurrentValue )
{
curObj.CssClass = "someCssClassHere";
}
}
protected LinkButton curObj;
protected void cmdSelect_click(object sender, CommandEventArgs e)
{
curObj = (LinkButton)sender;
CurrentValue = int.Parse(e.CommandArgument.ToString())-1;
}
I dont understand when/where you want to set cssclass..
If you want to set it in ItemDataBound:
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
LinkButton my_btn = (LinkButton)e.Item.FindControl("my_btn");
if (my_btn != null) my_btn.CssClass = "someCssClassHere";
}
or if you want to set after Click:
protected void cmdSelect_click(object sender, CommandEventArgs e)
{
LinkButton my_btn = (LinkButton)sender;
my_btn.CssClass = "someCssClassHere";
}
That's not quite how templated controls such as the Repeater work.
Firstly, you should do a FindControl inside ItemDataBound to find your LinkButton, and apply the CSS to the item that is found.
Secondly, you don't wire up events for controls inside a Repeater that way; instead you handle the ItemCommand event of the Repeater.
Can you post the code you're using to bind the repeater ? It would be useful to know what your DataSource is, then I can post something that works.
This post might help as well - Linkbutton inside Repeater for paging ASP.Net
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
I used the code below to make a change in div of the page of this code file:
HtmlGenericControl divTest = Page.FindControl("test") as HtmlGenericControl;
divTeste.InnerText = "tested";
But now, I want to change another page. Ex.: default.aspx code behind changes a div inner text in the test.aspx ...
The code above isn't appropriated. I want permanent changes. What code should be?
How can I do it?
You need to understand how ASP.NET works. Introduction to ASP.NET
If you are on Default.aspx and then wont to move to Test.aspx you can use values from Default.aspx in Test.aspx however you can't just change different pages from the page you are on. (Actually it is possible by writing data to a database and then later accessing that data on the page you want to change but for your example I don't believe you are looking for something like that)
The closest to your example I can think of is using a Query string.
Default.aspx Markup
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Default.aspx Code behind
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Test.aspx?DivMSG=Hello");
}
Test.aspx Markup
<div id="MyTestDiv" runat="server">
</div>
Test.aspx Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MyTestDiv.InnerText = Request.QueryString["DivMSG"];
}
}
Update:
One way to make it permanent is to write it to a database. A quick example but you need to do the database code
Default.aspx Markup
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Default.aspx Code behind
protected void Button1_Click(object sender, EventArgs e)
{
string messageToWriteToTestDiv = TextBox1.Text;
// Code: to write messageToWriteToTestDiv to the database
}
Test.aspx Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string myMessage = "";
// Code: string myMessage = Get Message from Database to write to div
MyTestDiv.InnerText = myMessage;
}
}
Test.aspx Markup
<div id="MyTestDiv" runat="server">
</div>
Html Button
<input id="Button1" type="button" value="button" runat="server"/>
.cs file:
public void display()
{
Response.Redirect("default.aspx");
}
How to call the display function which is in .cs file from html button click
Use a <asp:button> instead and just bind the click event in the designer.
This will generate an input in the actual html.
In your codebehind file use the following:
protected void Page_PreInit(object sender, EventArgs e)
{
Button1.ServerClick += display;
}
And change your display method signature to:
private void display(object sender, EventArgs e)
{
Response.Redirect("default.aspx");
}