asp.net的ObjectDataSource传递从控制参数以及文本框 [英] asp.net objectdatasource pass parameter from control as well as textbox
问题描述
我配置了 GridView控件
来填补的数据 ObjectDataSource控件
。这 ObjectDataSource控件
只需要一个被绑定到一个的DropDownList
参数。这一切运作良好。
I have configured a GridView
to fill data by an ObjectDataSource
. This ObjectDataSource
requires only one parameter which is bound to a DropDownList
. This all works well.
当我加载网页,它填补了的DropDownList
和任何字段显示在此的DropDownList
原样传递参数传递给 ObjectDataSource控件
进一步填补了 GridView控件
。
When i load the page, it fills the DropDownList
and whatever field is displayed in this DropDownList
is passed as a parameter to the ObjectDataSource
which further fills the GridView
.
现在,我要增强的功能,并有一个文本框
和按钮
旁边的这个的DropDownList
。我想给我的用户的选择要么选择从的DropDownList
值,或键入它在文本框
键, $ p $干燥综合征Enter键更新 GridView控件
。
Now, i want to enhance the functionality and have a TextBox
and Button
next to this DropDownList
. I want to give my user the option to either select a value from the DropDownList
, OR TYPE IT IN THE TextBox
AND PRESS ENTER TO UPDATE THE GridView
.
任何想法,该怎么办呢?
Any idea how to do it?
我曾尝试 dataSource.Selecting
事件。但它不工作我希望它的方式。
请大家帮忙
I have tried dataSource.Selecting
event. but it isn't working the way i want it to be.
please help
推荐答案
这是一个粗略的样本,但基本上你可以做的是创造你可以创建一个会话参数或类似的控制参数,而不是:
This is a rough sample but basically what you can do is instead of creating a control parameter you can create a session parameter or something similar:
因此,当你点击进入它会使用文本框的值或者当你改变的DropDownList它将使用DropDownList的值。
So when you click enter it will use the textbox value or when you change the dropdownlist it will use the dropdownlist's value.
您也可以单选按钮的使用户能够从他那里要的值指定选项。
You can also have radio button's giving the user the option to specify from where he want's the value.
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="true"
onselectedindexchanged="ddl_SelectedIndexChanged"></asp:DropDownList>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:Button runat="server" Text="ClickMe" ID="btnOne" OnClick="btnOne_Click"/>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
<SelectParameters>
<asp:SessionParameter SessionField="ObjectParameterName" />
</SelectParameters>
</asp:ObjectDataSource>
code背后:
Code Behind:
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl = (DropDownList)sender;
Session["ObjectParameterName"] = ddl.SelectedValue;
ObjectDataSource1.Select();
}
protected void btnOne_Click(object sender, EventArgs e)
{
var ddl = (DropDownList)sender;
Session["ObjectParameterName"] = txt.Text;
ObjectDataSource1.Select();
}
修改事后
您也可以代替分配参数会话场,只需设置ObjectDataSource的参数直接(如无异常处理)。
You can also instead of assigning the parameter to a session field, just set the objectdatasource's parameter directly (Barring Exception handling).
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl = (DropDownList)sender;
ObjectDataSource1.SelectParameters.Add(new Parameter() {Name="Name",DefaultValue=ddl.SelectedValue });
}
这篇关于asp.net的ObjectDataSource传递从控制参数以及文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!