By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,600 Members | 1,683 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,600 IT Pros & Developers. It's quick & easy.

How can I dynamically set a <form runat="server"...> action parameter?

P: n/a
bit long winded this one, so stick with me:

I'm trying to create a form that can go to one of 3 places, depending on
various elements. My form control looks like this:

<form runat="server" ID="myForm" method=POST>

so, first thing: how can I dynamically set the "Action" parameter? I
know that with <asp:HiddenField...for example, I have to do something
like this:

<% fieldID.Value = "stringContent" %>
<asp:HiddenField ID="fieldID"></asp:HiddenField>

so I'm guessing I can do the same kind of thing for the form control,
but "myForm.Action" doesn't appear to work.

I also tried

<% string myString = "here.aspx" %>
<form runat="server" ID="myForm" method=POST action="<%= myString %>">

but that didn't work either.

Here's the second part:
To get the value of the forms Action, I need to connect to a database,
so I setup a DataTable and grab the data. I figure I need to do this
outside of the <formtag, so that I can put the value into it's Action,
but I also want to be able to use that DataTable inside the form, but
I've found that anything I setup before the <form runat=serverline
isn't accessible within the form (such as the other fields from the
database for populating the form textboxes.

Can I set up a string, int, DataTable, etc outside the form that can
also be used inside it? If so, please please tell me how :)

Cheers

Kevin
Aug 23 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
if you are using 2.0, you can set the postback url in a button. in version
one, you need to use cient script.

-- brue (sqlwork.com)
"Kevin Blount" <ke**********@LOLgmail.comwrote in message
news:u1**************@TK2MSFTNGP04.phx.gbl...
bit long winded this one, so stick with me:

I'm trying to create a form that can go to one of 3 places, depending on
various elements. My form control looks like this:

<form runat="server" ID="myForm" method=POST>

so, first thing: how can I dynamically set the "Action" parameter? I know
that with <asp:HiddenField...for example, I have to do something like
this:

<% fieldID.Value = "stringContent" %>
<asp:HiddenField ID="fieldID"></asp:HiddenField>

so I'm guessing I can do the same kind of thing for the form control, but
"myForm.Action" doesn't appear to work.

I also tried

<% string myString = "here.aspx" %>
<form runat="server" ID="myForm" method=POST action="<%= myString %>">

but that didn't work either.

Here's the second part:
To get the value of the forms Action, I need to connect to a database, so
I setup a DataTable and grab the data. I figure I need to do this outside
of the <formtag, so that I can put the value into it's Action, but I
also want to be able to use that DataTable inside the form, but I've found
that anything I setup before the <form runat=serverline isn't accessible
within the form (such as the other fields from the database for populating
the form textboxes.

Can I set up a string, int, DataTable, etc outside the form that can also
be used inside it? If so, please please tell me how :)

Cheers

Kevin

Aug 24 '06 #2

P: n/a
Bruce: Thanks for the reply. I'm not familiar this the postback url, but
I've done some quick research and found the following example:

<asp:button id="Button2"
text="Post value to another page"
postbackurl="Button.PostBackUrlPage2cs.aspx"
runat="Server">
</asp:button>

Quick question about this code:

Should I be adding a different button for each URL, or can I set this
dynamically in the same way as my asp:HiddenField example, e.g.

<%
if (blah)
{
Button2.Text = "go to myURL";
Button2.postbackurl = myURL;
}
else
{
Button2.Text = "go to myOtherURL";
Button2.postbackurl = myOtherURL
}
<asp:button id="Button2" runat="Server">
</asp:button>

and if so, should I use ".PostBackUrl", "PostBackURL", "postbackurl" or
doesn't it matter (which I doubt)?

Cheers

Kevin
bruce barker (sqlwork.com) wrote:
if you are using 2.0, you can set the postback url in a button. in version
one, you need to use cient script.

-- brue (sqlwork.com)
Aug 24 '06 #3

P: n/a
On Wed, 23 Aug 2006 15:42:36 -0500, Kevin Blount wrote:
bit long winded this one, so stick with me:

I'm trying to create a form that can go to one of 3 places, depending on
various elements. My form control looks like this:
Short answer is, you can't do it.

ASP.NET pages post back to themselves, otherwise there's no way for them to
handle control events.

The longer answer is that ASP.NET 2.0 introduced a way to do postback's to
another page with the button control, but that may not be what you're
looking for.
Aug 24 '06 #4

P: n/a
bruce barker (sqlwork.com) wrote:
if you are using 2.0, you can set the postback url in a button. in version
one, you need to use cient script.

-- brue (sqlwork.com)
"Kevin Blount" <ke**********@LOLgmail.comwrote in message
news:u1**************@TK2MSFTNGP04.phx.gbl...
>bit long winded this one, so stick with me:

I'm trying to create a form that can go to one of 3 places, depending on
various elements. My form control looks like this:

<form runat="server" ID="myForm" method=POST>

so, first thing: how can I dynamically set the "Action" parameter? I know
that with <asp:HiddenField...for example, I have to do something like
this:

<% fieldID.Value = "stringContent" %>
<asp:HiddenField ID="fieldID"></asp:HiddenField>

so I'm guessing I can do the same kind of thing for the form control, but
"myForm.Action" doesn't appear to work.

I also tried

<% string myString = "here.aspx" %>
<form runat="server" ID="myForm" method=POST action="<%= myString %>">

but that didn't work either.

Here's the second part:
To get the value of the forms Action, I need to connect to a database, so
I setup a DataTable and grab the data. I figure I need to do this outside
of the <formtag, so that I can put the value into it's Action, but I
also want to be able to use that DataTable inside the form, but I've found
that anything I setup before the <form runat=serverline isn't accessible
within the form (such as the other fields from the database for populating
the form textboxes.

Can I set up a string, int, DataTable, etc outside the form that can also
be used inside it? If so, please please tell me how :)

Cheers

Kevin

Bruce,

Thanks again for pointing me in the right direction. Now that I'm back
in the office I've been playing around with PostBackUrl, and I found
that I can dynamically set this, using the following:

<%
EventSubmit.Text = "this is the button text";
EventSubmit.PostBackUrl = "/us/seminars/index.aspx";
%>
<asp:Button ID="EventSubmit" runat="server"></asp:Button>

This will let me set the text based on the translated text from the
database, and also set the resulting URL based on the contents of the
form/page.

Many thanks for the help; I sincerely doubt I would have found this
option if let to my own devices (I'm having to use Dreamweaver for my
aspx pages, which has very limited .NET intelisense)

Kevin
Aug 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.