This article will show you how you can populate data in
GridView on dropdown List selected role in asp.net using c#.net.
Some of my previous articles are as follows:
Random
Character and String Generation Using C#.Net, Windows Application and Linq ,
Strong
Random String Password Generation Using C#.Net, Display
Calendar Control Selected Date Into Javascript Alert Message In Asp.Net, Use
Of Css To Show Image However Effect In Asp.Net, Display
ModalPopupExtender on Page Load In Asp.Net Using AjaxControlToolkit, C#.Net,
Dragable
ModalPopupExtender In Asp.Net Using AjaxControlToolkit.
So for this article first we will create a table in out sql
db.
After creating sql table we will create a new asp.net
application and add the below code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Populate Data in GridView on DropdownList Selected Role in Asp.net Using C#.net
</title>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" cellpadding="4" cellspacing="4">
<tr>
<td align="center">
<b>USER DETAIL</b>
</td>
</tr>
<tr>
<td align="center">
<asp:DropDownList ID="DropDownList1" runat="server" Width="91px">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Admin</asp:ListItem>
<asp:ListItem>User</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Search" />
</td>
</tr>
<tr>
<td align="left">
<asp:GridView ID="GridView1" runat="server" Width="100%">
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
|
Now we will add the below code in your code behind page.
using System;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGridView("Select");
}
}
private
void BindGridView(string
userType)
{
SqlConnection
con = new SqlConnection(System.Configuration.ConfigurationManager.
ConnectionStrings["con"].ToString());
try
{
DataTable
objdt = new DataTable();
string
query = "";
if
(userType == "Select")
{
query = "select * from UserDetail;";
}
else
{
query = "select * from UserDetail where UserType='"
+ userType + "';";
}
SqlDataAdapter
da = new SqlDataAdapter(query,
con);
con.Open();
da.Fill(objdt);
con.Close();
if
(objdt.Rows.Count > 0)
{
GridView1.DataSource =
objdt;
GridView1.DataBind();
}
}
catch
{
con.Close();
}
}
protected
void Button1_Click(object
sender, EventArgs e)
{
BindGridView(DropDownList1.SelectedItem.ToString());
}
}
}
|
In above code I have created a method in which I have passed the user type to prepare the filter query and getting the data on the bases of user type.
Now we have done run the application and check the output.
i applied your code to display gridview on the selection of dropdown list . but it enters into infinite loop.
ReplyDeleteHi please let me know what the code you have used. please show me your code.
Delete