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");
}
Related
I have a HTML control ie. Button. PFB Snippet
<button type="button" id="Button7" onserverclick="btnSave_OnClick"
runat="server" commandname="SaveNext">
Next
</button>
I am calling a same server side method from 2 different Button. I need to validate which button is clicked using CommandName in method.
Code behind:
protected void btnSave_OnClick(object sender, EventArgs e)
{
}
The problem is EventArgs e does not have CommandName . I changed method parameter to commandeventargs e which gives error as it requires EventArgs e
I am not preferring to change the HTML control to a asp:button.
Please suggest.
In the server-side event handler, you can retrieve the CommandName from the attributes of the button:
protected void btnSave_OnClick(object sender, EventArgs e)
{
HtmlButton btn = sender as HtmlButton;
string commandName = btn.Attributes["CommandName"];
...
}
Nothing works, The button doesn't trigger the event
code of button:
<input type="button" id="regBtn" runat="server" onserverclick="RegClick"/>
script code:
<script runat="server">
void RegClick(object sender, EventArgs e)
{
regBtn.Value += "a";
}
</script>
If you use ASP.Net Web Form, you want to use Service Control whenever posting back to server. It will go through Life Cycle Events, and trigger the appropriate event correctly.
<asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click"/>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
SubmitButton.Text += "a";
}
I've a form (mainpage.aspx), on which there's a button called 'Cancel'. Here's the code for the button:
<asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CssClass="btn btn-default" OnClick="btnCancel_Click" />
and the called function
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("EventList.aspx");
}
So as you can see, what the button do is simple. Just go to another page. But it seems here that doesn't happen. Every time I click the Cancel button, first it will try to load the code
Response.Redirect("EventList.aspx");
but then somehow it will try to execute the codes below:
if (hdnEventId.Value != "" && hdnEventId.Value != "0")
{
LoadEvent();
}
I know this because I put a breakpoint on Response.Redirect("EventList.aspx"); and after that I step into the code above
btw the codes above are located inside
protected void Page_Load(object sender, EventArgs e)
inside the file mainpage.aspx.cs
That's because Page_Load is called on any postback and you're actually doing a postback by clicking Cancel. The solution is to wrap your non-postback specific code in a IsPostBack check:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (hdnEventId.Value != "" && hdnEventId.Value != "0")
{
LoadEvent();
}
}
}
I need to call a JavaScript method with parameters from the code behind.
Javascript method
<script type="text/javascript">
function changeControlSample(path)
{
$find('<%= PartialUpdatePanel7.ClientID %>').set_UserControlPath(path);
$find('<%= PartialUpdatePanel7.ClientID %>').refresh();
}
</script>
<iucon:PartialUpdatePanel runat="server" ID="PartialUpdatePanel7"
DisplayLoadingAfter="500" InitialRenderBehaviour="Clientside" EncryptUserControlPath="false">
<LoadingTemplate>
<div style="margin-left: 84px; margin-top: 10px;">
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/loading.gif" />
</div>
<div style="text-align: center">
Updating...
</div>
</LoadingTemplate>
</iucon:PartialUpdatePanel>
The code Behind of the page
protected Consultation controlconsultation = new Consultation();
protected void Page_Load(object sender, EventArgs e)
{
PartialUpdatePanel7.UserControlPath = "Espace_Candidat/Consultation.ascx";
controlconsultation.imageinfo += controlconsultation_imageinfo;
Session["controlconsultation"] = controlconsultation;
}
void controlconsultation_imageinfo(object sender, CommandEventArgs e)
{
PartialUpdatePanel7.UserControlPath = "Espace_Candidat/InfoEdition.ascx";
Page.ClientScript.RegisterStartupScript(this.GetType(),
"CallMyFunction",
"changeControlSample('Espace_Candidat/InfoEdition.ascx')", true);
}
Code behind of the user control
public event CommandEventHandler imageinfo ;
protected void Page_Load(object sender, EventArgs e)
{
Consultation current = (Consultation)Session["controlconsultation"];
imageinfo = current.imageinfo;
}
protected void Valider (object sender, CommandEventArgs e)
{
if (imageinfo != null)
{
string pageNumber = (string)e.CommandArgument;
CommandEventArgs args = new CommandEventArgs("Control", pageNumber);
imageinfo(this, args);
}
}
This call didn't work even I change the JavaScript method by another one.
For example, if I try
Page.ClientScript.RegisterStartupScript
(this.GetType(),
"CallMyFunction",
"alert('blabla');",
true);
I got the same result.
So, What is the error that I commited?
How can I fix my code?
If you have update panel in page then call like this,
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), Guid.NewGuid().ToString(), #"<script type='text/javascript'>changeControlSample('" + path + "');</script>", false);
It don't have update panel then call like this
Page.ClientScript.RegisterStartupScript(this.GetType(), "tabselect", "<script type='text/javascript'>changeControlSample("' + path + '");</script>");
If you wish to call JavaScript method with parameters from the code behind you can get this done using
ClientScriptManager.RegisterStartupScript Method
please check the link given below:
http://msdn.microsoft.com/en-us/library/z9h4dk8y(v=vs.110).aspx
Hope this helps.
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.