drop down menu selected w/ source code
- From: Slickuser <slick.users@xxxxxxxxx>
- Date: Tue, 7 Oct 2008 17:59:44 -0700 (PDT)
Hi,
Currently, I can add values the drop down menu list inside the grid
view.
I want my value from SQL database to be select in the drop down menu
list (COLOR).
Also, once the user change to the value, update it back to database.
How can I achieve this? Thanks a lot.
select * from name;
+----+--------+-------+
| ID | OWNER | COLOR |
+----+--------+-------+
| 1 | Jim | red |
| 2 | Andrea | green |
| 3 | Dan | white |
| 4 | Tim | blue |
+----+--------+-------+
Below are working code to display values from database and generic
drop down menu list.
Default.aspx
<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="This is a
test!"></asp:Label>
</div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="OWNER" HeaderText="OWNER" /
<asp:BoundField DataField="COLOR"
HeaderText="COLOR_VIEW" />
<asp:TemplateField>
<itemtemplate>
<asp:DropDownList id="colorDropList"
runat="server" AutoPostBack="true"
OnSelectedIndexChanged="colorDropList_SelectedIndexChanged">
</asp:DropDownList>
</itemtemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!(Page.IsPostBack))
{
// SQL query
string sqlStr = @"
SELECT ID, OWNER, COLOR
FROM name;
";
string connInfo = "Data Source=xxx;Initial
Catalog=fruits;User ID=xxx;Password=xxx";
SqlConnection sqlConn = new SqlConnection(connInfo);
SqlCommand sqlCmd = new SqlCommand(sqlStr, sqlConn);
try
{
sqlConn.Open();
SqlDataAdapter sqlData = new SqlDataAdapter();
sqlData.SelectCommand = sqlCmd;
DataTable dataTbl = new DataTable();
sqlData.Fill(dataTbl);
GridView1.DataSource = dataTbl;
GridView1.DataBind();
DropDownList dropdownlist = new DropDownList();
foreach (GridViewRow row in GridView1.Rows)
{
dropdownlist =
((DropDownList)row.FindControl("colorDropList"));
string occur_list =
"red,white,blue,green,purple,black";
string[] colorFreq = occur_list.Split(',');
for (int i = 0; i < colorFreq.Length; i++)
{
dropdownlist.DataValueField = colorFreq[i];
dropdownlist.Items.Add(colorFreq[i]);
}
}
}//try
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
sqlConn.Close();
sqlConn.Dispose();
}
}
}
protected void colorDropList_SelectedIndexChanged(object sender,
EventArgs e)
{
}
}
.
- Prev by Date: Re: How to loop through <configSections> elements?
- Next by Date: Re: readonly issue
- Previous by thread: Locking an ASP Control
- Next by thread: Implicitly typed local variables, why local only?
- Index(es):
Relevant Pages
|