ASP.NET DataSource & GridView databinding, setting a parameter value programmatically - asp.net

So, I have a GridView with an ObjectDataSource, and I want to programmatically set one of the SelectParameters of the ObjectDataSource.
I tried (during both Page_Load and DropdownList__SelectedIndexChanged)
objectDataSource.SelectParameters["my_parameter"].DefaultValue = "my_value";
objectDataSource.DataBind();
but it didn't work. What would you suggest?

Trap the onselecting event on the datasource.
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["month"] = DateTime.Now.Month;
}

Nevermind, I solved it myself.
In Page_Load:
objectDataSource.Selecting += new ObjectDataSourceSelectingEventHandler(objectDataSource_Selecting);
Then write the handler method:
void objectDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
int four = 2 + 2;
e.InputParameters["my_parameter"] = four;
}
Then make sure to databind the GridView somewhere
protected void dropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
gridView.DataBind();
}

Related

Initial selection for a DropDownList overriding user selection

I'm trying to set an initial selection for a DropDownList by calling: drop.SelectedIndex = 5 in Page_Load.
This works, but then if I change the selection manually and want to save the form, I'm still getting the initial selection instead of the new selection when calling drop.SelectedValue. What's wrong?
You have forgotten you check if(!IsPostback). Otherwise you will select the 6th item again on postbacks before the SelectedIndexChanged event is triggered (or a button-click event):
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack) // do this only on the initial load and not on postbacks
dropDwonList1.SelectedIndex = 5;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//set up data here
}
}
if (Page.IsPostBack)
{
//do page reload logic in here
}
protected void foo(object sender, EventArgs e)
{
//get your selected value here
}
Try this code
You should be using if(!IsPostback) in the Page_Load function.
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
drop.SelectedIndex = 5;
//yourcode
}
}
Through this your problem will be solved

Dropdownlist in gridview not firing selectedindex changed event

I have problem with not firing selected index changed event of dropdownlist in gridview. I gone through the SO Thread . It is not worked wholly for me. I have implementation like below.
.ASPX
<asp:DropDownList ID="DDL1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDL1_SelectedIndexChanged">
<asp:ListItem Text="Review" Value="Review" Selected="True">Review</asp:ListItem>
<asp:ListItem Text="Level1" Value="lvl1">Send Back to Level1</asp:ListItem>
</asp:DropDownList>
.CS
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// Bind the GridView to something.
DataBindGrid();
}
else {
// Bind the GridView again to maintain previous entered data in the gridview
DataBindGrid();
}
}
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
this.lblCity.Text = ((DropDownList)sender).SelectedValue;
}
protected void grdPoll_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(Page.IsPostBack)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("DDL1") as DropDownList;
if(ddl != null)
{
ddl.SelectedIndexChanged += new EventHandler(DDL1_SelectedIndexChanged);
}
}
}
}
When i keep if(!Page.IsPostBack) block only then it works fine. But i want else block also. Whats going wrong with implentation. Can you please suggest the solutions
The problem is block after !Page.IsPostBack block, which is not event else part as you said. You are binding grid again on post back which results in loss of the event being fired. You do not have to bind it again to have the changes in the grid.
Remove this code.
{
// Bind the GridView again to maintain previous entered data in the gridview
DataBindGrid();
}
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// Bind the GridView to something.
DataBindGrid();
}
else {
// Bind the GridView again to maintain previous entered data in the gridview
//DataBindGrid(); //remove DataBindGrid(); from else
}
}
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
this.lblCity.Text = ((DropDownList)sender).SelectedValue;
DataBindGrid();
}
replace event name "Page_Load" with "Page_PreRender"

Drop down selected index changed on page load

I have a drop down box that populates textbox values based on its value. It fires when it is changed but on pageload it doesnt fire. How do I get it to fire on page load?
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox3.Text = DropDownList1.SelectedValue;
TextBox12.Text = DropDownList1.SelectedValue;
TextBox21.Text = DropDownList1.SelectedValue;
//etc
Tim Schmelter's comment is right on the money.
// Wire up to the page load event
protected void Page_Load(object sender, System.EventArgs e)
{
updateTextBoxes();
}
// Wire up to the select index-changed event
protected void DropDownList1_SelectIndexChanged( object sender, EventArgs e )
{
updateTextBoxes();
}
// your workhorse method
protected void updateTextBoxes()
{
TextBox3.Text = DropDownList1.SelectedValue;
TextBox12.Text = DropDownList1.SelectedValue;
TextBox21.Text = DropDownList1.SelectedValue;
// etc.
}
It won't be called automatically at page load, you have to call it "manually":
void Page_Load(object sender, System.EventArgs e) {
// ....
DropDownList1_SelectedIndexChanged(DropDownList1, e);
}
SelectedIndexChanged fires in response to a user-driven change. Move the assignment logic to another method and call it manually from Page_Load and also from your event handler.

OnPreRender vs Page_PreRender

I knew about Page PreRender event and also about overriding OnPreRender, but didn't know existence of Page_PreRender method. Recently while going through this forum i found and search the difference between both but couldn't get much?
Here is my observation:
I have a page with label Label1.
Scenario 1:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "load ";
}
protected override void OnPreRender(EventArgs e)
{
Label1.Text += "OnPreRender ";
base.OnPreRender(e);
}
private void Page_PreRender(object sender, System.EventArgs e)
{
Label1.Text += "Page_PreRender ";
}
value of Lable1 would be: load OnPreRender Page_PreRender
Scenario 2:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "load ";
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Label1.Text += "OnPreRender ";
}
private void Page_PreRender(object sender, System.EventArgs e)
{
Label1.Text += "Page_PreRender ";
}
Note the call order of base.OnPreRender(e);
value of Lable1 would be: load Page_PreRender OnPreRender
I can only say base.OnPreRender(e) is calling Page_PreRender(), but isn't it private?
Can some one please tell, what is difference between both and when to use which?
In general which one should be used?
The OnPreRender method of the page is responsible for raising the Page_PreRender event.
Usually you should just subscribe to the Page_PreRender event.
If you need to do some special processing before it's safe to raise the Page_PreRender event, then you can override the OnPreRender method and do the processing before calling base.OnPreRender(e).
The Page_ events are called when AutoEventWireup="true" is set for your page. This is how you can create them and have them called without being explicitly wired up.

How can I show datas on gridview when selecting a dropdownlist in ASP.NET?

I have a dropdownlist and a gridview. When Dropdownlist is selected , gridview should show the data.
i think that's gonna work for you
http://www.facebook.com/groups/314912835194902/doc/406584239361094/
protected void DropDownListChanged(object sender, EventArgs e)
{
gridView.DataSource = GetDataSource();
gridView.DataBind();
}
you need to write an event SelectedIndexChanged="ddlChanged" and write your logic under it.
protected void ddlChanged(object sender, EventArgs e)
{
gridView.DataSource = GetData();
gridView.DataBind();
}
Make sure that you have AutoPostBack="true" on DropDownList. Otherwise it will not fire the event.

Resources