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>
Related
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
I have a dynamic table being build on page_load.
I put the table inside an update panel so that I could add rows dynamically without a full page postback. Each row has 4 cells first 3 containing textboxes and the 4th cell being an image button that needs to delete the row.
However the image button to remove the row is posting back but not firing the event handler sub.
I set the table to a session variable for reloading on postbacks.
<%--Update Panel with Image Button to add row--%>
<asp:UpdatePanel ID="pnlHA" runat="server" UpdateMode="conditional" >
<ContentTemplate>
<asp:ImageButton ID="imgAddHA" AlternateText="Add Row" ImageUrl="../images/plus_orange.png" style="width:17px; height:17px; cursor: hand;" runat="server" />
<div style="height:100px; overflow:scroll; overflow-x:hidden;">
<span runat="server" id="spanTBL_HA" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
on page.init if the pnl page is in postback then it recreates the span
If pnlHA.Page.IsPostBack Then
spanTBL_HA.Controls.Clear()
spanTBL_HA.Controls.Add(CType(Session("tblHA"), Table))
End If
Image button being added to table cell
img = New ImageButton
Session("tblHA_Counter") = CInt(Session("tblHA_Counter")) + 1
img.AlternateText = "Delete Row"
img.Attributes.Add("style", "height: 10px; width: 10px; cursor: hand;")
img.ImageUrl = "../images/minus_red.png"
img.ID = "img_" & CInt(Session("tblHA_Counter")).ToString
AddHandler img.Click, AddressOf imgRemove_Click
img.Attributes.Add("runat", "server")
tc.Attributes.Add("style", "width:35px")
tc.Controls.Add(img)
Click Event
Protected Sub imgRemove_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
Dim img As ImageButton = CType(sender, ImageButton)
Dim delRow As TableRow = Nothing
Dim rowIndex As Integer = 0
For Each row As TableRow In CType(Session("tblHA"), Table).Rows
If CType(row.Cells(3).Controls(0), ImageButton).UniqueID = img.UniqueID Then
delRow = row
End If
Next
If delRow IsNot Nothing Then
CType(Session("tblHA"), Table).Rows.Remove(delRow)
End If
spanTBL_HA.Controls.Clear()
spanTBL_HA.Controls.Add(CType(Session("tblHA"), Table))
End Sub
Dynamic controls need to be recreated every time the page is created to allow ASP.NET to restore view state, postback data and event wireup to the dynamically created controls. So remember to recreate them on the postback as well.
Please check whether you are creating them on every post back.
How to get the textbox value on code-behind after the textbox value assigned on a modal-box?
This is how the modal-box appear on the page:
<div id="dialog-form" title="Modal Box">
<asp:TextBox ID="Textbox1" runat="server" ReadOnly="True">
<asp:Button ID="Save_Button" runat="server" Text="Save"></asp:Button>
</div>
In order to assign the value on Textbox1, I attach a Javascript function to the LinkButton on a Gridview (the code not showed here) through code-behind based on the LinkButton clicked:
Dim myLinkButton As LinkButton
For i As Integer = 0 To GV1.Rows.Count - 1
myLinkButton = DirectCast(GV1.Rows(i).Cells(1).FindControl("LinkButton"), LinkButton)
myLinkButton.Attributes.Add("onclick", "shopModalPopup('" + .Rows(i).Cells(0).Text & "'); return false;")
Next
Rows(i).Cells(0) is the first column on the Gridview, it is "ID". This ID will be assigned to the Textbox1 while the Linkbutton clicked.
The Javascript code is on the same page as the Gridview code:
<script>
var grid_modal_options = {
height: 450,
width: 550,
modal: true
};
function shopModalPopup(id) {
var DataField = id;
grid_modal_options.open = function () {
$('#dialog-form #Textbox1').val(DataField);
};
$("#dialog-form").dialog(grid_modal_options);
$("#dialog-form").parent().appendTo('form:first');
}
</script>
When I clicked the Save Button on the modal-box, the value on the Textbox1 can't catch on the code-behind. It always return null value. How to do that? Thank you very much.
please read this
http://www.codeproject.com/Articles/37090/JQuery-UI-Dialog-with-ASP-NET-empty-post-values
i think it might help you the problem might be that the div of dialog is moved outside the form you can solve it by using reference above or just defining another hidden input and update the hidden input with the same value
$('#dialog-form #Textbox1').val(DataField);
$('hiddenfield').val(DataField);
and then read the value from the hidden field
not sure if it the best answer but might work as a quick fix
Does it work without Readonly="true"? If so, in your <asp:textbox>, replace Readonly="true" with Enabled="false".
I haven't tested this, but i recalled Readonly causing me issues like this in the past.
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
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.