I'm trying to create a quiz webpage in Visual Studio 2012 and have a little trouble getting the selected value from a dynamically created radiobuttonlist inside a table.
This is how I create the radiobuttonlist (and add it to the table)
Dim NewRow As New TableRow
Dim NewCell As New TableCell
Dim rblOptions As New RadioButtonList
rblOptions.ID = "Option" & intQuestion.ToString
NewCell.Controls.Add(rblOptions)
NewRow.Cells.Add(NewCell)
'Questions is a table
Questions.Rows.Add(NewRow)
This is how I am trying to get the selectedvalue from the radiobuttonlist after the users hits a button.
Dim rbl As RadioButtonList = DirectCast(Page.FindControl("Option" & intQuestion.ToString), RadioButtonList)
If rbl.SelectedValue.ToString = sdrGetQuestions.Item(0).ToString Then
intScore += 1
End If
In both cases the variable intQuestion = 0.
When running the program I come across the error:
Object reference not set to an instance of an object.
On this line:
If rbl.SelectedValue.ToString = sdrGetQuestions.Item(0).ToString Then
Which I think obviously means the program couldn't find the control it had to place in rbl. I'm couldn't find any answers online...
Could anyone point me in the right direction?
This is my .aspx code.
<asp:Table ID="Questions" runat="server">
</asp:Table>
<asp:Button ID="SaveButton" runat="server" Text="Save" />
This is my code behind file code.
I have added dynamic drop-down list.
On save button click, I am retrieving selected value.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim NewRow As New TableRow
Dim NewCell As New TableCell
Dim rblOptions As New RadioButtonList
rblOptions.ID = "Option1"
rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("1", "1"))
rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("2", "2"))
rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("3", "3"))
NewCell.Controls.Add(rblOptions)
NewRow.Cells.Add(NewCell)
'Questions is a table
Questions.Rows.Add(NewRow)
End Sub
Protected Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
If Page.IsValid Then
Dim rbl As RadioButtonList = DirectCast(Questions.FindControl("Option1"), RadioButtonList)
If rbl.SelectedValue.ToString = "ExpectedValue" Then
End If
End If
End Sub
Related
I am creating a simple guessing game. Many of the controls are repetitive, so I'd like to loop through them to databind and search for enabled or disabled controls. When I try to run the page after coding either foreach method in my code, I get an "object reference not set to an instance of an object" error.
How would I fix this without creating a new instance of my control? I've tried that already; it doesn't overwrite the existing control on my aspx page. Obviously it is creating new ones somewhere, but I'm not sure where; I don't see well. When I work on the computer I do it at 350% - 400% magnification.
Here is a sample of my aspx page:
<div class="Character">
<img src="Images/1.png" style="width: 100%;" /><br />
<asp:DropDownList ID="DDL1" runat="server" CssClass="BigText"></asp:DropDownList><br />
<asp:Button ID="BTN_SubmitGuess1" runat="server" CssClass="BigText Button2" Text="Submit Guess" />
</div>
Here is a sample of my codebehind. I have written my code for both databinding and finding enabled or disabled controls in it:
Dim arr() As String = {"One", "Two", "Three", "Four", "Five"}
Dim ddlControls() As DropDownList = {DDL1, DDL2, DDL3, DDL4, DDL5}
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
'What I have now
DDL1.DataSource = arr
DDL1.DataBind()
...
'What I'd like to have
For Each ddl As DropDownList In ddlControls
ddl.DataSource = arr
ddl.DataBind()
End If
Dim remaining As Integer
For Each ddl As DropDownList In ddlControls
If ddl.Enabled = True Then
remaining += 1
End If
Next
LBL_Remaining.Text = CStr(remaining)
End Sub
You have to set the values of your ddlControls array while the page is loading once the DropDownList controls are already created :
Dim arr() As String = {"One", "Two", "Three", "Four", "Five"}
Dim ddlControls() As DropDownList
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
ddlControls = {DDL1, DDL2, DDL3, DDL4, DDL5} ' <<======= HERE
DDL1.DataSource = arr
DDL1.DataBind()
For Each ddl As DropDownList In ddlControls
ddl.DataSource = arr
ddl.DataBind()
Next
End If
Dim remaining As Integer
For Each ddl As DropDownList In ddlControls
If ddl.Enabled = True Then
remaining += 1
End If
Next
Me.Title = CStr(remaining)
End Sub
I am trying to set the selected value of a radiobuttonlist that is a template column within a gridview. I am having to do this using the code behind as the database field containing the data (Status) contains null values hence I cant used SelectedValue='<%# Bind("Status") %>' on the asp side.
It has been suggested that I use onRowDataBound to do this and use DataItem to retrieve the value from the datasource and used it to set the radiobuttonlist selected value but I'm not sure how to do this in vb code.
I've tried the following:
Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim radioList = TryCast(e.Row.FindControl("rblActions"), RadioButtonList)
' this will be the object that you are binding to the grid
Dim myObject = TryCast(e.Row.DataItem, DataRowView)
Dim sStatus As String = Convert.ToString(myObject("Status"))
If sStatus <> Nothing Then
radioList.SelectedValue = sStatus
End If
End If
End Sub
but it hasnt work. Any help would be appreciated. Thanks
Found my own solution as follows:
Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim rbl As RadioButtonList = CType(e.Row.FindControl("rblActions"), RadioButtonList)
Dim selected As String = e.Row.DataItem("Status").ToString
If Not IsNothing(selected) Then
rbl.SelectedValue = selected
End If
End If
End Sub
I am trying to bind radiobutton list dynamically in programm. But I do
not know how to insert value.
Aspx Page code
<asp:Table ID="tblSponser" runat="server" cellpadding="0" cellspacing="10" Visible="false"></asp:Table>
on dropdownlist selectedindex event I am Binding Radio Button in Code
behind
Protected Sub ddlVotes_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlVotes.SelectedIndexChanged
For SponserId = 0 To sponserDtst.Tables(0).Rows.Count - 1
Dim tr As New TableRow
Dim c1 As New TableCell
Dim c2 As New TableCell
Dim rbtyes As New RadioButtonList
rbtyes.ID = "rbtypes" & SponserId
rbtyes.ClientIDMode = UI.ClientIDMode.Static
rbtyes.Items.Add(New ListItem("Yes"))
rbtyes.Items.Add(New ListItem("No"))
rbtyes.RepeatDirection = RepeatDirection.Horizontal
c2.Controls.Add(rbtyes)
c2.Controls.Add(rbtyes)
tr.Controls.Add(c1)
tr.Controls.Add(c2)
tblSponser.Controls.Add(tr)
Below code is not working. How to get value of radiobuttonlist and
store in database on button click event.
Protected Sub btnSubmitVote_Click(sender As Object, e As System.EventArgs) Handles btnSubmitVote.Click
Dim i As Integer
Dim x As Integer = 5
For i = 0 To x - 1
Dim str As String = "rbtypes" & i
Dim rb As RadioButtonList = CType(tblSponser.FindControl(str), RadioButtonList)
Dim ix As String = rb.SelectedValue
Next
At which point in the page load process do you execute your code? You should be doing this in OnInit method in order to work properly.
Take a look at this – it provides a good info on asp.net page life cycle
ASP.NET Page Life Cycle Overview
I'm trying to get a drop down list control to work in FormView. I need to have the list be a filtered view of a certain table and still be bound to a field in the data I'm editing. I've tried setting the item data programatically, ant that works but then the data binding
doesn't work, It tries to insert null into the database.
This is the code I've tried. I've also tried doing the same thing in several other events, it still tries to insert null into the database.
<asp:DropDownList ID="lstManagers" runat="server"
OnDataBound ="ManagersLoad"
SelectedValue='<%# Bind("UserName") %>' Width="100%"
DataSourceID="TimeOff" DataTextField="UserName" DataValueField="UserName">
</asp:DropDownList>
Protected Sub ManagersLoad(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lst As DropDownList = FormView1.FindControl("lstManagers")
'get list of managers
Using ef As New TimeOffData.TimeOffEntities
For Each item As ListItem In lst.Items
Dim li As ListItem = item
item.Text = (From x In ef.TimeOffUsers Where x.UserName = li.Value Select x.FirstName & " " & x.LastName).FirstOrDefault
Next
End Using
End Sub
I took all the data binding stuff of the control and just decide it would be easier to do it manually. I've changed the code to this,
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
Dim lst As DropDownList = FormView1.FindControl("lstManagers")
e.Values.Item("ManagerName") = lst.SelectedValue
End Sub
Protected Sub ManagersLoad(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lst As DropDownList = FormView1.FindControl("lstManagers")
'get list of managers
Using ef As New TimeOffData.TimeOffEntities
Dim mng = From x In ef.TimeOffUsers Where x.IsManager = True
For Each item In mng
lst.Items.Add(New ListItem(item.FirstName & " " & item.LastName, item.UserName))
Next
End Using
End Sub
whats wrong in this code
i have a Imagebutton2 in gridview whose commandname is xxx and i have added modalpopup extendar panel2 with ImageButton2 i want when image button 2 will be clicked then the modalpopup will display and retrieve the values from selected gridview row to the literal3 control of panel 1 which acts as a modalpopup control for gridview ?
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim myrow As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
If e.CommandName = "xxx" Then
Dim lab5 As Label = DirectCast(myrow.FindControl("Label5"), Label)
Dim lit3 As Literal = Me.Panel2.FindControl("Literal3")
lit3.Text = lab5.Text
End If
End Sub
Many things can fail:
e.CommandArgument may not be an integer
e.CommandSource may not be of Control type
the NamingContainer may not be of GridViewRow type
myRow can be null, at least when the code is ran
the "Label5" control may not be found, or may not be of Label type, when the code is ran
the "Literal3" control may not be found (null ref) or may not be of Literal type, when the code is ran
...