Saturday, 26 April 2014

Pass Selected Row of ASP.Net GridView control to another Page

http://www.aspsnippets.com/Articles/Pass-Selected-Row-of-ASPNet-GridView-control-to-another-Page.aspx

In one of my previous articles I have explained Pass ASP.Net GridView from one page to another page, now extending that article I am now explaining how to pass ASP.Net GridView Selected Row from one page to another.
 
HTML Markup
In the following HTML Markup there’s an Asp.Net GridView control with a Button to select the row. Also I have added a Button which will send the Asp.Net GridView Selected Row to the other page when clicked.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Font-Names="Arial"
Font-Size="10pt">
<Columns>
    <asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" />
    <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
    <asp:BoundField ItemStyle-Width="150px" DataField="PostalCode" HeaderText="PostalCode" />
    <asp:TemplateField>
    <ItemTemplate>
        <asp:Button ID="btnSelect" runat="server" Text="Select" CommandName = "Select" />
    </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSend" runat="server" Text="Send Selected Row" OnClick = "Send" />
 
 
Passing the Selected Row to the other page
When the send button is clicked it first checks whether the GridView Row has a Selected Row or not. If the GridView has a Selected Row it does a Server.Transfer to the Page2.aspx. I am doing Server.Transfer instead of Response.Redirect since with Server.Transfer we can reference the previous page and its controls. And if the user has not selected any row in the ASP.Net GridView we ask him to select one using a JavaScript alert.
Finally on Page2.aspx the data from the cells of the Selected Row of the ASP.Net GridView is displayed. 

C#
protected void Send(object sender, EventArgs e)
{
    if (GridView1.SelectedRow != null)
    {
        Server.Transfer("~/Page2.aspx");
    }
    else
    {
        ClientScript.RegisterStartupScript(this.GetType(), "alert""alert('Please select a row.')"true);
    }
}
 
 
VB.Net
 
Protected Sub Send(sender As Object, e As EventArgs)
    If GridView1.SelectedRow IsNot Nothing Then
        Server.Transfer("~/Page2.aspx")
    Else
        ClientScript.RegisterStartupScript(Me.[GetType](), "alert""alert('Please select a row.')"True)
    End If
End Sub
 
Now on Page2.aspx we fetch the ASP.Net GridView SelectedRow in the following way
 
C#
 
protected void Page_Load(object sender, EventArgs e)
{
    if (this.Page.PreviousPage != null)
    {
        GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
        GridViewRow selectedRow = GridView1.SelectedRow;
        Response.Write("CustomerId: " + selectedRow.Cells[0].Text + "<br />");
        Response.Write("City: " + selectedRow.Cells[1].Text + "<br />");
        Response.Write("PostalCode: " + selectedRow.Cells[2].Text);
    }
}
 
 
VB.Net
 
Protected Sub Page_Load(sender As Object, e As EventArgsHandles Me.Load
    If Me.Page.PreviousPage IsNot Nothing Then
        Dim GridView1 As GridView = DirectCast(Me.Page.PreviousPage.FindControl("GridView1"), GridView)
        Dim selectedRow As GridViewRow = GridView1.SelectedRow
        Response.Write("CustomerId: " + selectedRow.Cells(0).Text & "<br />")
        Response.Write("City: " + selectedRow.Cells(1).Text & "<br />")
        Response.Write("PostalCode: " + selectedRow.Cells(2).Text)
    End If
End Sub
 


Wednesday, 2 April 2014

Glossy accordian Vertical menus using Jquery,HTML and CSS

http://www.dynamicdrive.com/dynamicindex17/ddaccordionmenu-glossy.htm


Thursday, 27 March 2014

Populate One Asp.net Dropdown based on Selection in Another Dropdown


http://www.aspdotnet-suresh.com/2010/10/how-to-populate-dropdown-based-on-other.html



Introduction

Here I will explain how to populate one dropdown based on selection in another dropdown asp.net using c#.

Description

I have three dropdowns Country dropwdown, State dropdown, Region dropdown I need to populate states dropdown based on country dropdown and I need to populate region dropdown based on states dropdown for that what we have to do first design three tables in sql server with data like this 

CountryTable 

StateTable

RegionTable

After that design your aspx page like this 


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CasCading Dropdowns Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
Select Country:
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlCountry_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
Select State:
</td>
<td>
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlState_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
Select Region:
</td>
<td>
<asp:DropDownList ID="ddlRegion" runat="server"></asp:DropDownList>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

In code behind write like this 


private String strConnection = "Data Source=XZCBJ017550;Initial Catalog=MySamplesDB;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindContrydropdown();
}

}
/// <summary>
/// Bind COuntrydropdown
/// </summary>
protected void BindContrydropdown()
{
//conenction path for database
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from CountryTable", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryID";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select--""0"));
ddlState.Items.Insert(0, new ListItem("--Select--""0"));
ddlRegion.Items.Insert(0, new ListItem("--Select--""0"));

}
/// <summary>
/// Bind State Dropdown Based on CountryID
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from StateTable where CountryID="+CountryID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlState.DataSource = ds;
ddlState.DataTextField = "StateName";
ddlState.DataValueField = "StateID";
ddlState.DataBind();
ddlState.Items.Insert(0, new ListItem("--Select--""0"));
if(ddlState.SelectedValue=="0")
{
ddlRegion.Items.Clear();
ddlRegion.Items.Insert(0, new ListItem("--Select--""0"));
}

}
/// <summary>
/// Bind Region dropdown based on Re
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
int StateID = Convert.ToInt32(ddlState.SelectedValue);
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from RegionTable where StateID=" + StateID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlRegion.DataSource = ds;
ddlRegion.DataTextField = "RegionName";
ddlRegion.DataValueField = "RegionID";
ddlRegion.DataBind();
ddlRegion.Items.Insert(0, new ListItem("--Select--""0"));
}