INITIAL CHAMBER:
Step1) Open Your Visual Studio 2010 and Create an Empty Website, Give a suitable name [ListBox_demo].
Step2) In Solution Explorer you get your empty website, Add a web form and SQL Database. By going like this –
For Web Form:
ListBox_demo (Your Empty Website) -> Right Click -> Add New Item -> Web Form. Name it as -> ListBox_demo.aspx.
For SQL Server Database:
Accordion_demo (Your Empty Website) -> Right Click -> Add New Item -> SQL Server Database. [Add Database inside the App_Data_folder].
DATABASE CHAMBER:
Step3) In Server Explorer, Click on your Database [Database.mdf] - -> Tables - -> Add New Table -:- Make table like this:
- Table - -> tbl_data [Don’t Forget to make ID as IS Identity -- True]
DESIGN CHAMBER:
Step4) Open your ListBox_demo.aspx file from solution Explorer and start design you’re Application:
Here is the Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox2" runat="server" DataSourceID="SqlDataSource1"
DataTextField="name" DataValueField="name"></asp:ListBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [name] FROM [tbl_data]"></asp:SqlDataSource>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Click Here " />
<br />
<br />
<br />
<br />
<br />
<asp:ListBox ID="ListBox1" runat="server" Width="100px"
></asp:ListBox>
</form>
</body>
</html>
We will take DataSource as SQL Server Database, we will not write any code for Sql Connection, and by just calling Datasource of Listbox it will make our work.
After this Press Next – Next , at the end it will ask you about your table, just select here tbl_data, see below image.
At last window of configure data source, you can test the query just press the - - “Test Query” - - It will fetch all the name in your Database.
CODE CHAMBER:
At Last open your ListBox_demo.aspx.cs file to write the code, for making the list box like we assume.
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (ListItem lst in ListBox2.Items)
{
if (lst.Selected== true)
{
ListBox1.Items.Add(lst.Text);
}
}
}
}
OUTPUT CHAMBER:
Hope you like this, Thank you for Reading. Have a nice day!
No comments:
Post a Comment