I have 2 gridviews, gv1 and gv2 and an ObjectDataSource with the id ods1. Both the gridviews are pointing to DataSourceID="ods1".
My question is, how do I know in selecting event of an ObjectDataSource that which gridview has called ods1. I want to set input parameters based on which gridview has made a call to the ods1.
I think this is not easily possible and it feels like it would be against the idea behind ODS.
You can delegate two ObjectDataSources to get the data from THE SAME repository class but still, you need two different data sources if you want to have two different sets of parameters. Thus, you do not duplicate code as the repository code is shared between object data source instances.
Warning: Hack ahead
I tend to agree with Wiktor Zychla's answer, but if you really need to do this...
The only thing I can think of to accomplish this would be to handle the "DataBinding" event of each of your GridViews, and set a session variable to indicate which one is about to call the ObjectDataSource "Selecting" event.
So you would have your GridView methods:
protected void gv1_DataBinding(object sender, EventArgs e)
{
Session["currentGridID"] = "gv1";
}
and
protected void gv2_DataBinding(object sender, EventArgs e)
{
Session["currentGridID"] = "gv2";
}
And then, your ObjectDataSource could check that Session variable, to see which ID is in it while the ObjectDataSource is firing this time:
protected void ods1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if(Session["currentGridID"] == "gv1")
{
}
else
{
}
}
To get the name of the gridview which call the objectdatasource
You can do something like:
string CallingGridName = ((ObjectDataSourceID)sender).ID;
Related
In my code I have a dropdown selection and upon selection from dropdown the code performs further processing and generates report/data.
Further, the entire program depends on data which is gathered from 3 different operation
Operation1: processing a text files of size of size > 6MB
Operation2: SQL Query to a DB (Query takes around 1 minute)
Operation 3: HTTP POST request to server (The main costliest part of the programe)
So, to make it efficient I am thinking to perform this operation only once and use the data for all the different selection from dropdown.
Question is how can I do so as below:
I can't put it in "page_load" event because every time page loads the operations will carry out
I can't put it inside "dropdownlist_selectedindexchanged" event because then it will be same as #1.
I thought of doing it in "page_load" as below
void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{
Operation1();
Operation2();
Operation3();
}
}
This is fine; the operations gets performed only once and I can use the data throughout, but then my page will take time to load as the operations takes time.
Is there any other way I can achieve what I want? Please let me know.
Thanks,
Rahul
If the data set will not change, you probably could manage to do it once at Application_Start().
Edit - something like this (typing from memory and away from VS, i do VB):
Protected void page_load(object sender, eventargs e)
{
// the name can be anything
if (!System.Web.HttpContext.Current.Session["data_cache_filled"])
{
// code to fill the cache.
// ...
//mark it as filled
System.Web.HttpContext.Current.Session["data_cache_filled"] = "yes";
}
}
Cache it. Using the CacheHelper class from here, you could do:
internal List<Employee> Operation1()
{
List<Employee> employeeData;
if (!CacheHelper.Get("employeeData", out employeeData))
{
employeeData = (from x in db.Employees select x).ToList(); // or whatever
CacheHelper.Add(employeeData, "employeeData");
}
return employeeData;
}
Suppose i have event: protected void dpMyNoteBook_PreRender(object sender, EventArgs e),
Inside this event i want to call another event name as : protected void ibtnPinMarkedRemovePin_Click(object sender, System.Web.UI.ImageClickEventArgs e).
Is it possible to do this?
The easiest solution is to move the code that currently is in ibtnPinMarkedRemovePin_Click into a helper method and call that from dpMyNoteBook_PreRender as well.
Event handlers are normal functions that happen to be executed when an event is fired.
Yes. (You can pass nulls as the two parameters, unless your code actually uses the parameters)
However, it would probably be better to move the code to a separate method and call that method from both handlers.
Events are just methods. You will need to create a new ImageClickEventArgs object and make sure it's populated properly (and you'll want to make sure sender is set to the image in question receiving the "click"), but it's just a method call.
ImageClickEventArgs e = new ImageClickEventArgs();
// set properties for e that may be relevant
// commandargument
// commandname
// whatever
ibtnPinMarkedRemovePin_Click(myImagesId, e);
how to map the Selection_change event to DayRender event of the Calender control
I'm trying to bind a List<String> to a DropDownList in a user control. I think I'm doing the right thing, but it seems that after my code executes the bindings are cleared. Here's the code for review!
User control:
<asp:DropDownList ID="subjectNameDropDown" runat="server"/>
<asp:DropDownList ID="yearLevelDropDown" runat="server"/>
Auto-generated designed code-behind:
public partial class NewSiteMetadataUserControl {
protected global::System.Web.UI.WebControls.DropDownList subjectNameDropDown;
protected global::System.Web.UI.WebControls.DropDownList yearLevelDropDown;
}
Code-behind:
public partial class NewSiteMetadataUserControl : UserControl
{
protected override void CreateChildControls()
{
subjectNameDropDown = new DropDownList();
yearLevelDropDown = new DropDownList();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
EnsureChildControls();
// Attempt 1
List<String> subjectNames = GetSubjectValues();
foreach (var subjectName in subjectNames)
subjectNameDropDown.Items.Add(subjectName);
subjectNameDropDown.DataBind();
// Attempt 2
List<String> yearLevels = GetYearLevelValues();
yearLevelDropDown.DataSource = yearLevels;
yearLevelDropDown.DataBind();
}
}
Should this approach work?
If it should, how can I debug what happens after the code executes?
Yes, this approach should work, here's why it currently isn't,
A DropDownList done with DataBind needs a DataSource. This is why Attempt #1 is not working.
If you're binding to a List<string>, there is no clear key/value pair to bind to. This is why when binding to a List<Person> (for example), you need to override .ToString() in the Person class to provide the key/value binding, or manually set the DataTextField, DataValueField.
There is no way for ASP.NET to work out a key/value pair for a string.
Think about what HTML you want. What should be the key/value for a simple string? Doesn't make sense does it.
Since you don't really care about the "key" (only what is displayed), i suggest you bind to a Dictionary<TKey,TValue> instead.
Either make your method return that, or iterate through the list and add them to the dictionary with an index.
The problem here was CreateChildControls. Somewhere in my attempts to make this work I added this method that initialises the controls. This isn't necessary and in fact caused the data bindings to be wiped out, as it was automatically called by the framework after OnLoad.
The solution was to remove this method and the call to EnsureChildControls.
I want to pass 3 parameters for SelectMethod and 1 parameters for SelectCountMethod of ObjectDataSource.
How can I pass these? And how ObjectDataSource can distinguish which parameters for which methods?
There are two ways of passing parameters to an ObjectDatasource.
1) Through it's wizard you can bind the parameters to various controls, form fields, querystring, session, etc.
2) In it's Selecting event. Example:
protected void Page_Load(object sender, EventArgs e)
{
myObjDs.Selecting += new ObjectDataSourceSelectingEventHandler(myObjDs_Selecting);
}
void myObjDs_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["someparamname"] = "test";
}
Instead of using selecting event you can also directly add parameters in your button click or anyother function . It must differentiate on the basis of parameter name . I haven't tested it but it shall work.
ObjectDataSource2.SelectParameters.Clear()
ObjectDataSource2.SelectParameters.Add("Parameter1",ValueOfParameter1);
I have an ASP.NET 3.5 GridView on a WebForm.
The GridView gets data from an ObjectDataSource which is set via the DataSourceID property in the code in front.
The ObjectDataSource returns a List of custom data class objects (just a class with public properties) to populate the GridView.
What I want to do is use the a List comsumed by the GridView in another code-behind method. At a high level:
1. GridView is loaded with List data from ObjectDataSource.
2. In the GridView.OnDataBound method I call GridView.DataSource to get the List object.
3. I enumerate the List and use the same data to do some other operation.
The theory being one less duplicated method call and one less call to the back-end database.
I've tried calling DataSource from the GridView' DataBound method and calling GridView.Rows[x].DataItem. In each case I only get a Null reference exception ("Object reference not set to an instance of an object").
Is there any way to achieve what I'm after?
If I understand you correctly, you want the OnRowDataBound event. This way, you can use data from the row that was just databound:
protected void gvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
CustomDataClass data = e.Row.DataItem as CustomDataClass;
if (data != null)
{
// access data here...
}
}
But do you want the onRowDataBound event? It looks like you want the onDataBound event for the GridView's entire datasource...
So you don't necessarily want one instance (row) of CustomDataClass, you want the entire CustomDataClass[] array of rows to use somewhere else.
HELP! I need this too.
******UPDATE******
I found the answer. Do this as below and set the OnSelected event in your objectdatasource:
protected void ObjectDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
ObjectListRow[] objectArray = (ObjectListRow[])e.ReturnValue;
List objectList = objectArray.ToList();
}
It turns out my datasource was an array, but if yours is a List<> then just cast the e.ReturnValue as the List.
EASY CHEESY.