Showing posts with label handler. Show all posts
Showing posts with label handler. Show all posts

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!

Passing Variables Problem

Hi all

I hope someone can shed some light, im confused...

I have a GridView that displays diary data, the grids has a DataBound event handler shown below........

protected void GridView1_DataBound(object sender, EventArgs e) { TextBox1.Text =string.Empty; DateTime DRStart = DateTime.Now; DateTime DREnd = DateTime.Now;switch (DropDownList1.Text) {case"Tonight": DREnd = DateTime.Now.AddHours(23); txt_DRStart.Text = (string.Format("{0:ddd dd MMM}", DRStart)); txt_DREnd.Text = (string.Format("{0:ddd dd MMM}", DREnd));break;

There's a few more case statements but they just change the DRStart.Text and DREnd.text dates to populate the grid with differnet range of dates.

Back over on the Default.aspx page I have an SqlDataSource adaptor which look like this...........

"ResultsSqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT DISTINCT venue, town, artist, date FROM gigs WHERE (town = @dotnet.itags.org.town) OR (venue = @dotnet.itags.org.venue) OR (artist = @dotnet.itags.org.artist) AND (date BETWEEN @dotnet.itags.org.DRStart AND @dotnet.itags.org.DREnd)"> "TextBox1" Name="town" PropertyName="Text"> "TextBox1" Name="venue" PropertyName="Text"> "TextBox1" Name="artist" PropertyName="Text"> "txt_DRStart" Name="DRStart" PropertyName="Text"> "txt_DREnd" Name="DREnd" PropertyName="Text">

I get an 'error on page' from I.E. when I run it but no compiler error.

I've got a hunch that its because the @dotnet.itags.org.DRStart & @dotnet.itags.org.DREnd have been converted to strings in theGridView_DataBound event handler and the Sql SELECT statement cant read them. I just cant figure out how to get these values in the correct format!

Any suggestions

Kind regards

Tino

are you sure the tags(< >) you are writing here are correct?

Check the start and closings of each tag in Connection.


it also seems to me you have to transfer the data in correct format

do this

AND (date BETWEEN Date(@.DRStart) AND Date(@.DREnd))...................


Hi

Sorry the code I pasted was incorrect, i've tried the following but still no joy, same error on page in explorer.........

<asp:SqlDataSourceID="ResultsSqlDataSource1"runat="server"

ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT venue, town, artist, date FROM gigs WHERE (town = @.town) OR (venue = @.venue) OR (artist = @.artist) AND (date BETWEEN (@.DRStart) AND date(@.DREnd))">
<SelectParameters>
<asp:controlparameterControlID="TextBox1"Name="town"PropertyName="Text"/>
<asp:controlparameterControlID="TextBox1"Name="venue"PropertyName="Text"/>
<asp:controlparameterControlID="TextBox1"Name="artist"PropertyName="Text"/>
<asp:controlparameterControlID="txt_DRStart"Name="DRStart"PropertyName="Text"/>
<asp:controlparameterControlID="txt_DREnd"Name="DREnd"PropertyName="Text"/>
</SelectParameters>
</asp:SqlDataSource>

I get a compile error using this syntax --- BETWEEN (@.DRStart) AND date(@.DREnd))"

So I used 'Configure data source' and it makes it ---BETWEEN (@.DRStart) AND (@.DREnd))" which I assume is the correct SQL syntax for the

BETWEEN statement.

Still getting error though, any suggestions

Tino


Hi,

Based on my understanding, you want to use SqlDataSource to retrieve the data from database. When you run your asp.net application, you get an error message. If I have misunderstood you, please feel free to let me know.

What error message did you get? We can use SqlDataSource wizard to configure correct Sql statement.

For example, there is a table which has two datetime type columns: dateFrom, dateEnd. I want to retrieve the data between some datetime span:

 <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"> </asp:GridView> </div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnetforumConnectionString%>" SelectCommand="SELECT dateFrom, dateEnd FROM dateTable WHERE (dateFrom BETWEEN @.dateFrom AND @.dateEnd)"> <SelectParameters> <asp:ControlParameter ControlID="TextBox1" Name="dateFrom" PropertyName="Text" /> <asp:ControlParameter ControlID="TextBox2" Name="dateEnd" PropertyName="Text" /> </SelectParameters> </asp:SqlDataSource> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" />  </form>

I hope this helps.

PassingValues

I want to make sure that all the values of my controls on my form are available in my submit button's click handler. Currently, all my controls are yielding empty values. I tried making my controls a class level object, but that does not solve it. I am having trouble figuring out how to get these values using the eventargs object. Is there a good way of sending the controls current values as parameters to the handler method?To access properties and methods in User Controls, you need to add a line declaring the control in the CodeBehind.

C#
protected {Control Class Name} {Control Name on Form};

VB.NET
Protected {Control Name on Form} As {Control Class Name}