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

  • Thread starter Thread starter Fresh Air Rider
  • Start date Start date
F

Fresh Air Rider

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>
 
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
 
Thanks Yves

Yes, I thought about something like that.

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

Cheers anyway
John
 
Fresh Air Rider said:
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?
 
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
 
Fresh Air Rider said:
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.
 
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.
 
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
 
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
 
Fresh Air Rider said:
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.
 
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
 
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
 
Fresh Air Rider said:
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.
 
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
 
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
 
Fresh Air Rider said:
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.
 
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
 
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
 
Back
Top