find radiobuttonlist in gridview - asp.net

How can I find radiobuttonlist and it's items in gridview?
Gridview code:
<asp:TemplateField HeaderStyle-Width="20px">
<ItemTemplate>
<asp:RadioButtonList ID="rbl_NIU" runat="server" RepeatDirection="Horizontal" BorderStyle="None" BorderWidth="0px" BorderColor="Transparent">
<asp:ListItem Text="N" Value="1" ></asp:ListItem>
<asp:ListItem Text="I" Value="2" ></asp:ListItem>
<asp:ListItem Text="U" Value="3" ></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
and .vb code:
Protected Sub gvImport_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvImport.RowDataBound
'Dim radio As RadioButtonList = DirectCast(e.Row.FindControl("rbl_NIU"), RadioButtonList)
'Dim radio As RadioButtonList = TryCast(sender, RadioButtonList)
Dim radio As RadioButtonList = CType(e.Row.FindControl("rbl_NIU"), RadioButtonList)
For Each par As Paraugs In list
For Each item As RadioButton In radio.Items
par.Write.ToString()
Next
Next
End Sub
I have tried everything but every time I get "object reference not set to an instance of an object" in "For Each item" and On break-point "radio" returns "Nothing"
Where is the problem? Please help!
Thanks!

When you run the gvImport.RowDataBound this will still count headers, so you need a quick if statement at the top of the Sub:
If e.Row.RowType = DataControlRowType.DataRow Then
'Look for radio button list in this
End If

Related

In ASP.NET, how do I change the visibility of a button in the same column of a gridview itemtemplate when the other button is clicked?

I have the following gridview:
<asp:GridView ID="gridOpenMECs" runat="server">
<Columns>
<ItemTemplate>
<asp:ImageButton ID="btnShow" runat="server" ImageUrl="xxx.png"
OnClick="btnShow_OnClick" />
<asp:ImageButton ID="btnHidden" runat="server"
ImageUrl="yyy.png" Visible="false" />
</ItemTemplate>
</Columns>
</asp:GridView>
When button1's onclick serverside event is fired I want to obtain a handle on button2 so that I may change its Visible attribute to True.
Protected Sub btnShow_OnClick(ByVal sender As Object, ByVal e As ImageClickEventArgs)
Dim btn as ImageButton = CTYPE(sender, ImageButton) 'get the sending button handle
'' what next to make btnHidden visible?
End Sub
How can I accomplish this? Thank you.
Sorry, C# speak ...
GridViewRow whichrow = (GridViewRow)btn.NamingContainer;
ImageButton btnHidden = (ImageButton)whichrow.FindControl("btnHidden")

Get a DropDownList item from a Gridview

I'm using Visual Basic with ASP.NET. I have a GridView table with a DroDownList column and I need a way to get the selected item from it. Now I'm using an ImageButton in order to get the selected item from the DropDownList to pop-up as a message box. I already know how to get an integer from a boundfield. However, using the same code to get the DropDownList item won't work.
This is a snippet from my code:
ASP code:
<asp:BoundField DataField="Case#" HeaderText="Case#" ReadOnly="True" />
<asp:TemplateField HeaderText="Surgery Time">
<ItemTemplate>
<asp:DropDownList ID="Time_Slot" runat ="server">
<asp:ListItem Selected="True" Value="0">Select...</asp:ListItem>
<asp:ListItem Value="1">8:00</asp:ListItem>
<asp:ListItem Value="2">9:00</asp:ListItem>
<asp:ListItem Value="3">10:00</asp:ListItem>
<asp:ListItem Value="4">11:00</asp:ListItem>
<asp:ListItem Value="5">12:00</asp:ListItem>
<asp:ListItem Value="6">1:00</asp:ListItem>
<asp:ListItem Value="7">2:00</asp:ListItem>
<asp:ListItem Value="8">3:00</asp:ListItem>
<asp:ListItem Value="9">4:00</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField buttontype="Image" ImageUrl="~/Images/check.jpg" commandname="Accept" HeaderText="Accept" SortExpression="Accept" />
Visual Basic code:
Sub GridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Accept" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim selectedRow As GridViewRow = GridView1.Rows(index)
'This value is retrieved from the databound
Dim contactCell As TableCell = selectedRow.Cells(0)
Dim contact As String = contactCell.Text
'however here it is not retrieved from the dropdownlist
Dim contactCell2 As TableCell = selectedRow.Cells(1)
Dim contact2 As String = contactCell2.Text
MsgBox("case number is" + contact + "time is" + contact2)
End If
End Sub
Try this.
If e.CommandName = "Accept" Then
Dim index = e.CommandArgument
Dim timeSlot = CType(gridview1.Rows(index).FindControl("Time_Slot"), DropDownList)
Dim selectedTimeSlot = timeSlot.SelectedValue
End If

Gridview template radiobuttonlist SelectedInxedChanged, Getting the row the list was in when it fired

I have a Gridview in which one of the template fields has a radiobuttonlist and a dropdownlist. How can I access the row that the radiobuttonlist is in on the SelectedIndexChanged event, so I don't end up updating all of the dropdownlist inside the that template field of the gridview. I don't have any code currently but any help would be greatly appreciated
<asp:TemplateField HeaderText="Column with ListControls" >
<ItemTemplate>
<asp:DropDownList ID="DropdownList1" OnSelectedIndexChanged="SomethingChanged" AutoPostBack="true" runat="server" >
<asp:ListItem Text="1"></asp:ListItem>
<asp:ListItem Text="2"></asp:ListItem>
</asp:DropDownList>
<asp:RadioButtonList ID="RadioButtonList1" OnSelectedIndexChanged="SomethingChanged" AutoPostBack="true" runat="server">
<asp:ListItem Text="1"></asp:ListItem>
<asp:ListItem Text="2"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
Codebehind VB.NET:
Protected Sub SomethingChanged(ByVal sender As Object, ByVal e As EventArgs)
'in this example this handler is used for both, Dropdownlist and RadiobuttonList'
Dim listControl = DirectCast(sender, ListControl)
Dim row = DirectCast(listControl.NamingContainer, GridViewRow)
Dim item = listControl.SelectedItem
'with FindControl on the row you could also find controls in other columns...'
End Sub
C#:
protected void SomethingChanged(object sender, EventArgs e)
{
//in this example this handler is used for both, Dropdownlist and RadiobuttonList
var listControl = (ListControl)sender;
var row = (GridViewRow)listControl.NamingContainer;
var item = listControl.SelectedItem;
//with FindControl on the row you could also find controls in other columns...
}

How do I keep Selected ListItem from ignoring other ListItems?

When I run code the below, the If statement never resolves to 'True'. It always shows 'Assm' as the SelectedItem, even if I check all the checkboxes.
So how do I allow 'Assm' to be checked by default AND have the code-behind see that the other checkboxes are checked?
<asp:CheckBoxList ID="qualityChecks" runat="server" RepeatDirection="Horizontal" TabIndex="8">
<asp:ListItem Text="Assm" Selected="True"></asp:ListItem>
<asp:ListItem Text="Qual"></asp:ListItem>
<asp:ListItem Text="PMgr"></asp:ListItem>
<asp:ListItem Text="Plant"></asp:ListItem>
</asp:CheckBoxList>
If qualityChecks.SelectedItem.Text = "Qual" Then
'Some Code
End If
SelectedItem of a CheckBoxList works that way.
What you want to do is iterate through the ListItems and see if they are Checked.
For each li as ListItem in qualitychecks.items
if li.checked and li.text = "Qual" then
'some code
end if
next
Try creating an empty CheckListBox:
<asp:CheckBoxList ID="qualityChecks" runat="server" RepeatDirection="Horizontal" TabIndex="8">
</asp:CheckBoxList>
and than adding the ListItems on Page_Load
Protected Sub Page_Load(sender As Object, e As EventArgs)
Dim l1 As New ListItem()
Dim l2 As New ListItem()
Dim l3 As New ListItem()
Dim l4 As New ListItem()
l1.Text = "Assm"
l2.Text = "Qual"
l3.Text = "PMgr"
l4.Text = "Plant"
If Not Page.IsPostBack Then
l1.Selected = True
qualityChecks.Items.Add(l1)
qualityChecks.Items.Add(l2)
qualityChecks.Items.Add(l3)
qualityChecks.Items.Add(l4)
End If
End Sub
Try:
If qualityChecks.SelectedValue = "Qual" Then
'Some Code
End If
As you have specified no value for the list items, they take the value of the Text

ASP.NET, VB: checking which items of a CheckBoxList are selected

I know this is an extremely basic question, but I couldn't find how to do this in VB... I have a CheckBoxList where one of the options includes a textbox to fill in your own value. So I need to have that textbox become enabled when its checkbox (a ListItem in the CheckBoxList) is checked. This is the code behind, I'm not sure what to put in my If statement to test if that certain ListItem is checked.
Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged
If ___ Then
txtelect.Enabled = True
Else
txtelect.Enabled = False
End If
End Sub
You can loop through the checkboxes in a CheckBoxList, checking each to see if it is checked. Try something like this:
For Each li As ListItem In CheckBoxList1.Items
If li.Value = "ValueOfInterest" Then
'Ok, this is the CheckBox we care about to determine if the TextBox should be enabled... is the CheckBox checked?
If li.Selected Then
'Yes, it is! Enable TextBox
MyTextBox.Enabled = True
Else
'It is not checked, disable TextBox
MyTextBox.Enabled = False
End If
End If
Next
The above code would be placed in the CheckBoxList's SelectedIndexChanged event handler.
Assuming that your aspx look similar to this:
<asp:TextBox ID="txtelect" runat="server"></asp:TextBox>
<asp:CheckBoxList id="CheckBoxList1" runat="server" autopostback="true" >
<asp:ListItem Text="enable TextBox" Value="0" Selected="True"></asp:ListItem>
<asp:ListItem Text="1" Value="1" ></asp:ListItem>
<asp:ListItem Text="2" Value="2" ></asp:ListItem>
<asp:ListItem Text="3" Value="3" ></asp:ListItem>
</asp:CheckBoxList>
you can use the ListItem's-Selected property to check if your Textbox should be enabled:
Private Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged
'use the index of the ListItem where the user can enable the TextBox(starts with 0)'
txtelect.Enabled = CheckBoxList1.Items( 0 ).Selected
End Sub
I wouldn't do it this way, it's very inefficient. You are hitting the server just to enable or disable a text box, you should use javascript. The code below would be better
<asp:DataList ID="mylist" runat="server">
<ItemTemplate>
<input type="checkbox" id="chk<%#Container.ItemIndex %>" onclick="document.getElementById('txt<%#Container.ItemIndex %>').disabled=(!this.checked);" />
<input type="text" id="txt<%#Container.ItemIndex %>" disabled="disabled" />
</ItemTemplate>
</asp:DataList>
Funtion to get them in a string
Function ValueSelected(cbl As CheckBoxList, separator As String) As String
Dim s As String = ""
Dim cb As ListItem
For Each cb In cbl.Items
If cb.Selected Then
s += cb.Text & separator
end If
Next
s = s.Substring(0, s.Length - 1)
Return s
End Function

Resources