-2

I feel like this is a very simple solution that I am overlooking somehow. Basically I have a web page (using asp.net webforms) with a datalist. Inside my datalist, and inside the datalist items I have a div that I want the user to be able to click to cause a page redirect. I would like to do this without use of an anchor or javascript if possible. My datalist:

<asp:DataList ID="DataList1" ..etc >
<HeaderTemplate>
..etc
</HeaderTemplate>
<ItemTemplate>
<table >
<tr >
<td>..etc</td>
<td>..etc</td>
<td ><div id="clr_div" runat = "server"></div></td> <-- this is the div to be clicked
</tr>
</table>
</ItemTemplate>

And then in my code behind

protected void on_item_databound(object sender, DataListItemEventArgs e)
{
     System.Web.UI.HtmlControls.HtmlGenericControl div = (System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("clr_div");
     div.Attributes.Add("onclick", "**What goes here??**");
}

I have tried to put in the 'what goes here' section:

window.location.href = \"default.aspx\" **doesn`t work, the quotes don`t render properly in html
window.location.href = default.aspx **doesn`t work
return Response.Redirect(default.aspx); **doesn`t work

Any help is welcome. Thanks

twinPrimesAreEz
  • 125
  • 2
  • 13
  • Did you even google it first? http://stackoverflow.com/questions/10304809/anchor-tag-inside-a-div-when-div-click-it-page-should-redirect-to-one-page-and http://stackoverflow.com/questions/2188272/html-how-to-make-an-entire-div-a-hyperlink http://stackoverflow.com/questions/796087/make-a-div-into-a-link http://stackoverflow.com/questions/16949731/how-to-redirect-from-div-and-bypass-child-anchor-href – Display Name is missing Feb 11 '14 at 23:26
  • Yes of course I did. I edited my question to reflect that. I already know I can do it using javascript or anchors and was curious if there is a solution that doesn`t involve javascript or anchors. Downvoting the question is unnecessary since I clearly asked what to put in the onclick arg when adding attributes to the div and not 'can I do this using a different technique?' – twinPrimesAreEz Feb 11 '14 at 23:30
  • Could whoever is downvoting please explain why you think this is a bad question? – twinPrimesAreEz Feb 11 '14 at 23:51
  • Here you go: http://stackoverflow.com/questions/9294626/execute-serverside-code-on-div-click – gbs Feb 12 '14 at 00:43
  • Your question has been asked tons of times and has over a million google results - that's why it was downvoted. When people ask the same question it "pollutes" everything and it gets harder to find the actual answer – Display Name is missing Feb 12 '14 at 05:30
  • Great! Please share those 'millions' of results that answer my specific question (no javascript, no anchor, from codebehind) BTW it turns out that using javascript doesn`t work on my page atm anyway due to a bubbling issue apparently caused by 'fixed' vs non-fixed divs (javascript is not firing from a div not using fixed)....To whoever is going and downvoting my other questions including those that were upvoted and resolved over a year ago because this question bothers them -- get a life. To whoever is legitimately looking for an answer and trying to help, thanks -- I`m still looking. – twinPrimesAreEz Feb 12 '14 at 16:15
  • Did you try the solution in the link I posted? – gbs Feb 12 '14 at 18:02
  • This may help as well: http://stackoverflow.com/questions/6114643/div-adding-redirect-and-background-image-from-code-behind-in-asp-net – Display Name is missing Feb 12 '14 at 19:29
  • @gbs. Nope. I got pulled off onto another project today and also I started a new account since I didn`t have very high rep on this one to begin with and now all my older questions are getting downvoted by some troll (you know who you are). Dear troll, feel free to downvote away all my other questions to your heart`s content now lol. I won`t be using this account again. @better_use_mkstemp thanks for actually trying to help. I will try it later when I get a chance. I realized I was having an issue when some of my divs were fixed and other weren`t. – twinPrimesAreEz Feb 12 '14 at 21:41
  • Interesting - if you do find an answer you can post it here (via new account) and we'll upvote it so you can get the rep to comment etc. – Display Name is missing Feb 13 '14 at 00:30

1 Answers1

0

The answers listed should help you out once you fix your div issues. I have experienced event firing issues in non-fixed divs when using static positioning, so you might want to try specifying position:relative on your containing div in question if you have fixed divs elsewhere.

Difference between static and relative positioning

Also, TMTOWTDI. If you have an empty div, you could just also turn it into a button for an insignificant markup cost

as explained here

Just add an ItemCommand event to your datalist

<asp:DataList ID="DataList1" onitemcommand="on_item_Command" ..etc..

and change this

<div id="clr_div" runat = "server"></div>

to this

<asp:Button id="clr_div" CommandName="nav-to-page" runat="server" CommandArgument='<%#  Eval("field to eval if this came from a datasource") %>'  />

and in code behind

protected void on_item_Command(object source, DataListCommandEventArgs e)
{
   if (e.CommandName == "nav-to-page")
   {
       string s = (string)e.CommandArgument;
       Response.Redirect("NewPage.aspx?" + s);
   }
}
Community
  • 1
  • 1