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

K

Kevin Blount

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 <form> tag, 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=server> line
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
 
B

bruce barker \(sqlwork.com\)

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)
 
K

Kevin Blount

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
 
E

Erik Funkenbusch

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.
 
K

Kevin Blount

bruce said:
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)
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top