Asp.net objectdatasource passes parameter from control as well as text field

I installed a GridView to populate data using an ObjectDataSource . For this ObjectDataSource only one parameter is required, bound to a DropDownList . It all works well.

When I load the page, it fills the DropDownList , and any field displayed in this DropDownList is passed as a parameter to the ObjectDataSource , which further fills the GridView .

Now I want to improve the functionality of both TextBox and Button next to this DropDownList . I want to give my user the ability to either select a value from DropDownList , or TYPE IT IN THE TextBox AND PRESS ENTER to UPDATE GridView .

Any idea how to do this?

I tried the dataSource.Selecting event. but it doesnโ€™t work the way I want. please, help

+4
source share
2 answers

This is an example, but basically what you can do, instead of creating a control parameter, you can create a session parameter or something like that:

So, when you press the enter button, it will use the value of the text field or when changing the drop-down list, it will use the value of the drop-down list.

You may also have a radio button that allows the user to indicate where he wants this 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 for:

  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(); } 

CHANGE AFTER REPAIR

You can also, instead of assigning a parameter to the session field, simply set the objectdatasource parameter directly (deletion ban processing).

  protected void ddl_SelectedIndexChanged(object sender, EventArgs e) { var ddl = (DropDownList)sender; ObjectDataSource1.SelectParameters.Add(new Parameter() {Name="Name",DefaultValue=ddl.SelectedValue }); } 
+2
source

There is a SelectParameters.Add method that takes a ColumnName value and a value as a parameter to pass the parameter value.

 protected void btn_Click(object sender, EventArgs e) { ods.SelectParameters.Clear(); ods.SelectParameters.Add("ColumnName", SetparameterValue); } 
+1
source

All Articles