Hi Friends...
Here I explain full procedure of showing result on the selected value of dropdown in a grid view step by step on the onclick event.
Step 1: create a connection string in web.config page
<connectionStrings>
<add name="conn" providerName="System.Data.SqlClient"
connectionString="Data Source=Sk-Pc;uid=sa;pwd=12345;database=droplist"></add>
</connectionStrings>
<add name="conn" providerName="System.Data.SqlClient"
connectionString="Data Source=Sk-Pc;uid=sa;pwd=12345;database=droplist"></add>
</connectionStrings>
Step 2: create database
Firstly create a database and give it a name after then create a table I give it UserDetail name and keep them allow null and auto increment the ID column.
Step 3: HTML Design code goes here
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
.p {
padding: 55px 55px 55px 55px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="border: 2px groove #249fa3; height: 128px; width: 193px;" class="p">
<asp:DropDownList ID="DropDownList1" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Font-Overline="False" Height="31px" Width="102px">
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click1" BackColor="#249fa3" Font-Bold="True" ForeColor="White" Width="108px" Height="39px" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#249fa3"
BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
<AlternatingRowStyle BackColor="#249fa3" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#249fa3" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#249fa3" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
</div>
</form>
</body>
</html>
Step 4: Add some header files
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.Data;
Step 5: c# page code goes here
public partial class droplist : System.Web.UI.Page
{
string str = ("Data Source=Sheetal;Initial Catalog=droplist;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(str);
string com = "Select * from UserDetail";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("select * from UserDetail where id = '" + DropDownList1.SelectedValue + "'", con);
SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
Adpt.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
Label1.Text = "record found";
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
0 comments:
Post a Comment