473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I pass more than one arguement to a function using "<asp:LinkButton> ?

Hello

Could anyone please explain how I can pass more than one
arguement/parameter value to a function using <asp:linkbutton> or is
this a major shortfall of the language ?

Consider the following code fragments in which I want to list through
all files on a directory with a link to download each file by passing
a filename and whether or not to force the download dialog box to
appear.

Codebehind

public void DownloadFile(string fname,bool forceDownload)
{
.....
.....
}

<script language="C#" runat="server">

void CommandBtn_Click(Object sender, CommandEventArgs e)
{
DownloadFile(e.CommandArgument,???);
}
</script>
<asp:linkbutton id="LinkButton2"
style="Z-INDEX: 101; LEFT: 136px; POSITION: absolute; TOP: 200px"
OnCommand="CommandBtn_Click" runat="server"
CommandArgument="test.doc" Width="240px" Height="48px"
CommandName="FileName">LinkButton</asp:linkbutton>
Jul 21 '05 #1
17 7310
I never used asp before so there might be an option to provide multiple
arguments.

However, if there isn't, just concat your arguments with a specific
seperator and split it on the other side.

Yves

"Fresh Air Rider" <Fr*************@Hotmail.com> schreef in bericht
news:55**************************@posting.google.c om...
Hello

Could anyone please explain how I can pass more than one
arguement/parameter value to a function using <asp:linkbutton> or is
this a major shortfall of the language ?

Consider the following code fragments in which I want to list through
all files on a directory with a link to download each file by passing
a filename and whether or not to force the download dialog box to
appear.

Codebehind

public void DownloadFile(string fname,bool forceDownload)
{
.....
.....
}

<script language="C#" runat="server">

void CommandBtn_Click(Object sender, CommandEventArgs e)
{
DownloadFile(e.CommandArgument,???);
}
</script>
<asp:linkbutton id="LinkButton2"
style="Z-INDEX: 101; LEFT: 136px; POSITION: absolute; TOP: 200px"
OnCommand="CommandBtn_Click" runat="server"
CommandArgument="test.doc" Width="240px" Height="48px"
CommandName="FileName">LinkButton</asp:linkbutton>

Jul 21 '05 #2
Thanks Yves

Yes, I thought about something like that.

I guess that it must be a shortfall of Dot Net.

Cheers anyway
John
Jul 21 '05 #3
Fresh Air Rider <Fr*************@Hotmail.com> wrote:
Yes, I thought about something like that.

I guess that it must be a shortfall of Dot Net.


No it's not. If you want variable length parameter lists, you can
specify those (the "params" modifier in C#) but what would be the point
of allowing methods to take more parameters than they'll understand?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
Hi John

Many thanks for your reply.

I wasn't referring to variable length parameter lists.

I wanted to know how to call a function with, for the sake of
arguement, 2 parameters, from a LinkButton ?

Thanks
John

Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Fresh Air Rider <Fr*************@Hotmail.com> wrote:
Yes, I thought about something like that.

I guess that it must be a shortfall of Dot Net.


No it's not. If you want variable length parameter lists, you can
specify those (the "params" modifier in C#) but what would be the point
of allowing methods to take more parameters than they'll understand?

Jul 21 '05 #5
Fresh Air Rider <Fr*************@Hotmail.com> wrote:
I wasn't referring to variable length parameter lists.

I wanted to know how to call a function with, for the sake of
arguement, 2 parameters, from a LinkButton ?


If a method has 2 parameters, you can call it with 2 parameters. If it
*doesn't* have 2 parameters, you can't call it with 2 parameters. It
doesn't matter where you're trying to call it from.

Looking back at your code, I can't see what the problem is - you just
need to decide what value to use as the second parameter. No-one else
can decide for you what that second parameter's value should be.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
Hi,

I always do this

<asp:linkbutton CommandArgument='<%# Container.ItemIndex &
";" &
Container.DataItem("package_header_id")%>'></asp:linkbutton>
and then use

Dim X as String = e.CommandArgument
x.Split(";")

Doing this you can effectively pass as many arguments as you like,
providing you choose your delimiting character carefully.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 21 '05 #7
Hi John

I think that the question marks were perhaps a little misleading.

Suppose I want to call the function DownloadFile("test.doc",true) from
the <asp:linkbutton> with it's two parameters (arguments), the name of
the file to download(test.doc) and whether or not to force a download
dialog box to appear(true or false).

My question was that as the <asp:linkbutton> only has one
CommandArgument property there is surely a limitation in cases where
someone wants to call a function with more than one parameter unless
you do some nasty hack like passing several parameters through the
single CommandArgument property as comma separated values.

public void DownloadFile(string fname,bool forceDownload)
{
.....
.....
}

<script language="C#" runat="server">

void CommandBtn_Click(Object sender, CommandEventArgs e)
{
//How can the Boolean value be passed to the following function
call
//from the <asp:linkbutton> ?
DownloadFile(e.CommandArgument,true);
}
</script>

<asp:linkbutton id="LinkButton2"
style="Z-INDEX: 101; LEFT: 136px; POSITION: absolute; TOP:
200px"
OnCommand="CommandBtn_Click" runat="server"
CommandArgument="test.doc" Width="240px" Height="48px"
CommandName="FileName">LinkButton</asp:linkbutton>

I hope that this clarifies my question.

Many thanks for your help so far.
John
Jul 21 '05 #8
Cor
Hi Fresh Air,

Yes your question was a little misleading, but let me tell it on another
way.

The button itself does nothing than if it is a pushed on a webpage giving a
signal that it is been pushed to the server. (and sends the information on
that page back)

In the serverpart of your program you have an event that sees that the
button is pushed

And that can pass as much parameters to a function as you want.

I hope this clears it a little bit?

Cor
Jul 21 '05 #9
Fresh Air Rider <Fr*************@Hotmail.com> wrote:
I think that the question marks were perhaps a little misleading.

Suppose I want to call the function DownloadFile("test.doc",true) from
the <asp:linkbutton> with it's two parameters (arguments), the name of
the file to download(test.doc) and whether or not to force a download
dialog box to appear(true or false).

My question was that as the <asp:linkbutton> only has one
CommandArgument property there is surely a limitation in cases where
someone wants to call a function with more than one parameter unless
you do some nasty hack like passing several parameters through the
single CommandArgument property as comma separated values.


No, there's no limitation. The method called by the button can then
call other methods just as easily as if you could call it directly with
extra parameters. Indeed, the code you provided showed it doing just
that.

If you want to use the state of the button or whatever, you can access
the state of the controls appropriately, can't you?

I fail to see what actual outcome you're failing to be able to achieve
due to this supposed flaw.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10
Hi Chaps

First of all many thanks indeed for your help and advice so far, I
really do appreciate it.

I must admit that I still can't appreciate how one would call a
function that accepts two arguments from and <asp:LinkButton> that
only has one CommandArgument.

I fully appreciate that the CommandBtn_Click(Object sender,
CommandEventArgs e) event can then in turn call a function with as
many parameters as the function being called supports.

Going back to my scenario where I might be looping through a list of
files on a server and I might want some to be displayed within the
browser and some to be downloaded.

For each pass of my loop, the value of the boolean parameter of my
DownloadFile(string fname,bool forceDownload) function may be
different.

ie

....

DownloadFile("test.doc",false);
DownloadFile("test.xsl",true);
DownloadFile("test.mdb",false);

....

But if I'm calling my function from an <"asp:LinkButton">, I'm still
restricted to only one CommandArgument.

I can only pass the name of my file and not the Boolean value to flag
whether or not it should force a download box.

<asp:linkbutton id="LinkButton2"
style="Z-INDEX: 101; LEFT: 136px; POSITION: absolute; TOP:
200px"
OnCommand="CommandBtn_Click" runat="server"
CommandArgument="test.doc" Width="240px" Height="48px"
CommandName="FileName">LinkButton</asp:linkbutton>

Apologies if I'm being particularly dense but I just think that it
seems to be a big restriction of the language.

Thanks
John
Jul 21 '05 #11
Hi

Many thanks for your help so far.

I don't think I'm explaining the scenario properly.

How would the CommandBtn_Click event know the value of the Boolean
parameter being passed from the <asp:LinkButton> which can only able
tp pass the name of the file via its single CommandArgument ?

Within my CommandBtn_Click, I have hardcoded the value true as the
second argument when calling my DownloadFile(e.CommandArgument,true).

In other words I want to be able to call my DownloadFile(string
fname,bool forceDownload) function from the <asp:linkbutton>
and pass different values for each argument.

ie

DownloadFile("test.doc",true)
DownloadFile("test.xls",false)
DownloadFile("test.mdb",true)

If I call DownloadFile() from my <asp:LinkButton>, I can only pass the
value of one argument (ie the filename or the Boolean value) because
<asp:LinkButton> is limited to only one CommandArgument.

Can you know see the limitation ?

Thanks
John
Jul 21 '05 #12
Fresh Air Rider <Fr*************@Hotmail.com> wrote:
Many thanks for your help so far.

I don't think I'm explaining the scenario properly.

How would the CommandBtn_Click event know the value of the Boolean
parameter being passed from the <asp:LinkButton> which can only able
tp pass the name of the file via its single CommandArgument ?

Within my CommandBtn_Click, I have hardcoded the value true as the
second argument when calling my DownloadFile(e.CommandArgument,true).

In other words I want to be able to call my DownloadFile(string
fname,bool forceDownload) function from the <asp:linkbutton>
and pass different values for each argument.

ie

DownloadFile("test.doc",true)
DownloadFile("test.xls",false)
DownloadFile("test.mdb",true)

If I call DownloadFile() from my <asp:LinkButton>, I can only pass the
value of one argument (ie the filename or the Boolean value) because
<asp:LinkButton> is limited to only one CommandArgument.

Can you know see the limitation ?


No, because instead of hard-coding it, you find out the source of your
event and make a choice based on that. Slightly less instinctive, I'll
grant you, but I can't see this being an issue *very* often, and it
means the general case ends up being simpler to express.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #13
Hi John
No, because instead of hard-coding it, you find out the source of your
event and make a choice based on that. Slightly less instinctive, I'll
grant you, but I can't see this being an issue *very* often, and it
means the general case ends up being simpler to express.


Surely we know that the source of the event is the <asp:LinkButton>
but that desn't help us to extract any more arguments if it only
supports on CommandArgument in the first place does it ?

As far as I can see, the only way to do it is with some nasty hack
like passing the values of each parameter as a comma separated string
via the single CommandArgument parameter.

I'm not too worried about this particular scenario as I was just using
it to learn C# and .Net but I can quite easily see that I might need
to do it one day when I'm working on a project.

Cheers
John
Jul 21 '05 #14
Hi Chaps
No, because instead of hard-coding it, you find out the source of your
event and make a choice based on that. Slightly less instinctive, I'll
grant you, but I can't see this being an issue *very* often, and it
means the general case ends up being simpler to express.


Microsoft have an example of calling a function from a button (see
link below) but their function very conveniently only has one
parameter/argument.

http://tinyurl.com/3gxbq

I have searched extensively on the internet and cannot find an example
of the syntax for calling a function with more than one
parameter/argument from an <asp:LinkButton>

Thanks
John
Jul 21 '05 #15
Fresh Air Rider <Fr*************@Hotmail.com> wrote:
No, because instead of hard-coding it, you find out the source of your
event and make a choice based on that. Slightly less instinctive, I'll
grant you, but I can't see this being an issue *very* often, and it
means the general case ends up being simpler to express.
Surely we know that the source of the event is the <asp:LinkButton>
but that desn't help us to extract any more arguments if it only
supports on CommandArgument in the first place does it ?


If you know which button it was, you can set up which "extra"
parameters it would have had elsewhere. (You couldn't have had two
different sets of parameters from the same button anyway.) The lack of
elegance here is that the parameters would essentially be set up away
from the other bits of declaration of the button.

(In fact, in the simple case you gave, the most readable way would
probably to have two methods, one of which called DownloadFile with
true, and one of which called DownloadFile with false. That obviously
wouldn't scale to more general cases.)
As far as I can see, the only way to do it is with some nasty hack
like passing the values of each parameter as a comma separated string
via the single CommandArgument parameter.

I'm not too worried about this particular scenario as I was just using
it to learn C# and .Net but I can quite easily see that I might need
to do it one day when I'm working on a project.


I suspect that in real life it doesn't come up very often, and MS found
that a simple solution which covered 95% of cases and made the 5% a bit
harder (and less elegant) was better than a solution which made all
100% possible in an elegant way, but which made the 95% more
complicated to do.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #16
Cor
Hi Fresh Air Rider,

The linkbutton is one of those controls I never use, so I do it with a
textbox
You have a page with two textboxes, a label and one button, I can not figure
it out more simple

The user ask for the page and you load the page on the server and the user
gets it.

Then he types 2 and 3 in the textboxes and pushes the button.

In (a kind of psuedo) vb.net code

This is can than be a part of the button event
private sub buttonevent bla bla bla handles button1.click
me.label1.text = calculatefunction(textbox1, textbox2)
end sub

private function calculatefunction(byval as textboxA, byval as textboxB) as
string
return (Cint(textboxA.text) * Cint(textboxB.text)).tostring
end function

I think that is not that difficult?

Cor



"Fresh Air Rider" <Fr*************@Hotmail.com> schreef in bericht
news:55**************************@posting.google.c om...
Hi

Many thanks for your help so far.

I don't think I'm explaining the scenario properly.

How would the CommandBtn_Click event know the value of the Boolean
parameter being passed from the <asp:LinkButton> which can only able
tp pass the name of the file via its single CommandArgument ?

Within my CommandBtn_Click, I have hardcoded the value true as the
second argument when calling my DownloadFile(e.CommandArgument,true).

In other words I want to be able to call my DownloadFile(string
fname,bool forceDownload) function from the <asp:linkbutton>
and pass different values for each argument.

ie

DownloadFile("test.doc",true)
DownloadFile("test.xls",false)
DownloadFile("test.mdb",true)

If I call DownloadFile() from my <asp:LinkButton>, I can only pass the
value of one argument (ie the filename or the Boolean value) because
<asp:LinkButton> is limited to only one CommandArgument.

Can you know see the limitation ?

Thanks
John

Jul 21 '05 #17
Hi John and Cor

Thanks very much indeed for your contributions which have been most
constructive and helpful to me.

I think that was a very interesting discussion.

Best wishes to you both
John
Jul 21 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Fresh Air Rider | last post by:
Hello Could anyone please explain how I can pass more than one arguement/parameter value to a function using <asp:linkbutton> or is this a major shortfall of the language ? Consider the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.