for Preventing:
Suppose there is a textbox control called txtDemo. Add below line of code in the page_load method.
txtDemo.Attributes.Add("onkeypress", "return noenter()");
< script type="text/javascript" >
function noenter() {
return !(window.event && window.event.keyCode == 13); }
< /script >
For Triggering:
Option 1:
Suppose there is a textbox control called txtDemo and a button control namely btnSubmit. Add below lines of code in the page_load method.
txtDemo.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
btnSubmit.Attributes.Add("onclick", "return validate_Data();");
< script type="text/javascript" >
function clickButton(e, buttonid)
{
var bt = document.getElementById(buttonid);
if (typeof bt == 'object')
{
if (event.keyCode == 13)
{
bt.click();
return false;
}
}
}
< /script >
Option 2:
In case of IE4+
Suppose there is a textbox control called txtDemo. Add below line of code in the page_load method.
txtDemo.Attributes.Add("onkeypress", "return entsub(this.form)");
< script type="text/javascript" >
function noenter() {
return !(window.event && window.event.keyCode == 13); }
< /script >
In Case of Netscape Navigator
txtDemo.Attributes.Add("onkeypress", "return entsub(event,this.form)");
< script type="text/javascript" >
function entsub(event,ourform) {
if (event && event.which == 13)
ourform.submit();
else
return true;}
< /script >
Pagination in Repeater control using PagedDataSource
PagedDataSource Class
Encapsulates the paging-related properties of a data-bound control (such as DataGrid, GridView, DetailsView, and FormView) that allow it to perform paging. This class cannot be inherited.
Below is the source code to demonstrate the use of PagedDataSource for implementing pagination in repeater control.
fileName : PagedDataSourceDemo.aspx
< table id="tbl1" cellspacing="0" >
< asp:Repeater ID="rptr1" runat="server" >
< ItemTemplate>
< tr >
< td " >
< asp:Label ID="lblId" runat="server" Text='<%# (Eval("ID") != null && !Eval("ID").ToString().Trim().Equals("")?Eval("ID"):"") %>'>
< /td >
< td >
< asp:Label ID="lblName" runat="server" Text='<%# (Eval("Name") != null && !Eval("Name").ToString().Trim().Equals("")?Eval("Name"):"") %>' >< /asp:Label >
< /a >
< /td >
< /tr >
< /ItemTemplate >
< /asp:Repeater >
< /table >
< table id="tbl2" cellspacing="0" >
< tr >
< td >
< asp:Repeater id="rptr2" runat="server" >
< /asp:Repeater >
< /td >
< /tr >
< /table >
< script runat="server" >
private DataTable _data = null;
private PagedDataSource _objPds;
private LinkButton _linkMore = null;
private LinkButton _linkPrevious = null;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["page"] != null && !Request.QueryString["page"].ToString().Equals(""))
{
CurrentPage = Int32.Parse(Request.QueryString["page"].ToString()) - 1;
LoadData();
}
if (Request.QueryString["action"] != null)
{
if (Request.QueryString["action"].ToString().Trim().Equals("next"))
{
CurrentPage += 1;
}
if (Request.QueryString["action"].ToString().Trim().Equals("previous"))
{
CurrentPage -= 1;
}
LoadData();
}
if (!IsPostBack)
LoadData();
}
private void LoadData()
{
_data = ((DataTable)Cache.Get("MyData"));
if (_data != null && _data.Rows.Count > 0)
{
rptr1.DataSource = CreatePaging(_data);
rptr1.DataBind();
}
}
protected PagedDataSource CreatePaging(DataTable dt)
{
CreatePagingButton(dt);
_objPds = new PagedDataSource();
_objPds.DataSource = dt.DefaultView;
_objPds.AllowPaging = true;
_objPds.PageSize = 10;
_objPds.CurrentPageIndex = CurrentPage;
if (_linkMore != null) _linkMore.Visible = !_objPds.IsLastPage;
if (_linkPrevious != null) _linkPrevious.Visible = !_objPds.IsFirstPage;
return _objPds;
}
public int CurrentPage
{
get
{
object o = this.ViewState["_CurrentPage"];
if (o == null)
return 0;
else
return (int)o;
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}
protected void CreatePagingButton(DataTable TempTable)
{
if (TempTable != null && TempTable.Rows.Count > 10)
{
if (rptr2.HasControls())
rptr2.Controls.Clear();
_linkPrevious = new LinkButton();
_linkPrevious.ID = "lnkPrevious";
_linkPrevious.Text = "Previous";
_linkPrevious.Visible = false;
_linkPrevious.PostBackUrl = "~/PagedDataSourceDemo.aspx?action=previous";
rptr2.Controls.Add(_linkPrevious);
Label lblPrevious = new Label();
lblPrevious.ID = "lblprevious";
lblPrevious.Text = " | ";
lblPrevious.Visible = true;
rptr2.Controls.Add(lblPrevious);
int i = 0;
for (i = 0; i < TempTable.Rows.Count / 10; i++)
{
LinkButton linkButton = new LinkButton();
linkButton.ID = "Articles" + i;
linkButton.Text = (i + 1).ToString();
linkButton.Visible = true;
linkButton.PostBackUrl = "~/PagedDataSourceDemo.aspx?page=" + (i + 1);
rptr2.Controls.Add(linkButton);
Label lbl = new Label();
lbl.ID = "lbl" + i;
lbl.Text = " | ";
lbl.Visible = true;
rptr2.Controls.Add(lbl);
}
if (TempTable.Rows.Count % 10 > 0)
{
LinkButton linkButton = new LinkButton();
linkButton.ID = "Page" + i;
linkButton.Text = (i + 1).ToString();
linkButton.Visible = true;
linkButton.PostBackUrl = "~/PagedDataSourceDemo.aspxx?page=" + (i + 1);
rptr2.Controls.Add(linkButton);
Label lbl = new Label();
lbl.ID = "lbl" + i;
lbl.Text = " | ";
lbl.Visible = true;
rptr2.Controls.Add(lbl);
}
_linkMore = new LinkButton();
_linkMore.ID = "lnkMore";
_linkMore.Text = "Next";
_linkMore.Visible = true;
_linkMore.PostBackUrl = "~/PagedDataSourceDemo.aspx?action=next";
rptr2.Controls.Add(_linkMore);
}
}
< /script >
Encapsulates the paging-related properties of a data-bound control (such as DataGrid, GridView, DetailsView, and FormView) that allow it to perform paging. This class cannot be inherited.
Below is the source code to demonstrate the use of PagedDataSource for implementing pagination in repeater control.
fileName : PagedDataSourceDemo.aspx
< table id="tbl1" cellspacing="0" >
< asp:Repeater ID="rptr1" runat="server" >
< ItemTemplate>
< tr >
< td " >
< asp:Label ID="lblId" runat="server" Text='<%# (Eval("ID") != null && !Eval("ID").ToString().Trim().Equals("")?Eval("ID"):"") %>'>
< /td >
< td >
< asp:Label ID="lblName" runat="server" Text='<%# (Eval("Name") != null && !Eval("Name").ToString().Trim().Equals("")?Eval("Name"):"") %>' >< /asp:Label >
< /a >
< /td >
< /tr >
< /ItemTemplate >
< /asp:Repeater >
< /table >
< table id="tbl2" cellspacing="0" >
< tr >
< td >
< asp:Repeater id="rptr2" runat="server" >
< /asp:Repeater >
< /td >
< /tr >
< /table >
< script runat="server" >
private DataTable _data = null;
private PagedDataSource _objPds;
private LinkButton _linkMore = null;
private LinkButton _linkPrevious = null;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["page"] != null && !Request.QueryString["page"].ToString().Equals(""))
{
CurrentPage = Int32.Parse(Request.QueryString["page"].ToString()) - 1;
LoadData();
}
if (Request.QueryString["action"] != null)
{
if (Request.QueryString["action"].ToString().Trim().Equals("next"))
{
CurrentPage += 1;
}
if (Request.QueryString["action"].ToString().Trim().Equals("previous"))
{
CurrentPage -= 1;
}
LoadData();
}
if (!IsPostBack)
LoadData();
}
private void LoadData()
{
_data = ((DataTable)Cache.Get("MyData"));
if (_data != null && _data.Rows.Count > 0)
{
rptr1.DataSource = CreatePaging(_data);
rptr1.DataBind();
}
}
protected PagedDataSource CreatePaging(DataTable dt)
{
CreatePagingButton(dt);
_objPds = new PagedDataSource();
_objPds.DataSource = dt.DefaultView;
_objPds.AllowPaging = true;
_objPds.PageSize = 10;
_objPds.CurrentPageIndex = CurrentPage;
if (_linkMore != null) _linkMore.Visible = !_objPds.IsLastPage;
if (_linkPrevious != null) _linkPrevious.Visible = !_objPds.IsFirstPage;
return _objPds;
}
public int CurrentPage
{
get
{
object o = this.ViewState["_CurrentPage"];
if (o == null)
return 0;
else
return (int)o;
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}
protected void CreatePagingButton(DataTable TempTable)
{
if (TempTable != null && TempTable.Rows.Count > 10)
{
if (rptr2.HasControls())
rptr2.Controls.Clear();
_linkPrevious = new LinkButton();
_linkPrevious.ID = "lnkPrevious";
_linkPrevious.Text = "Previous";
_linkPrevious.Visible = false;
_linkPrevious.PostBackUrl = "~/PagedDataSourceDemo.aspx?action=previous";
rptr2.Controls.Add(_linkPrevious);
Label lblPrevious = new Label();
lblPrevious.ID = "lblprevious";
lblPrevious.Text = " | ";
lblPrevious.Visible = true;
rptr2.Controls.Add(lblPrevious);
int i = 0;
for (i = 0; i < TempTable.Rows.Count / 10; i++)
{
LinkButton linkButton = new LinkButton();
linkButton.ID = "Articles" + i;
linkButton.Text = (i + 1).ToString();
linkButton.Visible = true;
linkButton.PostBackUrl = "~/PagedDataSourceDemo.aspx?page=" + (i + 1);
rptr2.Controls.Add(linkButton);
Label lbl = new Label();
lbl.ID = "lbl" + i;
lbl.Text = " | ";
lbl.Visible = true;
rptr2.Controls.Add(lbl);
}
if (TempTable.Rows.Count % 10 > 0)
{
LinkButton linkButton = new LinkButton();
linkButton.ID = "Page" + i;
linkButton.Text = (i + 1).ToString();
linkButton.Visible = true;
linkButton.PostBackUrl = "~/PagedDataSourceDemo.aspxx?page=" + (i + 1);
rptr2.Controls.Add(linkButton);
Label lbl = new Label();
lbl.ID = "lbl" + i;
lbl.Text = " | ";
lbl.Visible = true;
rptr2.Controls.Add(lbl);
}
_linkMore = new LinkButton();
_linkMore.ID = "lnkMore";
_linkMore.Text = "Next";
_linkMore.Visible = true;
_linkMore.PostBackUrl = "~/PagedDataSourceDemo.aspx?action=next";
rptr2.Controls.Add(_linkMore);
}
}
< /script >
~ 2 comments: ~
How to use google maps
Below code for anchor tag will open the google map of the location in another window.
< a id="lnkMap" href='http://maps.google.com/maps?q=Street+City+State_Province+Zip_Postal_Code+Country' target="_blank" >Map/Directions< /a >
As the query string, you have to pass the adderss of the location seperated by '+' to the 'maps.google.com/maps' url.
< a id="lnkMap" href='http://maps.google.com/maps?q=Street+City+State_Province+Zip_Postal_Code+Country' target="_blank" >Map/Directions< /a >
As the query string, you have to pass the adderss of the location seperated by '+' to the 'maps.google.com/maps' url.
~ No comments: ~
Using Web Parts with SQL Server 2000
Many features of ASP.NET rely on intrinsic application services such as membership, roles, profiles, and personalization. The application services use providers, objects that persist application service data in long-term storage. For example, the ASP.NET personalization service uses a personalization provider to save personalized user settings on Web pages.
Each application service uses one provider to persist the service's data in a particular kind of data store. For each service, a SQL provider is included and configured as the default provider to persist the data in a Microsoft SQL Server database.
Note:
To persist ASP.NET application services data in another data store besides SQL Server (such as a Microsoft Access database, XML files, or other RDBMS systems), you must create a separate provider for each kind of data store. To create a custom provider, you can inherit from the base provider for a particular application service, and extend it to handle the data for the service in whatever type of data store you plan to use. For example, to create an Access database provider for the membership service, you could inherit from the MembershipProvider base class, and enable it to persist membership data in Access.
A configured personalization provider and database. Web Parts personalization is enabled by default, and it uses the SQL personalization provider (SqlPersonalizationProvider) with the Microsoft SQL Server Standard Edition to store personalization data. This walkthrough uses SSE and the default SQL provider. If you have SSE installed, no configuration is needed. SSE is available with Microsoft Visual Studio 2005 as an optional part of the installation, or as a free download. For details, see the Microsoft SQL Server 2005 Express Edition Web page. To use one of the full versions of SQL Server, you must install and configure an ASP.NET application services database, and configure the SQL personalization provider to connect to that database. For details, see Creating and Configuring the Application Services Database for SQL Server. You can also create and configure a custom provider to use with other, non-SQL databases or storage solutions. For details and a code example see Implementing a Membership Provider.
Reference : http://msdn2.microsoft.com/en-us/library/2fx93s7w.aspx
By default, Web Parts in ASP.NET 2.0 are configured to use the default SQL Express Personalization Provider for persisting Web Parts information. If you wish to use SQL Server 2000 together with Web Parts, you need to perform the following steps:
1. Run the aspnet_regsql.exe tool located in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\ to add the aspnetdb database into SQL Server 2000.
The aspnet_regsql utility is a mixed mode (both graphical- and command-line-based) tool that lets you configure SQL Server for use with your ASP.NET application.
You run the aspnet_regsql tool like this:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\>aspnet_regsql
If you invoke aspnet_regsql without any command options, you will see a wizard guiding you through the steps. Select the "Configure SQL Server for application services" option in the wizard window. Click Next and then specify your SQL Server 2000 name. Click Next and then Close.
2. In Web.config, add the following to change the default SQL Express Personalization Provider to the new SQL Server Personalization Provider:
Note: In the ConnectionString section user name and password can be changed accordingly.
< connectionStrings >
< add name="SQLConnString"
connectionString="Data Source=your_server_name;Initial
Catalog=aspnetdb;Integrated Security=false;uid=sa;pwd=kkak"
providerName="System.Data.SqlClient" / >
< /connectionStrings >
< system.web >
..
..
< webParts >
< personalization
defaultProvider="SqlPersonalizationProvider" >
< providers>
< add name="SqlPersonalizationProvider"
type="System.Web.UI.WebControls.
WebParts.SqlPersonalizationProvider"
connectionStringName="SQLConnString"
applicationName="/" / >
< /providers >
< authorization >
< deny users="*" verbs="enterSharedScope" / >
< allow users="*" verbs="modifyState" / >
< /authorization >
< /personalization >
< /webParts >
< /system.web >
The Web Parts information will now be stored in SQL Server 2000 rather than the ASPNETDB.MDF database.
Each application service uses one provider to persist the service's data in a particular kind of data store. For each service, a SQL provider is included and configured as the default provider to persist the data in a Microsoft SQL Server database.
Note:
To persist ASP.NET application services data in another data store besides SQL Server (such as a Microsoft Access database, XML files, or other RDBMS systems), you must create a separate provider for each kind of data store. To create a custom provider, you can inherit from the base provider for a particular application service, and extend it to handle the data for the service in whatever type of data store you plan to use. For example, to create an Access database provider for the membership service, you could inherit from the MembershipProvider base class, and enable it to persist membership data in Access.
A configured personalization provider and database. Web Parts personalization is enabled by default, and it uses the SQL personalization provider (SqlPersonalizationProvider) with the Microsoft SQL Server Standard Edition to store personalization data. This walkthrough uses SSE and the default SQL provider. If you have SSE installed, no configuration is needed. SSE is available with Microsoft Visual Studio 2005 as an optional part of the installation, or as a free download. For details, see the Microsoft SQL Server 2005 Express Edition Web page. To use one of the full versions of SQL Server, you must install and configure an ASP.NET application services database, and configure the SQL personalization provider to connect to that database. For details, see Creating and Configuring the Application Services Database for SQL Server. You can also create and configure a custom provider to use with other, non-SQL databases or storage solutions. For details and a code example see Implementing a Membership Provider.
Reference : http://msdn2.microsoft.com/en-us/library/2fx93s7w.aspx
By default, Web Parts in ASP.NET 2.0 are configured to use the default SQL Express Personalization Provider for persisting Web Parts information. If you wish to use SQL Server 2000 together with Web Parts, you need to perform the following steps:
1. Run the aspnet_regsql.exe tool located in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\ to add the aspnetdb database into SQL Server 2000.
The aspnet_regsql utility is a mixed mode (both graphical- and command-line-based) tool that lets you configure SQL Server for use with your ASP.NET application.
You run the aspnet_regsql tool like this:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\>aspnet_regsql
If you invoke aspnet_regsql without any command options, you will see a wizard guiding you through the steps. Select the "Configure SQL Server for application services" option in the wizard window. Click Next and then specify your SQL Server 2000 name. Click Next and then Close.
2. In Web.config, add the following to change the default SQL Express Personalization Provider to the new SQL Server Personalization Provider:
Note: In the ConnectionString section user name and password can be changed accordingly.
< connectionStrings >
< add name="SQLConnString"
connectionString="Data Source=your_server_name;Initial
Catalog=aspnetdb;Integrated Security=false;uid=sa;pwd=kkak"
providerName="System.Data.SqlClient" / >
< /connectionStrings >
< system.web >
..
..
< webParts >
< personalization
defaultProvider="SqlPersonalizationProvider" >
< providers>
< add name="SqlPersonalizationProvider"
type="System.Web.UI.WebControls.
WebParts.SqlPersonalizationProvider"
connectionStringName="SQLConnString"
applicationName="/" / >
< /providers >
< authorization >
< deny users="*" verbs="enterSharedScope" / >
< allow users="*" verbs="modifyState" / >
< /authorization >
< /personalization >
< /webParts >
< /system.web >
The Web Parts information will now be stored in SQL Server 2000 rather than the ASPNETDB.MDF database.