How to pass parameter programmatically in objectdatasource - asp.net

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

Related

Calling click event (CommandEventArgs) of a button serverside

I refer to this article.
I also use ASP.NET and I got the following button click event:
protected void Button1_Click(object sender, CommandEventArgs e)
{
//Do some stuff
}
When I try to call the click event serverside I get an error. It says that it can't convert System.EventArgs to System.Web.UI.WebControls.CommandEventArgs.
Is it possible to call the event the same way as in the article I did refer to?
Thanks!
You can absolutely call it still, it is just a normal C# method. But the caveat is that you need to provide correct values as command name and command argument, as this handler most likely uses one or both of these.
string commandName = "command name here";
object commandArgument = <argument here>;
Button1_Click(sender, new CommandEventArgs(commandName, commandArgument));
That is assuming you call it inside Page_Load, as the post you refer to. If not, make sure to provide reference to correct control as sender.

How to solve this error : No overload for 'node_changed' matches delegate 'System.EventHandler'

How to solve this error : No overload for 'node_changed' matches delegate 'System.EventHandler'
hi, i have done treeview in asp.net.
+football.
here + is having one property called "OnTreeNodePopulate" and Football contains property called "OnSelectedNodeChanged".
i designed in aspx page like follows:
so, when i click + it invokes in aspx.cs page like follows:
protected void Pluspopulate_clicked(object sender, TreeNodeEventArgs e )
{
session["aaa"]=e.node // here i can get current node
//here i am passing two argument to populateSublevel method ie id and current node.
PopulateSubLevel(Convert.ToInt32(e.Node.Value),' " + session["aaa"] + " ');
}
Till now fine. but when click on football, it invokes following aspx.cs page:
protected void Node_changed(object sender, Eventargs e )
{
//here also i want to pass two argument to populateSublevel method ie id and current node. like what i get in pluspopulate_cliked event.
// But here i am not able to get current node. here i need to get current node.
session["aaa"]=e.node // i need like this. but here i am not able get current node
}
so when i give TreeNoeEventArgs e in Node_changed, it throws an error like " No overload for 'node_changed' matches delegate 'System.EventHandler'"
how to solve this . how to can i get current node in node_chaged event. Please help me!.
TreeView.SelectedNodeChanged Event method has two arugments, object sender and Eventargs e. You can learn at TreeView.SelectedNodeChanged Event.
Here's how you can use it:
protected void Node_changed(object sender, Eventargs e )
{
//Assuming that your treeview id is TreeView1
TreeNode myNode = TreeView1.SelectedNode; // This is the current node you need.
session["aaa"]= myNode;
}

How to get Calling Gridview name in ObjectDataSource Selecting event

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;

How to hide querystring value in url

Requesting a page from GridView's RowCommand event, here is the code
protected void grdClaimList_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "ViewClaim":
Response.Redirect("ClaimStatus.aspx?id=" + e.CommandArgument);
break;
}
}
I would like hide the query string from url, is it possible? if yes, please let me know how?
You can also use session variables or view states instead.
I don't think it is possible but you could encrypt in and then decrypt it on the other side if you don't want the User seeing it and putting different values in.

Is it possible to call one event to another event in asp.net?

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

Resources