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

Discussion in 'ASP .Net' started by Kevin Blount, Aug 23, 2006.

  1. Kevin Blount

    Kevin Blount Guest

    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
     
    Kevin Blount, Aug 23, 2006
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
     
    bruce barker \(sqlwork.com\), Aug 24, 2006
    #2
    1. Advertising

  3. Kevin Blount

    Kevin Blount Guest

    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)
     
    Kevin Blount, Aug 24, 2006
    #3
  4. 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.
     
    Erik Funkenbusch, Aug 24, 2006
    #4
  5. Kevin Blount

    Kevin Blount Guest

    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" <> wrote in message
    > news:...
    >> 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

    >
    >

    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
     
    Kevin Blount, Aug 24, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?d3J5dGF0?=

    form in a form that runat server?

    =?Utf-8?B?d3J5dGF0?=, Jun 7, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    430
    Steve C. Orr [MVP, MCSD]
    Jun 7, 2005
  2. Joe Bloggs
    Replies:
    1
    Views:
    794
    Sudsy
    Aug 3, 2003
  3. John
    Replies:
    0
    Views:
    881
  4. André
    Replies:
    2
    Views:
    734
    André
    Jun 23, 2008
  5. Replies:
    0
    Views:
    636
Loading...

Share This Page