Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Check for listbox and checkbox before button click

Author  Topic 

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-06-13 : 10:45:09
Hi friends

Iam trying for this for 2 days
With javascripy i am selecting and deselecting a listbox
Suppose the listbox is selected fully, with checkbox check
I have a button, on clciking that i am refreshing the page and populating another listbox2 with items which corresponds to listbox1 selected items

But what happnes is when i hit the button, on page refreshing, the listbox1 and checkbox getting deselected

So i want to reselect the items of listbox1 before i bind the listbox2

I am so confused to do how?
I googled a lot, but didnt get enough tips
Please help me to figure out this

Thanks a lot

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-06-13 : 10:58:28
save the checked checkbox names or ids in a hidden field.
when you're reloading the page check those checkboxes that are submitted rhrough the hidden field.

another way is to use ajax and not having to refresh
the whole page at all.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

cvraghu
Posting Yak Master

187 Posts

Posted - 2007-06-13 : 14:03:50
If its an option, you can consider using asp.net check box server control, which will retain the state for you during page refresh. During page load you've check for its value and load the list1. Look into BOL for viewstate. You can also use client side code against these web server control.
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-06-14 : 10:49:50
Thanks for the reply to both
I am using server controls only
I enabled viewstate to true
What i have now is
 function onChange(checkBoxRef,listboxId)
{

var listboxRef = document.getElementById(listboxId)

if(checkBoxRef.checked)

{

for (var i=0;i<listboxRef.options.length;i++)


{

listboxRef.options[i].selected = true;

}
}
else

{

for (var i=0; i<listboxRef.options.length; i++)

{

listboxRef.options[i].selected = false;

}
}
}

function removeCheck(checkboxId)
{
document.getElementById(checkboxId).checked = false;
}

And in code behind
  lstAccounts.Attributes.Add("onChange", "Javascript:removeCheck('" + chkAll.ClientID + "');")
chkAll.Attributes.Add("onClick", "javascript:onChange(this, '" + lstAccounts.ClientID + "');")


This code works fine.
Here suppose the listbox items selected and checkbox is checked,
I have another button clcik event
 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
DataBindListbox2()

End Sub


When i hit this button, all the selection of lstacounts is gone and also the checkbox check
How can i reselect the lstaccounts and checkbox, coz Databindlistbox2 is based on lstaccounts selected items

I tried a lot, but not able to figure out
Please help me

Thanks
Go to Top of Page

cvraghu
Posting Yak Master

187 Posts

Posted - 2007-06-14 : 13:07:34
Did you find out why the check box is not retaining its state? Have you enabled viewstate for both the page and control? Also you can have an alert in removecheck function and make sure that it is not called unwantedly.
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-06-14 : 13:33:40
Thanks for the response
I have enabled viewstate true to page and controls
Also i checked for an alert in removecheck function.
It is calling correct only

Any ideas how can i solve this
I am in a situation, i cannot proceed further without clearing this
Please help me
Thanks
Go to Top of Page

cvraghu
Posting Yak Master

187 Posts

Posted - 2007-06-14 : 15:20:05
Please post the code for page load, and actions events if any.
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-06-15 : 10:02:37
Thanks for the reply
Page load code is below
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

lstAccounts.Attributes.Add("onChange", "Javascript:removeCheck('" + chkAll.ClientID + "');")
chkAll.Attributes.Add("onClick", "javascript:onChange(this, '" + lstAccounts.ClientID + "');")

If Not IsPostBack Then
DataBindList1()
Else
End If

End Sub

Public Sub DataBindList1()
--- binding lstaccounts
end sub



The remaining code which is for lstaccounts selection with javascript is as in the above post
I am not able to understand, what is the reson, how to solve this
Please help me
Thanks
Go to Top of Page
   

- Advertisement -