CheckBox inside ListView control using ASP.Net 3.5 - c#

When CheckBox is unchecked in a ListView i need to get a popup window?

I have make a JS function and just pass id of your list like as
OnClientClick="return GetSelectedCheckBoxInGrid('grdCustomer');"
function GetSelectedCheckBoxInGrid(obj)
{
var con = 'ctl00_ContentPlaceHolder1_' + obj
var Parent = document.getElementById(con);
var TargetChildControl = "chk";
if (Parent==null)
{
return false;
}
var items = Parent.getElementsByTagName("input");
for(var n = 0; n < items.length; ++n)
if(items[n].type == 'checkbox' &&
items[n].id.indexOf(TargetChildControl,0) >= 0 &&
items[n].checked == false)
alert('Hi');return false;)
}
I think this is that

Not too sure about this, but hypothetically, you could give each checkbox a class, eg chkbox, and then have some jquery code to handle a click event:
$('chkbox').click(function() {
alert("here is where you put your popup code");
});
You could use window.open here

$('chkbox').click(function() {
if (! $('#chkbox').is(':checked'))
{
window.open ("http://www.javascript-coder.com","mywindow","status=1");
}
});
or
$('chkbox').click(function() {
if(! $('#chkbox').attr('checked'))
{
window.open ("http://www.javascript-coder.com","mywindow","status=1");
}
});
How to check whether a checkbox is checked in jQuery?

Related

Variable in C# for loop

I have got many buttons in my Application Form. And I would like to check every buttons text (compare). How can I achive that ?
for (i = 1; i < 30; i++)
{
if (this.button1.Text == "Hello") //here is PROBLEM
{
//..some statement
}
}
So next time this.button1.Text must change to this.button2.Text and so on...
this.button[i].Text not working.
Buttons are not arrays. Each one is a discreet object, and a child of its container.
Ideally, you need to build a collection (array, list, whatever) of the buttons and iterate through that collection, rather than using an index variable (i).
Here's a good approach: https://stackoverflow.com/a/3426721/820068
This is a correct syntax:
foreach (Control button in this.Controls)
{
if (button.GetType() == typeof(Button) && button.Text == "Hello")
{
//..some statement
}
}
I'm quite sure that this is a windows form.
And in windows form you can iterate the controls like this.
foreach (Control c in panel.Controls)
{
string cType = c.GetType().ToString();
// check all buttons
if (cType == "System.Web.UI.WebControls.Button")
{
if(((Button)c).Text == "Hello")
{
}
}
}
So what the code does is to iterate all the controls inside a panel and check each control if it's type is a button.
Update:
As Wesley said, much better approach for the condition is to implement it like this
if (c is Button && c.Text.Equals("Hello")) {
for (int i = 1; i < 3; i++)
{
var buttonName = "button" + i;
Button button = this.Controls.Find(buttonName, true).FirstOrDefault() as Button;
string text = button.Text;
}
try this code.

Validate Gridview CheckBox Checked for Multiple Gridviews

I want to validate Gridview CheckBox Checked for Multiple Gridviews on the same page
I have tried the following but it is not working.
<script type="text/javascript">
var TargetBaseControl = null;
window.onload = function () {
try {
//get target base control.
TargetBaseControl = document.getElementById('<%= this.GridView1.ClientID%>', '<%= this.GridView2.ClientID%>');
}
catch (err) {
TargetBaseControl = null;
}
}
function TestCheckBox() {
if (TargetBaseControl == null) return false;
//get target child control.
var TargetChildControl = "chkSelectAdd";
//get all the control of the type INPUT in the base control.
var Inputs = TargetBaseControl.getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
if (Inputs[n].type == 'checkbox' &&
Inputs[n].id.indexOf(TargetChildControl, 0) >= 0 &&
Inputs[n].checked)
return true;
alert('Select at least one checkbox!');
return false;
}
</script>
There are two problems that I can see here. document.getElementById only supports one element at a time, so your code will only check the first GridView's checkboxes. Also, an element's ID ought to be unique and so your check against the ID here:
Inputs[n].id.indexOf(TargetChildControl, 0) >= 0 &&
will only return true for the first checkbox.
I'd suggest removing the use of TargetBaseControl and simply relying on the names, rather than IDs, of the checkboxes instead:
var Inputs = document.getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
if (Inputs[n].type == 'checkbox' &&
Inputs[n].name.indexOf(TargetChildControl, 0) >= 0 &&
Inputs[n].checked)
return true;
...

How to handle the mouse wheel button event in ASP.NET?

I have a project which uses EXT.NET framework for the controls. I'm currently working on the behavior of closing the panel tabs such as Google Chrome and all the modern browsers does.
I couldn't find the answer to this. What is the ASCII value for the mouse scroll wheel button? How can I handle this event in C# ASP.NET?
working on Firefox, Explorer and Chrome:
$(document).ready(function() {
$(document).mousedown(function(e) {
closeTab(e);
});
});
function closeTab(e) {
if (!e) {
e = window.event;
e.which = e.keyCode;
}
if(e.which == 2){
var tbpPrincipal = <%= tbpPrincipal.ClientID %>;
var activeTab = null;
for (var i = 0; i < tbpPrincipal.items.length; i++) {
var currentTab = tbpPrincipal.items.items[i];
if (e.target.innerText == currentTab.title || e.target.textContent == currentTab.title) {
activeTab = currentTab;
break;
}
}
if (activeTab) {
var activeTabIndex = tbpPrincipal.items.findIndex('id', activeTab.id);
tbpPrincipal.remove(activeTabIndex);
}
}
return true;// to allow the browser to know that we handled it.
}

why listbox selected index changed is not firing?

I have written a javascript method to sortlistbox items and it works well in a sense that the item that I type in text box gets highlighted.
But when I click on the highlighted item it dosen't gets selected. Why?
The selectedIndexchanged is not working.
Here is my javascript code:
function SearchList() {
var l = document.getElementById("<%= LBox.ClientID %>");
var tb = document.getElementById("<%= txtDepartments.ClientID %>");
if (tb.value == "") {
ClearSelection(l);
}
else {
for (var i = 0; i < l.options.length; i++) {
if (l.options[i].value.toLowerCase().match(tb.value.toLowerCase())) {
l.options[i].selected = true;
return false;
}
else {
ClearSelection(l);
}
}
}
}
function ClearSelection(l) {
l.selectedIndex = -1;
}
Do this, then call your function inside, you need to reference jquery library :
$(document).ready(function() {
$('#' + '<%= DropdownName.ClientID %>').change(function() {
// Call function here...
});
});

how to check status of checkboxes in gridview columns on click of button

I have used checkbox column in gridview. I want to check status of that checkboxes. On click of a button it should be checked that if any checkbox is checked or not. If none checkbox is checked then it should display alert message that check checkbox first.
Hey, I found answer. It is as follows:
function checkBoxselectedornot()
{
var frm=document.forms['aspnetForm'];
var flag=false;
for(var i=0;i<document.forms[0].length;i++)
{
if(document.forms[0].elements[i].id.indexOf('chkDownloadSelectedEvent')!=-1)
{
if(document.forms[0].elements[i].checked)
{
flag=true
}
}
}
if (flag==true)
{
return true
}else
{
alert('Please select at least one Event.')
return false
}
}
protected void OnCheckedChanged(object sender, EventArgs e)
{
bool flag = false;
foreach (GridViewRow row in Grid_InvoiceGarden.Rows)
{
CheckBox chkItem = (CheckBox)row.FindControl("chkSelect");
if (chkItem.Checked)
flag = true;
}
if (flag == true)
{
btnUpdate.Visible = true;
}
else
{
btnUpdate.Visible = false;
}
}
if(document.getElementById('checkBoxId').checked) {
//checked
} else {
//not checked
}
edit: if you want to check all checkboxes of a form you can loop through the collection:
var inputs = document.getElementById('formId').getElementsByTagName('input');
var isChecked = false
for( var i = 0; i < inputs.length; i++) {
if(inputs[i].type == 'checkbox' && inputs[i].checked) {
isChecked = true;
}
}
if(isChecked) {
//at least one checkbox checked
}
Server side:
//in your button click event :
bool flag = false;
for( int i=0; i < gridview1.rows.count ; i++)
{
if(checkbox1.checked)
flag = true;
}
if(flag)
{
//means atleast one check box is checked
}
You will have to add some custom Javascript to your page for the client-side alert to show. Here's a method that I've written that works with a GridView called 'GridView1' (this should be the default name if you've just dragged the control onto your ASPX page):
<script type="text/javascript">
function ClientCheck() {
var valid = false;
var gv = document.getElementById("GridView1");
for (var i = 0; i < gv.all.length; i++) {
var node = gv.all[i];
if (node != null && node.type == "checkbox" && node.checked) {
valid = true;
break;
}
}
if (!valid) {
alert("Invalid. Please select a checkbox to continue.");
}
return valid;
}
</script>
You can see that it sets a variable to the GridView control to start with, then iterates through all the children in a for loop. If the child is a checkbox and it is checked, then we set the valid variable to true. If we get to the end of the iteration and no checked checkboxes are found, then valid remains false and we execute the alert.
To link this into your GridView on your ASPX page, first make the button column a TemplateField and surround the LinkButton with your client-side code. If you've used the designer to set up your columns, you can use the "Convert this field into a TemplateField" link in the column editor). Here's an example of the source you'll end up with:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
<Columns>
<asp:TemplateField HeaderText="Button Field" ShowHeader="False">
<ItemTemplate>
<span onclick="return ClientCheck();">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="IDClick" Text='<%# Eval("YourDataSourceItem") %>'></asp:LinkButton>
</span>
</ItemTemplate>
</asp:TemplateField>
// ...your remaining columns...
Using the TemplateField lets us add any client-side code we like. Here we're adding a span and using onclick to call our ClientCheck method.
If you aren't bothered about the alert, you could achieve your aims by using a CustomValidator control, which executes on the server-side.
I hope this helps.
<script type="text/javascript" language="javascript">
function CheckboxSelect() {
var LIntCtr;
var LIntSelectedCheckBoxes = 0;
for (LIntCtr = 0; LIntCtr < document.forms[0].elements.length; LIntCtr++) {
if ((document.forms[0].elements[LIntCtr].type == 'checkbox') && (document.forms[0].elements[LIntCtr].name.indexOf('chkID') > -1)) {
if (document.forms[0].elements[LIntCtr].checked == true) {
LIntSelectedCheckBoxes = parseInt(LIntSelectedCheckBoxes) + 1;
}
}
}
if (parseInt(LIntSelectedCheckBoxes) == 0) {
alert('User(s) Must Be Selected For operation !');
return false;
}
}
</script>

Categories