简体   繁体   English

如何使用参数ASP.NET填充下拉列表

[英]How to populate dropdown list with parameters ASP.NET

I have an aspx page with some inputs. 我有一些输入的aspx页面。 In one hand, i have the following two inputs to capture the range of dates: 一方面,我有以下两个输入来捕获日期范围:

<asp:TextBox ID="bDate" runat="server" TextMode="Date"></asp:TextBox>
<asp:TextBox ID="eDate" runat="server" TextMode="Date"></asp:TextBox>

and, at the bottom of the page, I have a dropdown that should be filled with a query like 并且在页面底部,我有一个下拉菜单,其中应填充查询

select xxx from table where beginDate >= bDate and endDate <= eDate

The goal is to have this dropdown correctly populated with no need to click on any button. 目标是无需单击任何按钮即可正确填充此下拉列表。

PS: I have all the code on page load and the query correctly running (only select) so I should only want to know the knowledge to get this done. PS:我具有页面加载和查询正常运行(仅选择)上的所有代码,因此我只想知道将其完成的知识。

You can use OnTextChanged="DateTextChanged_OnTextChanged" on one (or both) of your textbox if both have value. 如果两个文本框都具有值,则可以在其中一个(或两个)文本OnTextChanged="DateTextChanged_OnTextChanged"上使用OnTextChanged="DateTextChanged_OnTextChanged"

<asp:TextBox ID="bDate" runat="server" TextMode="Date" OnTextChanged="DateTextChanged_OnTextChanged" AutoPostBack="true"/>
<asp:TextBox ID="eDate" runat="server" TextMode="Date" OnTextChanged="DateTextChanged_OnTextChanged" AutoPostBack="true"/>

Then validate they have value in your code behind if they do call the business logic to get information to bind to your dropdown list. 然后,如果他们确实调用业务逻辑来获取绑定到您的下拉列表的信息,请验证它们是否在您的代码中具有价值。

protected void DateTextChanged_OnTextChanged(object sender, EventArgs e)
{
    // do validation run query     
}

NOTE: This with post back will reload the screen so your probably want to use an update panel or something similar. 注意:带回发功能会重新加载屏幕,因此您可能要使用更新面板或类似的东西。

hope that helps 希望能有所帮助

If you have a DropDownList called ddlSubject, you must do: 如果您有一个名为ddlSubject的DropDownList,则必须执行以下操作:

var query = select xxx from table where beginDate >= bDate and endDate <= eDate

ddlSubject.DataSource = query ;
ddlSubject.DataTextField = "SubjectNamne";
ddlSubject.DataValueField = "SubjectID";
ddlSubject.DataBind();

And be careful if is or isn't a PostBack process: What is a postback? 并且要注意是否是回发流程: 什么是回发?

Because this can clean completely your DropDownList. 因为这样可以完全清除您的DropDownList。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM