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
Related
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
I have a web application that reads from a SQL database to create a gridview. From this gridview I want to select some data and transfer it back to another page via session variable. I have tried to create continuity by checking the boxes previously selected, but if I uncheck them and send the information back, the unchecked box remains in the session variable data.
This is the code that checks the session variable and checks boxes accordingly.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim String1 As String = ""
Dim String2 As String = ""
Dim boolcheck As Boolean = False
For Each row As DataRow In employeelist.Rows
String1 = row.Item(0)
For Each row2 As GridViewRow In EmployeeListGridView.Rows
String2 = row2.Cells(1).Text
If String1 = String2 Then
Dim checkRow As CheckBox = TryCast(row2.Cells(0).FindControl("checkRow"), CheckBox)
checkRow.Checked = True
End If
Next
Next
End Sub
This is the code that stores a datatable to the session variable
Protected Sub SelectButton_Click(sender As Object, e As EventArgs) Handles SelectButton.Click
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("ZID"), New DataColumn("Last Name")})
For Each row As GridViewRow In EmployeeListGridView.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim checkRow As CheckBox = TryCast(row.Cells(0).FindControl("checkRow"), CheckBox)
If checkRow.Checked Then
Dim zid As String = row.Cells(1).Text
Dim lastname As String = row.Cells(3).Text
dt.Rows.Add(zid, lastname)
End If
End If
Next
Session("employeedt") = dt
Response.Redirect("ManagerTraining.aspx")
End Sub
If a box is checked on page load from the first bit of code, unchecked by the user, the second piece of code will step into the "If checkRow.checked" statement even though the user has unchecked that box. The checkboxes are contained in a gridview. I am using ASP.NET and VB. I feel like the changes aren't being committed if the user unchecks.
Please read about the ASP.NET lifecycle.
Page.Load always executes before the event handlers on postback. You should wrap the code setting the checkbox values in
If not isPostback Then
Dim checkRow As CheckBox = TryCast(row2.Cells(0).FindControl("checkRow"), CheckBox)
checkRow.Checked = True
End If
i want to get value from grid view text box in Text Change Event but error comes
Object refrence is not set of an instance of an Object
Protected Sub onDebitChange(ByVal sender As Object, ByVal e As EventArgs)
Dim a, b As Integer
With GridView3
Dim rows As Integer = .Rows.Count
For i As Integer = 0 To rows - 1
Dim txtDebit As TextBox = CType(GridView3.Rows(i).FindControl("TotalDebit"), TextBox)
a = Val(txtDebit.Text)
b += a
Next
DebitBalance.Text = b
End With
End Sub
i also had tried for each loop but error is same
please help
You need to put the name of the ItemTemplate control correctly, in onDebitChange method please fix the control name to "Debit" not "TotalDebit" only:
Dim txtDebit As TextBox = CType(GridView3.Rows(i).FindControl("Debit"), TextBox)
Hope this helps.
I want to get the value of my textbox that I created dynamically when I click a button
I need to do this cause the value of my textbox is used for retrieve data from database
how could I achieved this thing??
the flow is Button click - creating textbox - filling textbox with value - Button Click - Get Text of textbox
here is my code to make the textbox
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 0 To 4
textbox = New TextBox With {.ID = "TextBox" & i}
plchld.Controls.Add(textbox)
Next
End Sub
I have tried something like this but the code didn't work
Protected Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
Dim a(5) As String
For i As Integer = 0 To 4
a(i) = CType(plchld.FindControl("Textbox" & i), TextBox).Text
Next
End Sub
thanks in advance for any help
edit for the answer
I've found the way to solve this. I use request.form to get the value of my textbox.
Thanks for anyone that participating
Regards,
Julian
This is how I have done in my asp.net application.
Creating dynamic control
TextBox txtDate = new TextBox();
txtDate.EnableViewState = true;
txtDate.ID = "PreixValue" + 1;
txtDate.Text = "07 Feb 2014"
pnl.controls.add(txtdate);
To retrieve the value from that textbox
DateTime datefrom = DateTime.Now ;
for (int cnt = 0; cnt < Request.Form.Count; cnt++)
{
if (Request.Form.AllKeys[cnt].Contains("Prefixvalue"))
{
int ParamStartPoint = Request.Form.AllKeys[cnt].IndexOf("Prefix") + 4;
int ParamNameLength = Request.Form.AllKeys[cnt].Length - ParamStartPoint;
string[] ControlName = Request.Form.AllKeys[cnt].Substring(ParamStartPoint, ParamNameLength).Split('$');
if (ControlName[0] == "Date From")
{
datefrom = DateTime.Parse(Request.Form[cnt]);
//datefrom has value now
}
}
}
This is how I have done in my web application, but there may be other ways achieve this.
basically when you create Dynamic control in webform this will be available through Request.Form.
hope this helps you.
Protected Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
Dim a(5) As String
For i As Integer = 0 To 4
Dim anotherObj As TextBox = Me.Controls.Item("Textbox" & i)
a(i) =anotherObj.Text
Next
The issue is that dynamic controls are lost on a postback so when the OkButton click event is handled, there is nothing inside your plchld control. You must recreate your controls with the same ID on postback if you want to retrieve the text in the textboxes.
Using your code, all you need to do is on postback determine if the textboxes were created and if so, recreate them.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Determine if the text boxes were created and if so, recreate them.
If CBool(ViewState("TextBoxesCreated")) Then
CreateTextBoxes()
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
CreateTextBoxes()
ViewState("TextBoxesCreated") = True
End Sub
Private Sub CreateTextBoxes()
For i As Integer = 0 To 4
plchld.Controls.Add(New TextBox With {.ID = "TextBox" & i})
Next
End Sub
Protected Sub OkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OkButton.Click
Dim a(4) As String
For i As Integer = 0 To 4
a(i) = CType(plchld.FindControl("Textbox" & i), TextBox).Text
Next
End Sub
I don't know the full extent of what you are doing but I would suggest not creating them dynamically if you don't need to. Just show or hide the textboxes instead.
Reference: http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i
I am trying to perfrom a postback with a Dropdownlist in a Gridview in Edit Mode. I am having problems getting to the value of the drop down. I can not do this in the RowDatabound event.
Postback is not over writing the row, I am stepping into the DropDownList7_SelectedIndexChanged event where I want to perform some operations, so it hasn't even gotten there. I do have the If Postback in the page load event.
Protected Sub DropDownList7_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim row As GridViewRow = DirectCast(GridView1.Rows.Item(0), GridViewRow)
Dim newNumDDL As DropDownList = row.Cells(0).FindControl("DropDownList7")
Dim newVal As Integer = newNumDDL.SelectedValue
Dim newKey As String = newNumDDL.SelectedItem.ToString
Dim newindex As Integer = newNumDDL.SelectedIndex
Issue I believe is with the findcontrol I can not find the DDL, keeps coming back nothing.
Thanks for any help.
Can you not use:
Dim newNumDDL As DropDownList = DirectCast(sender, DropDownList)