[RESOLVED] How to get the RadioButtonList selectedvalue in Javascript

I want to get the value of the selected item of a radiobutton List using javascript.
My code is:

<asp:RadioButtonList runat="server" ID="Radio" RepeatColumns="3" CssClass="textfont">
<asp:ListItem Value="1" Selected="True">First</asp:ListItem>
<asp:ListItem Value="2">Second</asp:ListItem>
<asp:ListItem Value="3">Third</asp:ListItem>
</asp:RadioButtonList>



And this is my Javascript code:

<script type="text/javascript">
function sendParameters() { 
var Id = '<%=HiddenField1.Value%>';
var ddl1 = document.getElementById("Radio").checked; 
</script>



How to proceed?

Try this

<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
foreach (ListItem item in this.RadioButtonList1.Items)
{
item.Attributes.Add("onClick", "javascript:ItemClicked(this);");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function ItemClicked(item) {
alert(item.value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem selected="true">Apples</asp:ListItem>
<asp:ListItem>Pears</asp:ListItem>
<asp:ListItem>Bananas</asp:ListItem>
</asp:RadioButtonList>
</div>
</form>
</body>
</html>

This will get the value of the selected button in RadioButton1 and put it in the TextBox txtRB:

<script type="text/javascript">
    function getSelected() {
      var targlist = "";
      var rbList = document.getElementById('<%= RadioButtonList1.ClientID %>');
      var rbCount = rbList.getElementsByTagName("input");
      //    Put values of Radiobutton into txtRB
      for (var i = 0; i < rbCount.length; i++) {
        if (rbCount[i].checked == true) {
         // var labelArray = rbCount[i].parentNode.getElementsByTagName('label');
         // targlist = targlist + labelArray[0].innerHTML + ", ";     // Get Label of Radiobutton
          targlist = rbCount[i].value;
        }
      }
      document.getElementById('<%=  txtRB.ClientID %>').value = targlist;
    }
  </script>

Hi,

Check this,

document.getElementById will only get the element with the ID you specify (the HTML spec is quite clear that only one element on a page can have a specific ID).

Each radio button has a different ID attribute, but if you look at the HTML source of a page, you will see that all radio buttons in the list have the same NAME attribute.

Ex:
document.getElementById('<%=radiobutton.ClientID%>');

[RESOLVED] Row colour as per date calculation

In my asp.net+vb we+access DB. i have a field named DOB which is date of birth. i displays the data in grid view whose DOB+90 is less than ot equl to current date

DOB+90<=now()

and it works currect. but when i try to used vb code to change the row colour as per data it works for this one.

 

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If (e.Row.DataItem("DOR") <= Now()) Then
                e.Row.ForeColor = Drawing.Color.Red
            End If

But i want three diffrent conditions

1.  whose DOB=current month

2.  whose DOB comes in next month

3. whose DOB comes in month after next month

The font colour should be diffrent

how can i do that

I think this is what you are looking for, but I'm not sure. Your "Red" condition checks the DOB is less than today. That is different than the month is less than this month. 

For instance, #3 - DOB comes in month after next. You can't really know when someone will be born, so I assume you mean is the birth month the same as what month it will be in two months. So if DOB = 5/15/2000, or 5/10/1978 or 5/1/2010 would all be true for condition 3. (as of this post date of March). 

If my interpretation is correct, give this a try:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As   System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            
                e.Row.ForeColor = getColor(e.Row.DataItem("DOB"))

        End If


    Protected Function getColor(ByVal DOB As Date)

        If DOB.Month < Now().Month Then
            Return System.Drawing.Color.Red
        End If

        'DOB is this month
        If DOB.Month = Now().Month Then
            Return System.Drawing.Color.Blue
        End If

        'DOB is next month
        If DOB.Month = Now().AddMonths(1).Month Then
            Return System.Drawing.Color.Green
        End If

        'DOB is in two months
        If DOB.Month = Now().AddMonths(2).Month Then
            Return System.Drawing.Color.Green
        End If

        'Not any so return null or some generic color
        Return System.Drawing.Color.Gray

    End Function


it gives compilation error

starts with protectyed sub and ends with end function there is something wrong

You need End Sub before the function. I just copied your code as is. I don't do VB so it could have some syntax errors but I think it is a good example to work from.

It is not working

Try this..

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then

If e.Row.DataItem("DOB").Month < Now().Month Then
e.Row.ForeColor= Color.Red
'DOB is this month
ELSE If e.Row.DataItem("DOB").Month = Now().Month Then
e.Row.ForeColor= Color.Blue
'DOB is next month
ELSE If e.Row.DataItem("DOB").Month = Now().AddMonths(1).Month Then
e.Row.ForeColor= Color.Green
'DOB is in two months
ELSE If e.Row.DataItem("DOB").Month = Now().AddMonths(2).Month Then
e.Row.ForeColor= Color.Green
Else
'Not any so return null or some generic color
e.Row.ForeColor= Color.Gray
End If
End If

End Sub

[RESOLVED] Formatting the apperance of a string

Hi

I have a string of text entered by a user in a multiline textbox, I want to output this text on a pdf in the same format, ie the same number of lines. 

I am using pdfsharp to produce the pdf but I need to format the string first before I place it on the pdf.

It's not possible.  There is no way of knowing what width the textbox is (remember that modern browsers can let the user resize the box unless you add mark-up to say otherwise).  You don't know what font they are using, what zoom level they are using, what the resolution of the screen is, how their browser is anti-aliasing the text.  You have no way of knowing how many "lines" the user has entered.  You could take a guess, but it would only be a guess.

I was afraid of that.  thanks for replying

[RESOLVED] How to change safari User Agent to act like IEFFChrome in .Net C#

Hi All,

I used silverlight File uploader control in my Application, and Its working fine with all browser accept Safari. I found that when we change the setting of safari ... Develop --> User Agent--> IE 9.0. then Its working fine to me. 

So could you please let me know if you ever try to change this settings in safari from our C# code. 

Its so urgent 

Thanks in Advance.

Browsers in general are detected based on the User Agent string that is passed in through an HttpHeader value. 

You can change this value within your Request to attempt to "spoof" the browser into thinking that it is using IE (the source for this is quite old and may likely be deprecated): 

System.Net.WebRequest objRequest = System.Net.HttpWebRequest.Create(strURL);

//Pretend to be IE9
((System.Net.HttpWebRequest)objRequest).UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)";

objResponse = objRequest.GetResponse();

(Related Source)

This is typically never really a very good idea and it can cause all kinds of erratic behavior.

[RESOLVED] All checkboxes returned checked, even if “ ”

I have a list of records which I use a repeater to display on screen. One of the columns contains a checkbox 'Contacted'. I have set some of the records to be checked and some not.( In the DB)...but when the page loads all the records are checked...Does anyone know why?

<input type="checkbox" name="chkBox"
 checked='<%# SetCheckboxValue((bool)Eval("Contacted"))%>' />

"Contacted" is a bool set in the code behind depending if the record is checked or not(from the DB)

protected string SetCheckboxValue(bool Contacted)
    {
        if (Contacted)
        {
            return "checked";
        }

        return String.Empty;
    }

SetCheckboxValue gets either T or F and should return "checked" or " " Then the application should display each record as checked or empty..but as I said all of the records are appearing as checked?

Any idea guys? Thanks




The fact that it *has* a checked attribute will be making it checked, regardless of its value

<input type="checkbox" name="chkBox"
 <%= SetCheckboxValue((bool)Eval("Contacted"))%> />

protected string SetCheckboxValue(bool Contacted) 
    { 
        if (Contacted) 
        { 
            return "checked=\"checked\""; 
        } 
 
        return String.Empty; 
    }

I'm not great at binding controls from the aspx page, but that's the rough idea.

Have you placed any kind of breakpoints within your code to check if it is firing properly and that your values are what they should be? Are the values that are being stored within your Contacted value actually proper values that can be parsed as booleans (such as True and False)?

You could also use a ternary operator to set this value as well : 

<input type="checkbox" name="chkBox" checked='<%# (bool.Parse(Eval("Contacted")) ? "checked" : "" %>' />



AidyF makes a good point.

The checked attribute will denote that the box is checked, regardless of it's value : 

<!-- This is checked -->
<input type='checkbox' checked='' />
<!-- This is checked -->
<input type='checkbox' checked='false' />
<!-- This is checked -->
<input type='checkbox' checked='checked' />
<!-- This is not checked -->
<input type='checkbox' />

His suggestion should be exactly what you need, which can also be thrown into a ternary statement : 

<input type="checkbox" name="chkBox" <%# (bool.Parse(Eval("Contacted")) ? "checked" : "" %> />




thanks for help

yes worked...that you for that