views:

424

answers:

1

Hi SO:

I am working on a FYI page that has large list of top-tiered links. Under these top-tier links, there are more links that usually go out to external websites. Currently, the way I handle populating the second-tier is as such: I query my database for all the top-tier links, then I construct an unordered list. Each list item has the format of:

<li class="multicolumnlistitem"><a href="linkpage.aspx?s={0}">Link Text</a></li>

Where {0} is the ID of the item. I would like to eliminate the need to pass the ID in the URL. It was suggested to me that I use client-side scripts to dynamically fill a hidden field with the value when one of the links are clicked, then read the form value on the linkpage.

Any ideas/examples/resources will be greatly appreciated.

Thanks!

Update

Here is the code that I have gotten down so far:

<form id="stateform" action="StateFyi/StateFyiView.aspx" method="post">
<p>
    <input type="hidden" id="idfield" name="s" runat="server" />
    <%
        GSDataLayer.Fyi.States StateManager = new GSDataLayer.Fyi.States();

        var stateList = StateManager.GetStateList();

        foreach (GSDataLayer.Fyi.States.State s in stateList)
        {
            stateListOutput.Text +=
            string.Format("<li class=\"multicolumnlistitem\"><a href=\"javascript:document.getElementById('{0}').value='{1}';document.stateform.submit();\">{2}</a></li>\n",
            new string[] {idfield.ClientID, s.ID.ToString(), s.Name});
        }        
    %>
    <ul class="multicolumnlist">
        <asp:Literal ID="stateListOutput" runat="server" Text="No Items!" />
    </ul>
</p>
</form>

The only remaining hurdle is the javascript (either in the above format, or the original document.getElementById('stateform')) returns null for the form. I cannot add "runat=server" to this form as there is already a form on the page. This preexisting form is required by the CMS I am using to function properly. Any ideas on where to go from here?

Thanks!

+2  A: 

Do you mean something like:

<form id="myform" action="linkpage.aspx" method="POST">
<input type="hidden" id="idfield" name="s" />
</form>
...
<li class="multicolumnlistitem"><a href="javascript:document.getElementById('idfield').value='0';document.getElementById('myform').submit();">Link Text</a></li>
quadelirus
Something like this, yes. However, it is not submitting just yet. I will have to see if I can see why not and then I will post back.
Anders
FireBug is saying: "document.getElementById("idfield") is null"
Anders
And according to the source view: <input id="ctl00_Content_ctl00_idfield" type="hidden" name="ctl00$Content$ctl00$idfield"/>. Do I have to accommodate for the extra text ASP puts in the id field?
Anders
yeah, I suppose so. I'm good with JS but I don't write ASP. If the ASP is giving you id="ctl00_Content_ctl00_idfield" then you'll have to use document.getElementById('ctl00_Content_ctl00_idfield')... you might also want to check if it changes the ID field of the form. Hopefully some other commenter will know why ASP is mucking around with your IDs.
quadelirus
I believe it has to do with master pages and how they organize their controls. What I will have to do is retrieve the list, then use 'idfield.ClientID()' to get the name of the control as ASP assigns it. Will report back with results.
Anders
Looks like you use server-side input field ("asp:input" instead of "input"). Since it's client-side scripting, it may be easier here to use plain-old html input field, not asp.net one.
DK
@DK: please review my edit to see what I am working with :)
Anders
Even 'idfield.Parent' is null :(
Anders