Wednesday, March 21, 2012

passing variables into event handlers

I need to pass a variable into an event handler.

In other words, I would like to go <asp:something onclick=doSomething(5)
void dosomething (e, sender, int i)
{
i becomes 5... do something with it
}

now, i know overloading of event handlers isn't possible. Someone suggested using command argument such as:

<asp:something onclick=doSomething commandargument=5>
void dosomething (e, sender)
{
i = sender.commandArgument
}

Unfortuantely the tag i'm using (dataGrid) doesn't support CommandArgument. Any other suggestions how i can pass a variable into an event handler?

Thankshi,
tell me about the situation !!!!
in datagrid you don't need topass anything, you can always get values from a datagrid programatically !!
The thing is that I have a dataGrid inside a repeater. Data is bound to the repeater, and I need that same data to be used to bind the dataGrid.

So for example

dataTable bindData(station)
{
return dataTable from command 'select * from station'
}

changePage (e, sender)
{
do some paging stuff...
dataGrid.Source = bindData(HOW DO I GET A VALUE INTO HERE?)
}

<repeater source = stations>
<datagrid source = bindData(Container.Station) onPageIndexChanged = changePage />
</repeater
------
see... i pass the repeater 'station' into the source for the datagrid, but how do i pass something into the "onPageIndexChanged" ?
What parameter do you want to send to bindData()?

Without any more information (and only haidar will read all your code if you post it, just FYI), I would say that maybe you want to have a link in your repeater that redirects back to the same page, but with a parameter in the querystring, and then on Page_Load you can check for the existence of that parameter in the querystring and run your logic there.

ie ...


Page_Load(...)
{
if (Request.QueryString["selected_id"] != null)
{
bindData(Convert.ToInt32(Request.QueryString["selected_id"]));
}
}

:D whoa
not an elegant solution to say the least

oh well
at least i have a backup plan
thanks
> not an elegant solution to say the least

I prefer "working" to elegant, so I guess I'm about five up on you there ... ;)

But if you find something that you consider to be more "elegant", definitely post it here, since I wouldn't mind seeing a different way of doing this. I use querystrings a lot, since the programming model that I tend to use is that the page pretty much reflects the database so I try to do as little as possible with viewstate. My sites tend to have many concurrant users, so I can't afford stale data sets.

To me ... elegant = simple + powerful
LOL, i cheated

I added a

<asp:LinkButton id="stationIdButtonHolder" CommandName="stationIdHolder" CommandArgument=<%# DataBinder.Eval(Container.DataItem, "StationId") %> Runat="Server" /
Then i accessed it with:

String StationId = ((LinkButton)((Control)sender).NamingContainer.FindControl( "stationIdButtonHolder" )).CommandArgument.ToString();

from my paging function :D ya'ay!

0 comments:

Post a Comment