asp.net objectdatasource pass parameter from control as well as textbox
Solution 1
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:
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 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();
}
EDIT AFTERTHOUGHT
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 });
}
Solution 2
There is the SelectParameters.Add
method which takes the ColumnName and value as a parameter to pass in the parameter value.
protected void btn_Click(object sender, EventArgs e)
{
ods.SelectParameters.Clear();
ods.SelectParameters.Add("ColumnName", SetparameterValue);
}
Shezi
you know you are a geek and a natural at programming when you write programs in your head to accomplish everyday tasks...
Updated on June 04, 2022Comments
-
Shezi almost 2 years
I have configured a
GridView
to fill data by anObjectDataSource
. ThisObjectDataSource
requires only one parameter which is bound to aDropDownList
. This all works well.When i load the page, it fills the
DropDownList
and whatever field is displayed in thisDropDownList
is passed as a parameter to theObjectDataSource
which further fills theGridView
.Now, i want to enhance the functionality and have a
TextBox
andButton
next to thisDropDownList
. I want to give my user the option to either select a value from theDropDownList
, OR TYPE IT IN THETextBox
AND PRESS ENTER TO UPDATE THEGridView
.Any idea how to do it?
I have tried
dataSource.Selecting
event. but it isn't working the way i want it to be. please help -
Shezi almost 13 yearsThank you Akhtar bhai, what i actually did was used your code, and added dropdownlist_selectedindexchanged event ... and whenever the event is triggered, i take the parameter value from dropdownlist.selectedvalue... and if the button is clicked, i used your code... Thanks a lot
-
Shezi almost 13 yearsThank you TBohnen, what i actually did was used your Logic and got the idea, I added dropdownlist_selectedindexchanged event ... and whenever the event is triggered, i take the parameter value from dropdownlist.selectedvalue... and if the button is clicked, i use the code given by Muhammad Akhtar.. What exactly i was missing was that i wasn't clearing the value of selectParameter before adding a new parameter.... However, i solved my problem....Thanks a lot