Thursday, March 12, 2009

how to Post data from html using asp.net as a backend?

Some times you want to post your data from html page..
do this was a really simple task

<form method="post" action="URL" id="frmAddmissionEnquiry">
<input type="text" id="txtname" />
<input type="text" id="txtEmail" />
</form>


Capturing the same in asp page.
Request.Form["txtname"].ToString()


Returing back to the same form
Response.Redirect(Request.UrlReferrer.ToString());

Sunday, March 8, 2009

Paging in grid view using asp.net with c#?

There are certain scenario when we required pagination in gridview

Solution for this is very simple.

In .aspx page

<asp:GridView ID="GvEmployee" runat="server" AllowPaging="True" Width="100%" PageSize="10" OnPageIndexChanging="GvEmployee_PageIndexChanging">
<columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>

</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>

In c# Page
protected void GvEmployee_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GvEmployee.PageIndex = e.NewPageIndex;
databind();
}
catch (Exception ex)
{

}
}

Friday, March 6, 2009

How to bind datat using Gridview in asp.net with c#?

Today we will learn how to use gridview in asp.net using c# to display records using dataset,datatable?

In .cs page
GridView1.DataSource = DataTable or Dataset
GridView1.DataBind()

In aspx page
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="CandidateName" DataField="name" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText="Email" DataField="emailid" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText="DateTime" DataField="DateTime" ItemStyle-HorizontalAlign="Center" />

<asp:TemplateField HeaderText="Phone" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>

<asp:Label ID="CanName" runat="server" Text='<% #Bind("fname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<ItemTemplate>
<asp:Label ID="LblClass" runat="server" Text='<%#Bind("Class") %>'>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DDLClass" Width="100%" runat="server" DataSource='<%#Class%>' DataTextField="Class" DataValueField="id" >
<asp:listitem value="1" Text="1"></asp:listitem>
<asp:listitem value="2" Text="2"></asp:listitem>
<asp:listitem value="3" Text="3"></asp:listitem> </asp:DropDownList>
</EditItemTemplate>

</Columns>
</asp:GridView>

Thursday, March 5, 2009

Using string builder to write html on asp.net aspx page?

using System.Text;

StringBuilder sb = new StringBuilder();
sb.append("Your html body");


div.innerHTML=sb.ToString();

Tuesday, March 3, 2009

How to tell search engine the page has moved using 301 permanent redirect asp.net c#?

Many a times you have a situation where you have to rename your page or move your page to a new location.In short URL for that page is change.we can tell search engine that this page is redirected to a newer location using 301 status.

HttpContext context = HttpContext.Current;
if(url=="Your Old URL")
context.Response.Status = "301 Moved Permanently";
context.Response.AddHeader("Location", strUrl );
context.Response.End();
endif

Monday, March 2, 2009

How to generate unique number in asp.net using c#?

Many a times u want to generate unique number

string TranId = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Millisecond.ToString();

Sunday, March 1, 2009

Connection string in web.config asp.net c#

We can also specify connection string in our web.config file.
In asp.net 1.1 we can specify connection in 2 ways
1) In AppSettings
<appSettings>

<add key="ConnectionInfo" value="server=servername;uid=userid;pwd=password;Initial Catalog=database"/>
<appSettings>

2)connectionStrings
<add connectionString="Server=severname;Database=DB;UID=userid; pwd=password;" name="conn" providerName="MySql.Data.MySqlClient;"/>
</connectionStrings>