想按字母顺序排列下拉列表 [英] Want to arrange dropdown list by alphabetically
本文介绍了想按字母顺序排列下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码我想按字母顺序排列下拉列表,
我是c#&的初学新手asp.net,
请详细解释
我尝试过:
This is my code i want to sort dropdownlist in alphabetically format ,
I am very new beginner of c# & asp.net,
Kindly explain in detail
What I have tried:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Index : System.Web.UI.Page
{
JRDSQL jsql = new JRDSQL();
JRDPopup jpop = new JRDPopup();
SqlConnection con = new SqlConnection(@"Data Source=xxx.xxx.xxx.xxx,1434; Initial Catalog=apbills_Anay;UId=apbills_Anay;Pwd=Lootbazzar@12345;");
protected void Page_Load(object sender, EventArgs e)
{
bool blogin = false;
if (Session["SID"] != null)
{
if (Convert.ToInt32(Session["SID"].ToString()) > 0)
{
blogin = true;
}
}
if (blogin == true)
{
if (!IsPostBack)
{
}
}
else
{
Response.Redirect("Login.aspx", false);
}
//Checking Connection State and opening if closed
if (con.State == ConnectionState.Closed)
{
con.Open();
}
if (!Page.IsPostBack)
{
//Call countries DropDownList on page load event
BindCountry();
fillDDSMN();
}
}
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int locationId = Convert.ToInt32(ddlcountry.SelectedValue);
//Select all States corresponding to the selected Country
SqlDataAdapter adp = new SqlDataAdapter("select * from tbdealerlist where locationid= " + locationId, con);
DataSet ds = new DataSet();
adp.Fill(ds);
ddlstate.DataSource = ds;
ddlstate.DataTextField = "Dealer_Name";
ddlstate.DataValueField = "Dealer_Id";
ddlstate.DataBind();
ddlstate.Items.Insert(0, new ListItem("--Select--", "0"));
//If State is not selected then clear City DropDownList also
//if (ddlstate.SelectedValue == "0")
//{
// ddlCity.Items.Clear();
// ddlCity.Items.Insert(0, new ListItem("--Select--", "0"));
//}
}
catch (Exception ex)
{
//Printing any exception if occcured.
Response.Write("Error occured: " + ex.Message.ToString());
}
finally
{
//Close the connection
con.Close();
}
}
//protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
//{
// try
// {
// int Dealer_Id = Convert.ToInt32(ddlstate.SelectedValue);
// //Select all Cities corresponding to the selected State
// SqlDataAdapter adp = new SqlDataAdapter("select * from tbdealerlist where Dealer_id=" + Dealer_id, con);
// DataSet ds = new DataSet();
// adp.Fill(ds);
// ddlCity.DataSource = ds;
// ddlCity.DataTextField = "City_Name";
// ddlCity.DataValueField = "City_id_pk";
// ddlCity.DataBind();
// ddlCity.Items.Insert(0, new ListItem("--Select--", "0"));
// }
// catch (Exception ex)
// {
// Response.Write("Error occured : " + ex.Message.ToString());
// }
// finally
// {
// con.Close();
// }
//}
protected void BindCountry()
{
try
{
SqlDataAdapter adp = new SqlDataAdapter("select * from Location", con);
DataSet ds = new DataSet();
adp.Fill(ds);
ddlcountry.DataSource = ds;
ddlcountry.DataTextField = "Location";
ddlcountry.DataValueField = "LocationId";
ddlcountry.DataBind();
ddlcountry.Items.Insert(0, new ListItem("--Select--", "0"));
ddlstate.Items.Insert(0, new ListItem("--Select--", "0"));
//ddlCity.Items.Insert(0, new ListItem("--Select--", "0"));
}
catch (Exception ex)
{
Response.Write("Error occured : " + ex.Message.ToString());
}
finally
{
con.Close();
}
}
private void fillDDSMN()
{
SqlParameter[] paramdg = {new SqlParameter("@Mode","product")
};
jpop.PopupCombo(DDSMN, "sp_Registration", paramdg);
}
protected void submit_Click(object sender, EventArgs e)
{
try
{
SqlParameter[] paramsave = { new SqlParameter("@Mode","sale"),
// new SqlParameter("@saledate",txtsldate.Text.Trim()),
new SqlParameter("@sale_Invoice",txtslinvoice.Text.Trim()),
new SqlParameter("@Dealer_Name",ddlstate.SelectedValue),
new SqlParameter("@SProduct_name",DDSMN.SelectedValue),
new SqlParameter("@Model_name",DDSMN.SelectedValue),
new SqlParameter("@SProduct_Qty",txtslmdlqty.Text.Trim()),
new SqlParameter("@sale_rate",txtslrate.Text.Trim()),
//new SqlParameter("@Scredit",nmbcrd.Text.Trim()),
new SqlParameter("@Scash",nmbcsh.Text.Trim()),
// new SqlParameter("@Scash",nmbcsh.Text.Trim()),
//new SqlParameter("@Product_cat",ddSMC.SelectedValue),
new SqlParameter("daily_scash",nmbcsh.Text.Trim()),
//new SqlParameter("Daily_Scredit",nmbcrd.Text.Trim()),
};
int id = 0;
id = jsql.execStoredProcudure("sp_sale", paramsave);
if (id > 0)
{
JRDMessage.UserMessage(this, "Data submitted successfully !!!");
}
}
catch (Exception ex)
{
JRDMessage.UserMessage(this, ex.Message);
}
}
}
推荐答案
最简单的方法是在你的SQL中添加一个Order By子句。
具体来说;
The easiest method is to add an Order By clause to your SQL.
Specifically;
SqlDataAdapter adp = new SqlDataAdapter("select * from tbdealerlist where locationid = " + locationid + " ORDER BY Dealer_Name", con);
AND
AND
SqlDataAdapter adp = new SqlDataAdapter("select * from Location ORDER BY Location");
只需将ORDER BY添加到您的选择查询中。此外,您可以尝试添加DropDownList,它将按字母顺序按升序或降序排序。只是一个建议:)
Just add the ORDER BY to your select query. Also, you can try adding a DropDownList that will serve as sort alphabetically in Ascending or Descending order. Just a suggestion :)
这篇关于想按字母顺序排列下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文