Click here to Skip to main content
15,889,462 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionPerhaps Crazy Idea for a Summer Project: Intelligent Book Library and Online Ordering System Pin
QuadlessPyjack11-May-14 23:01
QuadlessPyjack11-May-14 23:01 
QuestionRe: Perhaps Crazy Idea for a Summer Project: Intelligent Book Library and Online Ordering System Pin
Richard MacCutchan11-May-14 23:17
mveRichard MacCutchan11-May-14 23:17 
AnswerAmazon has a SDK Pin
David Mujica12-May-14 10:08
David Mujica12-May-14 10:08 
QuestionGenerat PDF from aspx page. in asp.net with formating Pin
deepika wani11-May-14 20:39
deepika wani11-May-14 20:39 
QuestionGoogle Site - SMTP support? ASP.NET? Microsoft SQL server? Pin
Swab.Jat11-May-14 20:27
Swab.Jat11-May-14 20:27 
AnswerRe: Google Site - SMTP support? ASP.NET? Microsoft SQL server? Pin
Richard MacCutchan11-May-14 23:16
mveRichard MacCutchan11-May-14 23:16 
GeneralRe: Google Site - SMTP support? ASP.NET? Microsoft SQL server? Pin
Swab.Jat12-May-14 12:55
Swab.Jat12-May-14 12:55 
QuestionHow to display a radiobuttonList along with a textbox with dynamically generated values? Pin
samflex9-May-14 14:33
samflex9-May-14 14:33 
Hi Gurus,

We have an online ballot app that has a bunch of radioButtonList with values dynamically populated from the database.

Please see screenshots below to see example ballot

Sample ballot

All values ( Candidate A, Candidate B, Write In Candidate) are all stored on the database dynamically populate the radioButtonList.

Example 1 shows the current setup.

How do I modify my script to have setup similar to example2?

Here is the code I am working with so far:

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Online Ballot</title>
<meta http-equiv="X-UA-Compatible" content="IE=7, IE=8, IE=9, IE=10, IE=11" />

<meta name="DownloadOptions" content="noopen" />
<link rel="Stylesheet" href='Styles/TakeSurvey.css' type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type = "text/javascript">
        function validateCheckBoxList(source, args) {
            var chkListModules = $(source).parent().find("table[id$='_CheckBoxList1']");
            if (chkListModules.length == 0 || chkListModules.find('input:checkbox:checked').length > 0)
                args.IsValid = true;
            else
                args.IsValid = false;
        }
    </script> 
<script type="text/javascript">
    function show(ctr) {
        var tb = document.getElementById("TextBox1");
        if (ctr == "RadioButtonList1") {
            tb.style.display = "block";
        }
        else
            tb.style.display = "none";

    }
     </script>

<style type="text/css">
fieldset { border:1px solid red }

legend {
  padding: 0.2em 0.5em;
  border:1px solid red;
  color:green;
  }

</style>
</head>
<body background="images/bg.gif">
<form id="form1" runat="server">
 <table bgcolor="#003366" width="100%">
  <tr><td><img src="images/ballotslogo.png" alt="" /></td></tr></table>
<div id='header-wrapper'>
<div class='header section' id='header'><div class='widget Header' id='Header1'>
</div></div>
</div>
<div id='outer-wrapper'>
<div id='wrap2'>

<div id='content-wrapper'>
<div id='crosscol-wrapper' style='text-align:center'>
<div class='crosscol section' id='crosscol'><div class='widget PageList' id='PageList1'>
<div style="position:absolute; left:170px; top:206px; z-index:800;">

<div align="center">
  <asp:Panel ID="Panel1" runat="server" GroupingText="" BorderColor="#999999"  Width="100%" Font-Size="14pt">
    <fieldset style="margin-bottom: 20px;">
     <legend style="font-weight: bold;">DECISION 2014 - CLOSING DATE: MARCH 21, 2014 @ 5:00PM</legend>
        <table style="width: 100%">
               <tr>
                 <td>
                    <asp:DataList ID="DataList1" runat="server" DataKeyNames="ElectionId" 
                        DataSourceID="SqlDataSource1" Width="100%" CellPadding="3" 
                         BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="0px" 
                         GridLines="Vertical">
                        <ItemStyle BackColor="#EEEEEE" ForeColor="Black" />
                        <ItemTemplate>
                            <table style="width: 100%;">
                              <tr>
                                    <td style="text-align: center;">
                                        <asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Size="X-Large"
                                            Text='<%# Eval("ElectionName_Position") %>'></asp:Label><br />
                                         <asp:Label ID="lbl_MessageType" runat="server"  ForeColor="red" Text='<%# GetMessageType(Eval("ElectionName_Position"))  %>'/>
                                    </td>
                                  </tr>
                                  <tr>
                                    <td style="text-align: left;">
                                        <div id="buttonDiv" runat="server">
                                          <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="true" align="center" style="width:20px;height:20px;zoom:120%; white-space:nowrap;">
                                          </asp:RadioButtonList>
                                          <asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server" 
                                            ControlToValidate="RadioButtonList1" ForeColor="Red" Display="Dynamic" ErrorMessage="Required"></asp:RequiredFieldValidator>
                                       </div>
                                        <asp:CheckBoxList ID="CheckBoxList1" runat="server" align="center" style="width:20px;height:20px;zoom:120%; white-space:nowrap;">
                                        </asp:CheckBoxList>
                                        <hr />
                                        <asp:CustomValidator ID="CustomValidator1" runat="server" 
                                            ClientValidationFunction="validateCheckBoxList" Display="static" 
                                            ErrorMessage="Please select at least one box." ForeColor="Red"></asp:CustomValidator>
                                        <asp:TextBox ID="TextBox1" runat="server" Columns="30" Font-Bold="False" 
                                            Rows="5" TextMode="MultiLine" Visible="false"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" Runat="server" 
                                            ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="Required"></asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                            </table>
                             <asp:HiddenField ID="HiddenField1" runat="server" 
                                Value='<%# Eval("AnswerType") %>' />
                            <asp:HiddenField ID="HiddenField2" runat="server" 
                                Value='<%# Eval("PositionId") %>' />
                        </ItemTemplate>
                        <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                        <HeaderTemplate>
                            <asp:Label ID="Label4" runat="server" Font-Bold="True" 
                                Text=""></asp:Label>
                            <br />
                            Instructions: Select the candidate(s) of your choice by clicking the Checkbox <img src="images/check.png" alt="" />  and the the Oval   <br /><img src="images/oval.png" alt="" /> adjacent to the   candidate's name. 
                            <br /><img src="images/line.gif" alt="" />
                            <br />
                        </HeaderTemplate>
                        <AlternatingItemStyle BackColor="#DCDCDC" />
                        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                        <FooterTemplate>
                        <tr vAlign="center">
                        <td align="center">
                            <asp:ImageButton ID="Button1" runat="server" alt="Submit" 
                                ImageUrl="images/btn-submit-vote.png" OnClick="Button1_Click" 
                                onmouseout="this.src='images/btn-submit-vote.png'" 
                                onmouseover="this.src='images/btn-submit-vote.png'" style="text-align: center;" 
                                title="Submit" />
                                </td></tr>
                        </FooterTemplate>
                        <SelectedItemStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                    </asp:DataList>
                   <asp:Label ID="Label5" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="Red"></asp:Label></td>
               </tr>
            </table>
          </fieldset>
        </asp:Panel>
        <asp:label id="lblMsg" font-size="Medium" runat="server"></asp:label>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BallotsConnectionString %>"
            SelectCommand="SELECT [ElectionName_Position] = CASE WHEN GROUPING(mh.PositionId) = 0 THEN MAX(mh.Position)  ELSE mh.ElectionName END
        ,CandidateName = CASE WHEN GROUPING(mh.PositionId) = 0 THEN MAX(mh.CandidateName)    ELSE '' END
        ,PositionId    = CASE WHEN GROUPING(mh.PositionId) = 0 THEN mh.PositionId   ELSE 0 END
        ,AnswerType = CASE WHEN GROUPING(mh.PositionId) = 0 THEN MAX(mh.AnswerType) ELSE '' END
  FROM  PreAgg mh
  GROUP BY ElectionName,PositionId WITH ROLLUP
  HAVING GROUPING(mh.ElectionName) = 0
  ORDER BY mh.ElectionName, GROUPING(mh.PositionId) DESC, mh.PositionID;">
           <SelectParameters>
           <asp:SessionParameter SessionField="ElectionId" Type="Int32" Name="ElectionId" DefaultValue="0" />
          </SelectParameters>
        </asp:SqlDataSource>
       </div>
       </div>
       </div>
       </div>
       </div>
       </div>
       </div>
       </div>
     </form>
   </body>
</html>


VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'If your Page was submitted'
        If IsPostBack Then

            Dim rb1 As CheckBoxList = DataList1.FindControl("RadioButtonList1")
            rb1.Items.Add("writein")

            Dim tb As TextBox = New System.Web.UI.WebControls.TextBox()
            Dim btnDiv = DataList1.FindControl("buttonDiv")
            tb.ID = "writein"
            tb.Text = " "
            tb.Visible = False
            btnDiv.Controls.Add(tb)

        End If

    Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rbl As RadioButtonList = Page.Form.FindControl("RadioButtonList1")
        If rbl.SelectedItem.Text = "Write In Candidate" Then
            Page.Form.FindControl("writein").Visible = True
        Else
            Page.Form.FindControl("writein").Visible = False
        End If
    End Sub
    
      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Dim questld As HiddenField = DataList1.FindControl("HiddenField2")

        For Each item As DataListItem In DataList1.Items
            If item.ItemType = ListItemType.Item Or item.ItemType = ListItemType.AlternatingItem Then
                Dim positionid As Integer
                Dim choiceid As Integer = 0
                Dim choicetext As String = ""
                'positionid = CType(item.FindControl("Label3"), Label).Text
                positionid = CType(item.FindControl("HiddenField2"), HiddenField).Value
                Dim anstype As HiddenField = item.FindControl("HiddenField1")
                Select Case anstype.Value
                    Case "S"
                        Dim rbl As RadioButtonList = item.FindControl("RadioButtonList1")
                        Dim txt1 As TextBox = item.FindControl("writein")
                        If rbl.SelectedValue = 33 Then
                            txt1.Visible = True
                        Else
                            txt1.Visible = False
                        End If
                        choiceid = rbl.SelectedValue
                        SaveVotes(positionid, choiceid, "", "NA")
                    Case "M"
                        Dim cbl As CheckBoxList = item.FindControl("CheckBoxList1")
                        For i As Integer = 0 To cbl.Items.Count - 1
                            If cbl.Items(i).Selected Then
                                choiceid = cbl.Items(i).Value
                                SaveVotes(positionid, choiceid, "", "NA")
                            End If
                        Next
                    Case "T"
                        Dim txt As TextBox = item.FindControl("TextBox1")
                        choicetext = txt.Text
                        SaveVotes(positionid, 0, choicetext, "")
                End Select
            End If
        Next
        DataList1.Visible = False

    End Sub


When I run this code, I get following error:

Object reference not set to an instance of an object.

Line 36: rb1.Items.Add("writein")


Thanks a lot in advance for your help
AnswerRe: How to display a radiobuttonList along with a textbox with dynamically generated values? Pin
Richard MacCutchan9-May-14 21:46
mveRichard MacCutchan9-May-14 21:46 
Questionset values to TextBox in Item template of gridview Pin
ganesh169-May-14 6:08
ganesh169-May-14 6:08 
Questionhow t send usper 100 request to a crystal report for tesing report performance Pin
mhd.sbt9-May-14 2:30
mhd.sbt9-May-14 2:30 
AnswerRe: how t send usper 100 request to a crystal report for tesing report performance Pin
V.11-May-14 20:42
professionalV.11-May-14 20:42 
Questionhow to bind data from database to option in classic ASP Pin
Ye Htut7-May-14 18:06
Ye Htut7-May-14 18:06 
AnswerRe: how to bind data from database to option in classic ASP Pin
Peter Leow7-May-14 18:42
professionalPeter Leow7-May-14 18:42 
Questionhow to make dropdownlist select manually inserted value Pin
Nico Haegens7-May-14 0:55
professionalNico Haegens7-May-14 0:55 
AnswerRe: how to make dropdownlist select manually inserted value Pin
Shameel7-May-14 1:23
professionalShameel7-May-14 1:23 
AnswerRe: how to make dropdownlist select manually inserted value Pin
Nico Haegens7-May-14 1:38
professionalNico Haegens7-May-14 1:38 
QuestionControl to be focused on postback Pin
Kandepu Rajesh5-May-14 22:47
Kandepu Rajesh5-May-14 22:47 
AnswerRe: Control to be focused on postback Pin
Tom Marvolo Riddle6-May-14 0:04
professionalTom Marvolo Riddle6-May-14 0:04 
GeneralRe: Control to be focused on postback Pin
Kandepu Rajesh6-May-14 0:12
Kandepu Rajesh6-May-14 0:12 
GeneralRe: Control to be focused on postback Pin
Tom Marvolo Riddle6-May-14 0:54
professionalTom Marvolo Riddle6-May-14 0:54 
AnswerRe: Control to be focused on postback Pin
jkirkerx8-May-14 12:09
professionaljkirkerx8-May-14 12:09 
QuestionSwitching from PHP to ASP.NET Pin
Daniel Lieberwirth (BrainInBlack)4-May-14 22:38
professionalDaniel Lieberwirth (BrainInBlack)4-May-14 22:38 
AnswerRe: Switching from PHP to ASP.NET Pin
Peter Leow4-May-14 23:16
professionalPeter Leow4-May-14 23:16 
GeneralRe: Switching from PHP to ASP.NET Pin
Daniel Lieberwirth (BrainInBlack)4-May-14 23:25
professionalDaniel Lieberwirth (BrainInBlack)4-May-14 23:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.