所以,
我的事件page_Load中有一个SQL查询结果,我有一个下拉列表。我想要做的是当我的下拉列表中的selectedIndexChanges事件被触发并将其放在文本框中时获取当前值。
查看下面的代码。
我的下拉列表定义:<asp:DropDownList ID="ddProfil" runat="server" Width="550" AutoPostBack="True" OnSelectedIndexChanged="ddProfil_SelectedIndexChanged" >
我的网页加载事件:protected void Page_Load(object sender, EventArgs e)
{
loadDDProfil(Request.QueryString["sitename"]);
}
加载下拉列表的功能:
protected void loadDDProfil(string siteName)
{
SqlCommand requete = new SqlCommand();
requete.Connection = connWeb.ConnectionToDb;
requete.CommandType = System.Data.CommandType.Text;
string strReq = "ps_get_all_IndexProfil " + "MRF";
requete.CommandText = strReq;
DataTable dtPrf = connWeb.ExecuteQueryDB(requete);
SqlDataAdapter adapter = new SqlDataAdapter(requete);
adapter.Fill(dtPrf);
var dtSource = from p in dtPrf.AsEnumerable()
select new {
ind = p.Field("IndexProfil"),
DisplayedField = String.Format("{0} [ {1} ]", p.Field("NomProfil"), p.Field("Description"))
};
ddProfil.DataSource = dtSource;
ddProfil.DataValueField = "ind";
ddProfil.DataTextField = "DisplayedField";
ddProfil.DataBind();
ddProfil.SelectedIndexChanged += ddProfil_SelectedIndexChanged;
public void ddProfil_SelectedIndexChanged(object sender, EventArgs e)
{
string s = ddProfil.SelectedValue;
}
谢谢你的帮助。
答案 0 :(得分:1)
在您的网页加载事件中尝试添加以下内容:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadDDProfil(Request.QueryString["sitename"]);
}
}