这是我们已经有一段时间以来一直存在的问题的修改版本。
我们有一个在线投票应用程序,其中包含一堆radioButtonList,其值从数据库中动态填充。
示例设置如下:
CandidateA CandidateB 写在候选人
控制ID为radiobuttonList1的radiobuttonList将使用数据库中的这些值填充,包括Write In Candidate值。
用户可以单击单选按钮选择CandidateA,CandidateB,或单击Write In Candidate。
我们的问题是 Write In Candidate 单选按钮没有允许用户输入他们选择的候选人的文本框。
我很难修改我的脚本,因此Write In Candidate单选按钮除了它之外还有一个文本框,所以当用户点击Write In Candidate单选按钮时,他们可以输入候选人的姓名。
示例:
如何修改下面的代码以包含文本框功能?
以下是我目前使用的代码:
<!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>
<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>
Instructions: Select the candidate(s) of your choice by clicking the Checkbox <img src="images/check.png" alt="" /> and the the Oval
<img src="images/oval.png" alt="" /> adjacent to the candidate's name.
<img src="images/line.gif" alt="" />
</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>
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
运行上面的代码时出现以下错误:
Object reference not set to an instance of an object.
Line 36: rb1.Items.Add("writein")
答案 0 :(得分:0)
你必须纠正代码:
Dim rb1 As CheckBoxList =(CheckBoxList)Page.FindControl("CheckBoxList1")