Asp.net form links - asp.net

How can I use a link in asp.net as a button. What's the right way to do this? For example with a label I'm doing it like this:
<a class="navbar-brand" href="#">Welkom<asp:Label ID="txtWelkom" runat="server" Text=""></asp:Label></a>
And then I set the label to a value that I received from the server for example:
txt.Welkom = name;
So how can I do this with a link (without seeing the button).
Is this the right way to do it (in asp.net forms)?

You want the LinkButton Control:
On .aspx side
<asp:LinkButton ID="lnkWelkom" runat="server" OnClick="lnkWelkom_OnClick" />
Code behind (aspx.cs)
using System;
using System.Web.Security.AntiXss;
using System.Web.UI;
namespace StackOverflowExamples.WebForms
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var name = "";
lnkWelkom.Text = AntiXssEncoder.HtmlEncode(name, true);
}
protected void lnkWelkom_OnClick(object sender, EventArgs e)
{
//Do stuff
}
}
}

Related

Pass value from ASPX page to code behind through a link or button

I have the code:
Test Link</div>
And code behind is:
protected void Test(object sender, EventArgs e)
{
Response.Redirect(Request.RawUrl);
}
What I want is to send an assumed value like <%Eval("testdata")%> to Test()
that I can execute some code like updating database base on <%Eval("testdata")%>, and the Page is not redirected to another one.
(So I dont use Request.QueryString)
You could use a link button for your use case.
<asp:LinkButton runat="server" ID="Test" CommandArgument='<%#Eval("testdata"%>' OnClick="Test"></asp:LinkButton>
Then in your code behind.
protected void test (object sender, EventArgs e)
{
var linkButton = (LinkButtone)sender;
var argument = linkButton.CommandArgument;
......
}

Using Nemiro OAuth library for Google authentication in Webforms

I am trying to use Nemiro library to authenticate against Google in a Webforms asp.net project. This library documentation is at Nemiro GoogleClient Documenation
I have a simple aspx page called ExternalLogin.aspx, whose markup and code-behind are as given below.
Question
With code that I have, when Login using Google button is clicked, then user does not get directed to Google's authorization page. What is missing in my code that is causing this?
Markup
<%# Page Language="C#" AutoEventWireup="true" CodeFile="ExternalLogin.aspx.cs" Inherits="ExternalLogin" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnGoogle" runat="server" Text="Login using Google" OnClick="btnGoogle_Click" />
</div>
</form>
</body>
</html>
Code-behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nemiro;
using Nemiro.OAuth.Clients;
using Nemiro.OAuth;
public partial class ExternalLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGoogle_Click(object sender, EventArgs e)
{
var result = OAuthWeb.VerifyAuthorization();
if (result.IsSuccessfully)
{
var user = result.UserInfo;
Response.Write(String.Format("User ID: {0}<br />", user.UserId));
Response.Write(String.Format("Name: {0}<br />", user.DisplayName));
Response.Write(String.Format("Email: {0}", user.Email));
}
}
}
I have also defined the keys for Google OAuth in Application_Start event as below.
void Application_Start(object sender, EventArgs e)
{
Nemiro.OAuth.OAuthManager.RegisterClient(
new Nemiro.OAuth.Clients.GoogleClient(
"some-value-1",
"some-value-2"
));
}
I think you should be looking at OAuthWeb.RedirectToAuthorization method. Here's the API doc for your reference. So just call this method in your btnGoogle_Click, and then verify your authorization in Page_Load event handler.
Here's the sample code:
protected void btnGoogle_Click(object sender, EventArgs e)
{
OAuthWeb.RedirectToAuthorization("Google", new Uri(Request.Url, "ExternalLogin.aspx").AbsoluteUri);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
var result = OAuthWeb.VerifyAuthorization();
if (result.IsSuccessfully)
{
var user = result.UserInfo;
Response.Write(String.Format("User ID: {0}<br />", user.UserId));
Response.Write(String.Format("Name: {0}<br />", user.DisplayName));
Response.Write(String.Format("Email: {0}", user.Email));
}
}
}
Also, if you'd like to verify the authorization results on a different page, just change the page name in the URI constructor, and put verification code in the Page_Load event of your new page.
Hope it helps.

Creating dynamic button and its event handler

I am trying to use dynamics buttons and events. When I clicked static button and I showed dynamic button. But When I clicked dynamic button I didn't work dinamikButon_Click event. What is my wrong? Sorry my language. Thx in advance.
Default.aspx.cs is below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestWebApplication
{
public partial class _Default : Page
{
int i = 1;
Button dinamikButon;
protected void Page_Load( object sender, EventArgs e )
{
}
protected void btnStatik_Click( object sender, EventArgs e )
{
dinamikButon = new Button
{
Text = "Dinamik" + i,
ID = "btnDinamik" + i,
CommandArgument = "commandArgument",
CommandName = "commandName"
};
dinamikButon.Click += dinamikButon_Click;
panel1.Controls.Add( dinamikButon );
i++;
}
void dinamikButon_Click( object sender, EventArgs e )
{
Label1.Text = "Merhaba dinamik butondan geliyorum.";
}
}
}
that's because when the page posts back the button doesn't exist. You have to create the buttons on page Load or PreInit. Microsoft suggests PreInit You can dynamically set a master page or a theme for the requested page, and create dynamic controls.
int i = 1;
Button dinamikButon;
private void Page_PreInit(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
CreateButton();
}
}
protected void btnStatik_Click( object sender, EventArgs e )
{
CreateButton();
}
private void CreateButton()
{
dinamikButon = new Button
{
Text = "Dinamik" + i,
ID = "btnDinamik" + i,
CommandArgument = "commandArgument",
CommandName = "commandName"
};
dinamikButon.Click += dinamikButon_Click;
panel1.Controls.Add( dinamikButon );
i++;
}
Update:
Do do what you've asked now we have to specify that the button has been created using either viewstate, querystring or session.
In this example I'll use a session:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Page.IsPostBack)
{
if (Session["Created"] != null)
{
CreateButton();
}
}
}
private void CreateButton()
{
dinamikButon = new Button
{
Text = "Dinamik" + i,
ID = "btnDinamik" + i,
CommandArgument = "commandArgument",
CommandName = "commandName"
};
Panel1.Controls.Add(dinamikButon);
dinamikButon.Click += dinamikButon_Click;
i++;
Session["Created"] = "true";
}
private void dinamikButon_Click(object sender, EventArgs e)
{
//your action here
}
To fill value in "Label1" control using dynamic button
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DynamicCtrl._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" language="javascript">
function dynamicevnt() {
document.getElementById("Label1").innerHTML = "Merhaba dinamik butondan geliyorum.";
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnStatik" runat="server" Text="Click" OnClick="btnStatik_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Panel ID="panel1" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DynamicCtrl
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnStatik_Click(object sender, EventArgs e)
{
CreateButton();
}
private void CreateButton()
{
int i = 1;
Button dinamikButon = new Button();
dinamikButon.Text = "Dinamik" + i;
dinamikButon.ID = "btnDinamik" + i;
dinamikButon.OnClientClick = "return dynamicevnt();";
dinamikButon.Click += new EventHandler(dinamikButon_Click);
panel1.Controls.Add(dinamikButon);
i++;
}
protected void dinamikButon_Click(object sender, EventArgs e)
{
Label1.Text = "Merhaba dinamik butondan geliyorum.";
}
}
}

Sending data from usercontrol to main page on postback

I want to create a usercontrol for retrieving and filtering a dataset. The problem I'm having is that the controls to be populated are not in the usercontrol, but on the main page on which the usercontrol exists. The reason for this is that the controls that are to be populated are different on each page.
If I could get some help solving the following simplified problem, I will probably be on my way to solve the bigger problem:
<body>
<form id="form1" runat="server">
<div>
<uc1:uc1 ID="uc1" runat="server" />
<asp:Label ID="lbl" runat="server"></asp:Label>
</div>
</form>
</body>
What I want to do is set the label on the main page by clicking a button on the usercontrol. Problem I'm having is that the usercontrol postback happens after the main page.
Thanks!
I managed to solve it by calling Parent.Page. I also implemented an interface so that I can call different pages' methods from the user control. Not sure if it's a good solution, but it will do:
interface IFilter
{
void SetLabel(string text);
}
public partial class uc : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
((IFilter)Parent.Page).SetLabel("changed");
}
}
public partial class _Default : System.Web.UI.Page , IFilter
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
public void SetLabel(string text)
{
lbl.Text = text;
}
}
create a custom event in user control
create a property in usercontrol
fire the event when the button is clicked & set the property value
access the property in the main page in user control's eventhandler

How to add event handler to html tag in asp.net code behind?

I have this html.
<a id="a1" runat=server>
I want to add event handler to this <a> tag using code behind.
If a user press this tag, it should cause an event on the server-side.
For an easy solution there is the linkbutton on the toolbox which shows as a link but reacts as a button.
I'm not sure what you are trying to accomplish, but it seems that you want something like this:
On the .ASPX page
<asp:LinkButton ID="myLinkButton" OnClick="myLinkButton_Click" runat="server"></asp:LinkButton>
In the .ASPX.CS
protected void Page_Load(object sender, EventArgs e)
{
myLinkButton.CommandArgument = "1";
}
protected void myLinkButton_Click(object sender, EventArgs e)
{
int myLinkButtonID = Convert.ToInt32(((LinkButton)sender).CommandArgument);
}
Try this,
Html Tag:
<a id="a1" runat="server">
Code Behind:
protected void Page_Init(object sender, EventArgs e)
{
a1.ServerClick += new EventHandler(a1_ServerClick);
}
protected void a1_ServerClick(object sender, EventArgs e)
{
//Your Code here....
}
This will Create a Click Event for the <a> tag.

Resources