Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programaticaly attaching OnSelected handler to a GridView's SQLDataSou

Status
Not open for further replies.

simonB2010

Programmer
Jul 19, 2011
5
0
0
GB
Well new to .Net, new to WDM and new to the forums .. so hello !
.. and apologies if I may use the odd incorrect terminology, I shall do my best.


So, I have made a userControl from the standard GridView: 'myPageGridView'.

Unfortunately, there is one event that I am having to handle in the external web page itself, and I want to package it up into the class itself, so that the whole thing is nice and self-containg.

The code:
protected void SqlDataSource1_OnSelected(object sender, SqlDataSourceStatusEventArgs e)
{
myPageGridView1.MockItemCount = int.Parse(e.Command.Parameters["@setSize"].Value.ToString());
}

... this works fine in the end web page, just getting an output parameter from the dataSource and assigning it to a variable.


Now I do not want to have to copy'n'paste that code every time I use my grid.

So I want my class to get a handle on the instance's SQLDataSource and attach a handler to it.
Then the handler can grab the value within the class.

I have tried this, within the 'myPageGridView' class:

this.DataSourceObject.DataSourceChanged += new SqlDataSourceStatusEventHandler(myPageGridView_SQLSourceOnSelected);

..... but that doesn't seem to be the right handler, it's asking me to crerate the handling function with get/set only.



There was success when I did something similiar with another event, one belonging directly the Grid itself:

this.PageIndexChanging += new GridViewPageEventHandler(this.myPageGridView_PageIndexChanging);

... but I fear reaching out to the Grid's SQLDataSource is not just a s simple as referencing a level down !?!?


Ok ... so you can tell, this is day "1" of my .net life and I just hope that someone understands what I am looking at ....

Any help appreciated.


Thanks

Simon




 
It's good that you want to reduce the amount of repetitive code in your app. The problem is that you are using the DataSource control. Those controls are helpful for very basic quering of a database. The best thing to do is create a DAL(Data Access Layer) of code that you write and maintain yourself. This way you have full control, and you create classes to contain the data.
 
jbenson001,

I have to thank you for your honest and blunt reply.
Sometimes in a new environment, you're not sure if something is just 'hidden' or 'impossible'.

Seems mad though, the Grid has a datasource, the datasource has an event handler... can't see where MS had difficulty in letting me at it !? (Grid.Source.event)

Before .NET I used to create my own structures in Javascript ... lookslike it's no different here then.

But thanks for your advise, I shall off and Google DALs !!

:)


 
I didn't mean to sound rude. I just like to tell people that the Datasource controls have shortcommings. I know from experience. I know that MS puts all of these examples out there that use the DataSource controls. Yes, they work, however, their examples are very simplistic. Once you need to do a complicated query, or something like you are, it becomes almost, if not impossible.
Like I said, the best thing is to create your own DAL, which you have done before it seems. You can also look into using MS Entity Frame work, MS built in ORM tool. However, this also has a big learning curve. You may want to tackle that at another time.
Just keep at it, you will get it, and please post here if have any questions.

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top