ASP.NET refer a control from .aspx file - asp.net

I have the following code :
<html xmlns="http://www.w3.org/1999/xhtml">
<body style="height: 1336px">
<input type="hidden" id="loadingtime"/>
.
.
.
.
</body>
</html>
and the Code.cs file is :
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string s = "";
HiddenField t = (HiddenField)Page.FindControl("loadingtime");
s = t.Value;
Response.Write("<script language='javascript'>alert('It took : " + s + "')</script>");
}
}
I get error
Any idea?
EDIT :
The above problem is successfully solved by
Now when I retrieve the value of loading time, the value is empty string. Can anyone please solve this one?

Your input should have runat="server" enabling it to be parsed by the ASP.NET runtime i.e.
<input type="hidden" runat="server" id="loadingtime"/>
You can then refer to the control as you would normally in code behind using:
HiddenField t = (HiddenField)loadingtime;
AS LONG AS it is not wrapped in another server control such as an asp:repeater in which case you would need to use alternative method of traversing the control hierarchy

Related

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.

Asp.net form links

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
}
}
}

C# Calendar Tool ASP.NET

So I'm trying to create a calendar tool that pulls in dates from an SQL Database.
I've created a TableAdapter which is here: http://bit.ly/KRaTvr
This is the small bit of ASP I have to create my calendar:
<asp:Calendar ID="calEvents" runat="server"> </asp:Calendar>
Then I have the C# in my project used to pull the information through.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using BreakyBottomTableAdapters;
public partial class Events : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Data Table
BreakyBottom table;
//Table Adapter
BreakyBottomTableAdapters.ScheduleDate ta;
ta = new BreakyBottomTableAdapters.ScheduleDate();
//Populate the table using the Table Adapter passing current date
table = ta.GetData(e.Day.Date);
//Check the number of rows in the data table
if (table.Rows.Count > 0)
{
//Change the background colour to gray
e.Cell.BackColor = System.Drawing.Color.Gray;
//Allow the Day to be selected
e.Day.IsSelectable = true;
}
}
}
Now I know I'm missing something but I can't for the life of my figure out what it is.
Here is a screenshot of the compile error I get: http://bit.ly/ISuVsT - I know I'm probably missing something insanely obvious but any assistance would be appreciated.
Best Regards
The compiler is telling you that EventArgs does not have any member called Day which you are apparently using in your code incorrectly:
protected void Page_Load(object sender, EventArgs e)
{
....
table = ta.GetData(e.Day.Date); //WRONG: Day is not a member of EventArgs
}
If the idea is to use the Current date, as you mentioned, then do this:
table = ta.GetData(DateTime.Now);

Can't override preload because not function?

Another .net newbie question
I created a class that inherits System.Web.UI.Page with the intention of overriding the preload and Unload functions. But Visual Studios complains I can't override because "System.Web.UI.Page.PreLoad" is not a function. What's wrong with my code? Here it is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class BTPage : System.Web.UI.Page
{
protected SqlConnection cnx;
public override void PreLoad(object sender, EventArgs e)
{
cnx = new SqlConnection(ConfigurationManager.AppSettings["btdatabase"]);
cnx.Open();
}
protected void Unload(object sender, EventArgs e)
{
cnx.Close();
}
}
If there's anything else alarming about my code, please tell me. I'm new to .Net and I'm not sure if I'm doing things in the ".NET way".
What I also want to do is have every web page inherit from BTPage so that they all open a connection to the database on preload.
PreLoad is not a method of the Page type. You need to override OnPreLoad

Any Way To get The selected Indices only in CheckBoxList?

I want to get the selected Indices or Items only in checkBox lsit instead of iterating through each item as Like is there in ListBox.
I am Getting Selected Value In tWo case In this way:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ChkBxList_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string li = "";
foreach(ListItem lt in CheckBoxList1.Items)
{
if(lt.Selected)
li += lt.Text;
}
Response.Write(li);
}
protected void Button2_Click(object sender, EventArgs e)
{
string li = "";
foreach (int lt in ListBox1.GetSelectedIndices())
{
li += ListBox1.Items[lt].Text;
}
Response.Write(li);
}
}
In ListBox we have the Option To get Seected Only Items is there any For Check Box List?
I dont think there is but you could use this extension method that does exactly that:
public List<ListItem> GetSelectedItems(this CheckBoxList checkBoxList)
{
List<ListItem> list = new List<ListItem>();
foreach(ListItem lt in checkBoxList)
{
if(lt.Selected)
list.Add(lt);
}
return list;
}
//Call it like this
checkBoxList.GetSelectedItems();
You actually answered your own question. There is no way to get the selected only items in the CheckBoxList control, unlike the ListBox control.
This article has an explanation and a work around help method.
http://weblogs.asp.net/jgalloway/archive/2005/10/02/426346.aspx

Resources