Radio button list malfunction - asp.net

How can I check if a radio button inside a radio button list is checked?
I used if radiobuttonlist1.selectedindex > -1 and then I checked a radio button, but it is not reading the selected radio button, it is always going to the else statement.
this is my code:
Dim rl1 As RadioButtonList = New RadioButtonList()
If rbl1.SelectedIndex > -1
Label2.Text = "Checked"
Else
Label2.Text = "Not Checked"
End If

You can do this.
Go to your RadioButton_CheckedChanged event and add this code
If RadioButton1.Checked = True Then
Me.Label1.Text = "Checked"
Else
Me.Label1.Text = "UnChecked"
End If

Are you using the correct variable name in your code? You are declaring rl1 but validating rbl1. If it's not the case, check if the following code helps:
Dim rl1 As RadioButtonList = New RadioButtonList()
'Just adding some items and selecting one of them for demonstration
rl1.Items.Add(New ListItem("Option 1", "1"))
rl1.Items.Add(New ListItem("Option 2", "2"))
rl1.SelectedIndex = 1
If rl1.SelectedIndex > -1 Then
MsgBox("Checked")
Else
MsgBox("Not Checked")
End If

Dim rl1 As RadioButtonList = New RadioButtonList()
So you created the above? How does the above know or have anything to do with the web page? There is zero relationship between some variable you create in code as opposd to the controls you have on the web page?
You want
MyRadioButtonOnTheWebPage.Selectedindex
Probably this:
RadioButtonList1.Selected index.
So, you would not out of the blue create + dim some variable that has nothing to do with the actual Radio button list on the actual web page. I mean, if you have 3 RadioButton lists on your web page, which one does the above refer to?
So, if you have a Radiobuttion list "1" like this:
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True"
RepeatDirection="Horizontal">
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Tow</asp:ListItem>
<asp:ListItem>Three</asp:ListItem>
</asp:RadioButtonList>
Then either a button postback, or EVEN say set the autopostback = true for the radio button list.
Then in code you can use:
Debug.Print(RadioButtonList1.SelectedIndex)
Debug.Print(RadioButtonList1.SelectedValue)
So above would get the index value, or the actual value of the radio button.
And if you have auto postback = true, then you can/would use the event (changed) like this that will trigger when you choose/change the selection in the radio button
Protected Sub RadioButtonList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles RadioButtonList1.SelectedIndexChanged
Debug.Print(RadioButtonList1.SelectedIndex)
Debug.Print(RadioButtonList1.SelectedValue)
End Sub

Related

How do I access properties of non-fired LinkButtons in an ASP RepeaterControl?

I have a navigation bar which is dynamically populated with LinkButtons by an ASP repeaterControl.
I have no problem accessing, and setting properties for the clicked LinkButton. This I can do using the sender object from the fired LinkButton. Once a LinkButton is clicked, it is highlighted in bold.
My problem is to clear the bold property of the previously clicked linkButton when a new LinkButton ( another RepeaterItem within the same repeater) is clicked.
Any ideas on this, please? Many thanks!
ps.
I cannot access the buttons through their ID, since they all have the same ID within the repeater.
I have unique arguments on each RepeaterItem (CommandArgument), but when I try to iterate through all linkbuttons, only static linkbuttons are found, none inside the repeater. See below:
Dim c As Control
For Each c In Form1.Controls
If TypeOf c Is LinkButton Then
MsgBox(DirectCast(c, LinkButton).CommandArgument)
End If
Next c
Try this:
For each item as RepeaterItem in YourRepeaterControl.Items
Dim button as LinkButton = item.FindControl("YourLinkButtonId")
If button IsNot Nothing Then
'Do whatever you want here
End If
Next

grid view checkbox and javascript not informing server side code?

I've got a asp.net gridview and inside of the grid view I have a check box at the header of the grid view like so:
<HeaderTemplate>
<asp:CheckBox Width="1px" ID="HeaderLevelCheckBox" AutoPostBack="true" OnCheckedChanged="SelectAllRows" runat="server" />
</HeaderTemplate>
This gives me a nice little check box at the top of the grid view...the event OnCheckedChanged calls a function called SelectAllRows that looks like this:
Public Sub SelectAllRows(ByVal sender As Object, ByVal e As System.EventArgs)
Dim gr As GridViewRow = DirectCast(DirectCast(DirectCast(sender, CheckBox).Parent, DataControlFieldCell).Parent, GridViewRow)
Dim h As CheckBox = DirectCast(gr.FindControl("HeaderLevelCheckBox"), CheckBox)
For Each Row As GridViewRow In Me.gvLineItems.Rows
Dim cb As CheckBox = CType(Row.FindControl("chkSelector"), CheckBox)
cb.Checked = h.Checked
Next
End Sub
So if I click this header checkbox it checks all of the items in the gridview, and if I uncheck it, it unchecks all the items in the gridview. This works fine...but what doesnt seem to work is if the page loads up and I check the grid view header checkbox to true and it selects all the items in the gridview, then i click a button such as a DELETE button that calls some server side code. That code simply loops through the grid view and checks if the checkbox has been checked, if it is it calls code to delete an item. Something to this effect:
For Each Row As GridViewRow In Me.gvLineItems.Rows
Dim cb As CheckBox = CType(Row.FindControl("chkSelector"), CheckBox)
Dim lID As Long = Convert.ToInt32(gvLineItems.DataKeys(Row.RowIndex).Value)
If cb IsNot Nothing AndAlso cb.Checked Then
'ok to delete
End If
Next
When I place a watch and debug on this it seems that the value cb is always false...
Even though it was set to true when I clicked the header checkbox...
What gives ???
The actual chkSelector in the grid view is for each row and it looks like this:
<ItemTemplate>
<asp:CheckBox ID="chkSelector" runat="server" onclick="ChangeRowColor(this)" />
</ItemTemplate>
Also I am already checking for postback..that is not the issue, remember chkSelector does not autopostback...
Thanks
I doubt, your gridview is rebinding on a Delete button click, because a click of the Delete button loads the page first where it will rebind and your checkbox's become unchecked again. I think you are binding your gridview some where in the page load event.
You have to do something like this
If(!Page.IsPostBack)
{
//Gridview Binding Code goes here....
}
Edit: Alternatively you can check/uncheck rows using javascript. It will save a round trip to the server side and resolve your current issue as well.
Here is complete code
<script language="javascript" type="text/javascript">
function SelectAll(spanChk,grdClientID) {
var IsChecked = spanChk.checked;
var Chk = spanChk;
Parent = document.getElementById(grdClientID);
var items = Parent.getElementsByTagName('input');
for(i=0;i<items.length;i++)
{
if(items[i].type=="checkbox")
{
items[i].checked=document.getElementById(spanChk).checked;
}
}
}
<HeaderTemplate>
<asp:CheckBox runat="server" ID="chkHeader" onclick="SelectAll('<%=chkHeader.ClientID %>, <%=yourGrid.ClientID %>') />
</HeaderTemplate>

Updating placeholder during runtime

here is the problem I am having with placeholder:
I have a repeater and within that repeater, I have an item template. Now this template is formatted with a couple of tables, but for this question I have removed them to make things easier to read:
<asp:Repeater ID="Repeater1" OnItemDataBound="R1_ItemDataBound" runat="server">
<ItemTemplate>
<asp:PlaceHolder ID="phAnswers" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:Repeater>
Then, on the event OnItemDataBound, I create a new placeholder, bind it to the existing on (phAnswers), however the placeholder is not updated with the radiobuttons/textboxs that are created:
Dim rdList As New RadioButtonList
Dim newRadio As New RadioButton
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim tempPH As PlaceHolder
tempPH = e.Item.FindControl("phAnswers")
For x As Integer = 0 To (t_MC.Count - 1)
newRadio = New RadioButton
newRadio.ID = "Answer" + x.ToString
newRadio.Text = t_MC(x).Value
rdList.Controls.Add(newRadio)
Next
tempPH.Controls.Add(rdList)
Any ideas why phAnswers is not updated with the new tempPH placeholder?
Cheers
OnItemDataBound may be too late to add controls. Try it in OnItemCreated and see if that helps. It's a quick test - just change your repeater event declaration like this:
OnItemCreated="R1_ItemDataBound"
If this idea doesn't help, you can easily switch it back.
Edit - I just noticed something. To populate a RadioButtonList, you should use ListItems, like this:
ListItem item - new ListItem("your text", "your value");
rdList.Items.Add(item);
This is probably why your RadioButtonList did not appear, but lone radio buttons worked.
Try using a Panel instead of a PlaceHolder

Adding Checkbox in asp.net Listview control to allow multiple delete

I am trying to display checkboxes in front of every row in list view. So that after selecting the desired checkboxes user clicks on delete button and we should delete that records.
but how can it be done?
Add Checkbox in markup
<asp:CheckBox ID="ChkSelect" runat="server" />
code behind as follows:
Dim ChkSelect As CheckBox = Nothing
Dim ListItem As ListViewDataItem = Nothing
Dim ItemList As List(Of Person) = New List(Of Person)
Dim Item As Person= Nothing
For Each ListItem In MyDataList.Items
ChkSelect = ListItem.FindControl("ChkSelect")
If ChkSelect.Checked Then
Dim UIN As Integer = _
MyDataList.DataKeys(ListItem.DisplayIndex).Value.ToString()
Item = Persons.GetData(UIN)
Item.Deleted = True
ItemList.Add(Item)
End If
Next
Data = Persons.UpdateBulk(ItemList)
If Data = True Then
BindMyData()
End If
You need to create a template for the items in the ListView, put the checkbox in it, and then get all the items that were checked when you click the Delete button. You could either keep track of the selected items on the client or the server, but it would always require some work to persist them.
Here's an article on using templates with the ListView:
http://msdn.microsoft.com/en-us/library/bb398790.aspx#CreatingTemplatesForTheListViewControl
I use GridView Template if I want to do that in GridView...try to look if theres ListView Template if there is.

Setting LinkButton Title in ASP.Net Wizard Sidebar Template

Playing around with customizing the appearance of the Wizard control in ASP.Net, and I've found out how to disable the sidebar buttons using the SideBarTemplate and catching the OnItemDataBound event. All pretty easy. What I want to do now is to modify the text of the rendered LinkButton to prefix the step name with something like ">>" for the current step.
So, in my ItemDataBound event handler for the SideBarList, I have the following code:
Dim stepCurrent As WizardStep = e.Item.DataItem
Dim linkCurrent As LinkButton = e.Item.FindControl("SideBarButton")
If Not stepCurrent Is Nothing Then
Trace.Write("SideBar", "Current Step = " & stepCurrent.Wizard.ActiveStep.Name)
Trace.Write("Sidebar", "Link Button = " & linkCurrent.Text)
linkCurrent.Enabled = False
If stepCurrent.Wizard.ActiveStepIndex = e.Item.ItemIndex Then
linkCurrent.Style.Add(HtmlTextWriterStyle.Color, "#000000")
linkCurrent.Style.Add(HtmlTextWriterStyle.FontWeight, "bold")
linkCurrent.Text.Insert(0, ">> ")
End If
End If
However, what I find is the trace output is showing an empty string for the lunkbutton text, but the style changes work.
Am I trying to set the text in the wrong place?
Thanks
I did not find any way to change "SideBarButton" text property that is why I added
another link button control in SelectedItemTemplate to DataList and set visible="fasle" in SideBarButton. SelectedItemTemplate will be used to render item in sidebar for current wizard step.
<ItemTemplate>
<asp:LinkButton ID="SideBarButton" runat="server"/>
</ItemTemplate>
<SelectedItemTemplate>
<asp:LinkButton ID="ActiveSideBarButton" runat="server">
<asp:LinkButton Visible="false" ID="SideBarButton"unat="server"/>
</SelectedItemTemplate>
In OnItemDataBound event do something like
Dim stepCurrent As WizardStep = e.Item.DataItem
If stepCurrent.Wizard.ActiveStepIndex = e.Item.ItemIndex Then
Dim linkCurrent As LinkButton = e.Item.FindControl("ActiveSideBarButton")
linkCurrent.Style.Add(HtmlTextWriterStyle.Color, "#000000")
linkCurrent.Style.Add(HtmlTextWriterStyle.FontWeight, "bold")
LinkCurrent.Text = stepCurrent.Title;
linkCurrent.Text.Insert(0, ">> ")
End If
SideBarButton will not be rendered because of visible="false" and only ActiveSideBarButton for current step will be rendered with parameters you need.

Resources