tags:

views:

671

answers:

3

This is so damn stupid but driving me absolutely fing crazy.

<input type="radio" name="OptGroup" id="<%#"rbEmail" + ((Action)Container.DataItem).ID %>" value="<%#((Action)Container.DataItem).ID %>" runat="server" /><label for="<%#"rbEmail" + ((Action)Container.DataItem).ID %>"><%#((Action)Container.DataItem).Action %></label>

ok, wtf am I doing wrong here! I've also tried:

<input type="radio" name="OptGroup" id='<%#"rbEmail" + ((Action)Container.DataItem).ID %>' value='<%#((Action)Container.DataItem).ID %>' runat="server" /><label for='<%#"rbEmail" + ((Action)Container.DataItem).ID %>'><%#((Action)Container.DataItem).Action %></label>

and

<input type="radio" name="OptGroup" id="<%#'rbEmail' + ((Action)Container.DataItem).ID %>" value="<%#((Action)Container.DataItem).ID %>" runat="server" /><label for="<%#'rbEmail' + ((Action)Container.DataItem).ID %>"><%#((Action)Container.DataItem).Action %></label>

I specifically do not want to use an asp.net radiobutton due to the problems with GroupName it creates while inside a repeater. I want to use a bare radio button and bind its values to my datasource.

A: 

The first one is wrong because, as you've suspected, the double quotes for the attribute value and the double quotes around the string literal confuse the parser.

I can't for the life of me see what is wrong with the second one though. Do you get the same "not well formed" error with that one too?

Al W
A: 

Use single quotes for your html. For example:

<input type='radio' name='OptGroup' id='<%#"rbEmail" + ((Action)Container.DataItem).ID %>' value='<%#((Action)Container.DataItem).ID %>' runat='server' /><label for='<%#"rbEmail" + ((Action)Container.DataItem).ID %>'><%#((Action)Container.DataItem).Action %></label>
Chris Lively
only thing I can think of is the last <%#((Action)Container.DataItem).Action %>I noticed that intellisense (or the compiler I guess) picks up "Action" type by coloring it blue but the rest of the (Action) that are in quotes are white since they are strings
same <input type='radio' name='OptGroup' id='<%#"rbEmail" + ((Action)Container.DataItem).ID %>' value='<%#((Action)Container.DataItem).ID %>' runat='server' /><label for='<%#"rbEmail" + ((Action)Container.DataItem).ID %>' runat='server'><%#((Action)Container.DataItem).Action %></label>
about to pull my hair at this point
Take out the " quotes.I'm not sure what they are there for.
Chris Lively
+3  A: 

Do you need to access the control server-side? If not, take off the runat="server", you cannot databind to the ID property for a server control. Not sure if thats the problem, since that should give you a different error

EDIT:

Something like this should suit your purposes..

<asp:Repeater runat="server">
    <ItemTemplate>
        <label><input type="radio" name="rbEmail" value='<%# ((Action)Container.DataItem).ID %>' /><%# ((Action)Container.DataItem).Action %></label>
    </ItemTemplate>
</asp:Repeater>

Then in the postback, you can get the value from Request.Form["rbEmail"]

EDIT2:

Fully tested simple page example..

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <label><input type="radio" name="rbEmail" value='<%# Container.DataItem %>' /><%# Container.DataItem %></label>
            </ItemTemplate>
        </asp:Repeater>
        <asp:Button ID="submit" runat="server" OnClick="submit_Click" Text="submit" />
    </form>
</body>
</html>

Default.aspx.cs

using System;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Repeater1.DataSource = new string[] { "Hello", "World" };
        Repeater1.DataBind();
    }

    protected void submit_Click(object sender, EventArgs e)
    {
        Response.Write(Request.Form["rbEmail"]);
    }
}
Adam
I do. this will be inside a repeater. Then server-side I'll access the selectedvalue
@unknown, check edit and see if that helps
Adam
I seriously don't get the <# vs <= I know one is inline vs. whatever but I do not get this
and why are you putting the radiobutton inside the label vs. putting it along side?
@unknown, well you still need to databind the repeater which i've left out here. the <%# is the databinding expression
Adam
@unknown, since we didn't set an id, we have nothing to set for= for the label. if you still want to select the radiobutton by clicking on the text, you have to put the radiobutton inside the label
Adam
yea, i did databind it. It's just the mark-up that's not ok
>>>since we didn't set an id, we have nothing to set for= for the label I had an id in there
<%# vs. <%= I also don't get. What's <%= is what confused me. I see that it means inline, so what the hell does that mean?
anyway, I'm still stuck. Nothing has made this piece of code "happy" today, even your suggestion
@unknown, ok last shot. added working example. see if you cant make it with with your data source. good luck, good night
Adam
for your Container.DataItem how did yo tell it what field though?
but this doesn't state what field '<%# Container.DataItem %>'
@unknown, since its only a string array bound to the dataitem, the object itself is a string. In your case, the object appears to be an Action? So you can cast as an Action and set the field like you do in your sample
Adam
even if I used yours, I don't see how it's wiring up the Id and Action without specifying the name of those fields.Second, no matter how I get the mark-up to work, accessing the selected value, I assume would be done something like (RadioButton)Request.Form["rbEmail"].SelectedValue
hmm seemed to work in terms of getting rid of the errors. Now just wondering a couple more thingsa) why or can you not use <lable for..> instead of wrapping label around input which is just weird to meb) how to grab selected item from code behind. I tried to case Request.Form["rbEmail"]
and you're accessing it with the Request.Form using name not id?
err, sorry but asp.net has made me HTML stupid so bare with me. This is so frustrating
I'm too used to stupid asp.net controls now
I wanted to do something like if(Request.Form["rbEmail"].checked)
@unknown you should be able to add the label for with no trouble. Request.Form["rbEmail"] will give you the value set as a string of the selected radio button. Also note that you can do a simple <%# Eval("ID") %> to bind properties of your class.
eglasius
@unknown, a) you can use <label for=> if you want, but its unnecessary b) radiobuttons post their values as their name (which is their group - which i think was the whole point of this exercise) the selected value will be in Request.Form["rbEmail"] where rbEmail is the name of your radiobutton grp
Adam
@unknown in your scenario, the Request.Form["rbEmail"] will correspond to your action.ID property. No need to do checked or any other thing, the browser will send the value of the selected radio button. I guess you would say it comes with the email id or email action id (whichever makes sense).
eglasius
>>>Also note that you can do a simple <%# Eval("ID") %> to bind properties of your classThanks for the info on the fact that it's actually giving me the selected value already by doing that. I thought I'd have to check for chekced. In terms of Eval, trying to stay away from that syntax, slower
>>>you can use <label for=> if you want, but its unnecessaryThanks. I tried using that but ended up with this mess of errors
>>b) radiobuttons post their values as their namethat's right id is only for wiring up lets say a for attribute in a label. I'm getting too confused with IDs in HTML vs. control IDs in ASP.NET! I think
omg, I need to get the hell out and into MVC again and away from MS controls completely. So sick and tired of this
And yea, since I'm not using an asp.net radiobutton or radiobuttonlist, I loose type. So I can't do a SelectedIndex anymore obviously
it's all strings and then convert from here. But what if I wanted the index of the input?
Good god, I would have not gotten this on my own. Thanks a lot for the help!
@unknown, yikes! if you only need the index, then i'd bind the value as <#% Container.ItemIndex %>. if you need both index and value, then it gets more intersting
Adam
ASP.NET MVC, where are you so I can get back to you, get me out of asp.net controls! I am *upid because of them and frustrated due to their bugs!
ah right...thanks. I don't just need the index but that makes sense. Ok, way past my bedtime. It's 2am and I need to get up again at 5am for work. This sucks. Nice fun with radiobuttonlist today and fun with ditching that and back to plain html
if only I did not have to create a custom Radiobuttonlist and find out that every time I selected a value from it, I got an emptystring back so had to ditch that
damn it one more problem. I needed 2 repeaters. But then if I put the names different, the radiobuttons in both sets are not mutually exclusive
I want to be able to check one set to see if selected vs. the other set because they're 2 completely different lists that the customer can choose from..not something I want to combine