SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Canada, Ontario
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript, filling post var and submit within onclick

    Searched for a half an hour on this trying to find a solution.

    I have a link and I just want to send through a variable depending on what linked was clicked back to the same page. I'm not good at javascript and I only need to use it in this one place.

    <a href="#" onclick="javascript:document.all['myvar']='myVal';document.forms[0].submit();">

    {text or image}

    </a>

    Is that correct? Im trying c# to read the var but with no luck.

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can skip the javascript: fromthe onclick. Also, don't use document.all, use document.getElementById instead.

    I don't know what myvar is, so I don't really know what you are trying to do.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Canada, Ontario
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im trying to send a variable to the page so when it reloads I can read that variable in c#.

  4. #4
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does the form have to submit? You can try setting the href to page.aspx?myvar=value, and you can get the value in C# using:
    Code:
    string myvar;
    if ( Request.QueryString["myvar"] != null )
       myvar = Request.QueryString["myvar"];
    What exactly are you trying to do? You might be better getting this thread moved to the .NET forum...

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Canada, Ontario
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its more javascript though, because Im asking for help specifically for writing a line of javascript.

    Ill try to explain again.

    Im trying to reload the page, and have some sort of variable set that can be accessed after/during the page refresh. The variable will be set to different values depending on what text link was clicked.

    Lets say I have two links:

    Link one
    Link two

    If the user clicks one, a value such as "Userclickone" to be in the http post vars contained in a variable called something like 'linkval' so I can access it on the next page refresh. If link two is clicked I want to see "Userclicktwo" in 'linkval' variable.

    My current code doesnt do that and Im trying to figure out why.

  6. #6
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it a different page? Are the values needed to be retrieved user specified? There are a myraid of ways you can do this very easily with C# and webcontrols without the need for JavaScript at all, and without any real effort either.

    Example, if you have two links, and want to determine what link is pressed, you simply need to LinkButton controls, with the CommandArgument set for each and use a single handler to determine which was clicked by checking the CommandArgument. Or you can have a handler for each LinkButton. Or use the CommandName method.

    If you can explain exactly what your wanting to accomplish, we can prolly help more.

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Canada, Ontario
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help but yes I just had trouble explaining it. I contacted a buddy and he was able to help me out.

    <input type="hidden" id="linkClicked" name="linkClicked" value=""></input>

    Then, I can tell which link the user clicked by doing this.

    <a href="#" onclick="javascript:document.getElementById('linkClicked').value='LinkOne';document.forms[0].submit();">

    Substituting value as the name of the link clicked.

    Now on the page refresh my C# can dig into the post params and dig out which one it clicked, allowing me to run specific code when the page is refreshed. (Im not working accross pages, just refreshing one and making it work differently).

    Thanks for your efforts and help.

  8. #8
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, but your making this overly difficult on yourself mate, you don't need to do what your doing. Try this:

    On the .aspx page in the HTML have this:
    Code:
    <asp:LinkButton ID="link1" runat="server" Text="Link1" OnClick="button_Click"></asp:LinkButton>
    <asp:LinkButton ID="link2" runat="server" Text="Link2" OnClick="button_Click"></asp:LinkButton>
    And your C# code:
    Code:
    protected void button_Click(object sender, EventArgs e)
    	{
    		switch (((LinkButton)sender).ID)
    		{
    			case "link1" : Link1Actions(); break;
    			case "link2": Link2Actions(); break;
    		}
    	}
    No need for JavaScript, and doing things the proper .NET.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •