Combat: ASP. NET DropDownList and ListBox are used together

Source: Internet
Author: User

2014-71 application of a portal network application sorting management

1: Receive parameters, initialize

int totalrows = 7;

int type = 0;

protected void Page_Load (object sender, EventArgs e)
{
Try
{
Type = Convert.ToInt32 (request.querystring["f_id"] = = null? "0": request.querystring["f_id"]);
Totalrows = Convert.ToInt32 ((request.querystring["columns"] = = null? "7": request.querystring["Columns"));
}
catch (Exception)
{
}
Finally
{
if (! Page.IsPostBack)
{
Bindtype (type. ToString ());
Bindapp (type. ToString ());
}
}

}

2: Binding Data Source (DropDownList and ListBox)

private void Bindtype (String type)
{
String SqlType = "SELECT ID as TypeId, Name as TypeName from View_dt_wkcommon_leveldicview where parentid= 4 order by Re Aloredernum ";

DataSet typeds = SqlHelper.ExecuteDataset (conn, CommandType.Text, SqlType);

if (typeds! = null && typeds. Tables[0]. Rows.Count > 0)
{

//Data source binding method

DataRow r = typeds. Tables[0]. NewRow ();
r["TypeId"] = "0";
r["TypeName"] = "Default";

Typeds. Tables[0]. Rows.insertat (r, 0);

Dropdownlist1.datasource = Typeds. Tables[0];
Dropdownlist1.datavaluefield = "TypeId";
Dropdownlist1.datatextfield = "TypeName";
Dropdownlist1.databind ();

  Options how to add
DropDownList1.Items.Clear ();

DROPDOWNLIST1.ITEMS.ADD (New ListItem ("Default", "0"));

for (int i = 0; i < typeds. Tables[0]. Rows.Count; i++)
{
ListItem item = new ListItem (typeds. Tables[0]. rows[i]["TypeName"]. ToString (), Typeds. Tables[0]. rows[i]["TypeId"]. ToString ());
DROPDOWNLIST1.ITEMS.ADD (item);
}

ListItem it = DropDownList1.Items.FindByValue (type);

if (it = null)
{
DropDownList1.Items.FindByValue (Type). Selected = true;
}
Else
{
Dropdownlist1.selectedindex = 0;
}

}
Else
{
ListItem item = new ListItem ("No Classification", "0");
LISTBOX1.ITEMS.ADD (item);
}

}

Binding app
private void Bindapp (String type)
{
String condition = Type = = "0"? String. Format ("S.isdefault=1"): String. Format ("a.type={0}", type);

String Sqlapp = String. Format ("Select S.id, A.name, A.type, S.sort from Dt_work_nh_selfsettings as S, Dt_work_nh_selfdefapps as a where s.appid = a.ID and S.createrid = {0} and {1} ORDER by sort ", UserId, condition);

DataSet Appds = SqlHelper.ExecuteDataset (conn, CommandType.Text, Sqlapp);

if (Appds! = null && Appds. Tables[0]. Rows.Count > 0)
{
ListBox1.Items.Clear ();

for (int i = 0; i < Appds. Tables[0]. Rows.Count; i++)
{
ListItem item = new ListItem (Appds. Tables[0]. rows[i]["Name"]. ToString (), Appds. Tables[0]. rows[i]["Id"]. ToString ());
LISTBOX1.ITEMS.ADD (item);
}

Insertline ();
Listbox1.selectedindex = 0;
Lbmsg. Text = string. Format ("There are {0} apps for this category," Appds. Tables[0]. Rows.Count);
Lbstep.text = "";
ISDISENABLEBTN (FALSE);
}
Else
{
ListBox1.Items.Clear ();
LISTBOX1.ITEMS.ADD ("No related data item! ");
Lbmsg. Text = "Hint: The app category is not currently included in any app!" ";
Lbstep.text = "";
ISDISENABLEBTN (TRUE);
}
}

3:dropdownlist Data linkage

Rebind classification Information
protected void DropDownList1_SelectedIndexChanged (object sender, EventArgs e)
{
Bindapp (Dropdownlist1.selectedvalue);
}

Description

The listbox sometimes needs to add additional options to differentiate groupings, such as dividing each 4 options into a group, adding dividers between groups and groups

such as:---------------({0}) Platoon---------------

static string line = "---------------({0}) platoon---------------";

//Remove each row of separator lines
private void Removeline ()
{
int rows = listbox1.items.count/totalrows;
int others = ListBox1.Items.Count% Totalrows;

for (int m = 1; m <= rows; m++)
{
ListBox1.Items.Remove (New ListItem (string. Format (line, M), "1");
}

if (Others > 0)
{
ListBox1.Items.Remove (New ListItem (string. Format (line, rows + 1), "-1"));
}
}

Add each row of divider lines
private void Insertline ()
{
int rows = listbox1.items.count/totalrows;
int others = ListBox1.Items.Count% Totalrows;

for (int m = 1, n = 0; m <= rows; m++, n++)
{
int index = m * Convert.ToInt32 (totalrows) + N;
ListBox1.Items.Insert (Index, new ListItem (string. Format (line, M), "1");
}

if (Others > 0)
{
ListBox1.Items.Insert (ListBox1.Items.Count, New ListItem (string. Format (line, rows + 1), "-1"));
}
}

The effect is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.