Hi & thanks in advance,
New to the Visual C# and ASP route and trying to create a shopping list: add, removing (either all or one at a time), works but I've come undone with the text decoration depending on a check box.
I'm trying to add some text decorations to the checkboxlist items' text depending on condition, in this case, whether or not a check box is 'checked'.
Below is my code, however I'm getting an error where the TextDecorations has no definition.
According to MSDN it comes from the System.Windows namespace, which is defined at the top of my code.
Any suggestions or help would be greatly appreciated.
Below is my function which I am trying to get to run through all of the list items and if the checkbox is checked then strikethrough the text,
protected void Finished_Click(object sender, EventArgs e)
{
foreach(ListItem li in TaskList.Items)
{
if(li.Selected)
{
li.TextDecorations = TextDecorations.Strikethrough;
}
}
}
Define css styles as per example and then set required class to the li-item
if(li.Selected)
{
li.CssClass = "dupa";
}
More here Striking through a number in an ordered list CSS/HTML
Related
I have a site that is using the aspdotnetstorefront platform, although this should pertain to any C# site. There is a custom control in a dll named Account. This has several elements including text boxes for customers to enter name, phone number etc.
I have checked the source code with DotPeek and verified I am using the correct naming conventions.
I am attempting to use the javascript onChange event to copy the first name, last name and phone number to lower boxes when a check box (account information same as billing) is checked. So that if customers select that the information is the same it will be automatically copied as they move from one box to the next.
The odd thing is, this works with some of the text boxes but not others. To make things simple I have removed the JS that copies the contents and replaced it with a pop up box for testing.
This works, when I change the text I get a "Hello World" pop up box:
ctrlAccount.txtFirstName.Attributes.Add("onchange", "alert('hello,world')");
But this does not:
ctrlAccount.txtPhone.Attributes.Add("onchange", "alert('hello,world')");
The error I get is:
CS1061: 'AspDotNetStorefrontControls.Account' does not contain a definition for 'txtPhone' and no extension method 'txtPhone'
accepting a first argument of type
'AspDotNetStorefrontControls.Account' could be found
So it looks like the compiler cannot recognize the phone text box. When I look at the rendered page code (When the error has been removed of course) the box is there and the ID is correct.
Reading the source code with DotPeek I see:
public Account()
{
this._txtFirstName = new TextBox();
this._txtLastName = new TextBox();
this._txtEmail = new TextBox();
this._txtPassword = new TextBox();
this._txtPasswordConfirm = new TextBox();
this._txtPhone = new TextBox();
}
private void AssignClientReferenceID()
{
this._txtFirstName.ID = "txtFirstName";
this._txtLastName.ID = "txtLastName";
this._txtEmail.ID = "txtEmail";
this._txtPassword.ID = "txtPassword";
this._txtPasswordConfirm.ID = "txtPasswordConfirm";
this._txtPhone.ID = "txtPhone";
}
(I've removed a bunch of other fields in the interest of readability). But that certainly looks to me like text box for the phone number should have the idea of "txtPhone" and "txtFirstName" and "txtLastName" work just fine, so why would this fail on only the Phone box?
The code you've added to the question show only assignment of IDs and nothing else. I can't see the entire source code of Account control but usually fields with leading underscore are protected or private and, to expose these fields, public properties are used so please check if it's present a public property named txtPhone.
EDIT
ctrlAccount.txtFirstName.Attributes.Add("onchange", "alert('hello,world')");
ctrlAccount.txtPhone.Attributes.Add("onchange", "alert('hello,world')");
Try to press F12 with the caret over txtFirstName or txtPhone on the lines above to see where these property are defined.
P.S.:
Do not confuse the names of id and public properties of a class
It's VS bug, simple restart should help you.
Right-click the txtFirstName part and choose "Go to definition". Ensure that the txtPhone property is defined nearby and is accessible.
If you open your project on visual studio, you can right click on txtPhone or txtFirstName and click on "Go to definition". It will take you to the place where it is defined. Maybe txtFirstName is being read from somewhere else where txtPhone does't exist.
I get these kind of warning(not an error) on my application sometimes in compiler however on run time, it recognise the definition and no problem is caused.
I have got an ASP-Site, which enables the user to Add Label-Elements. I don’t know how many Labels where added or which ID they have. I know only, they will be within the Panel pnl_Added. After the user has added all his labels, he pushes a Send-Button for Update.
So, now I am at my Server, awaiting this postback, but I don’t know where, when and how to find out, which Elements were Added to pnl_Added. Can somebody help me?
I have tried something like that:
protected void Page_Load(object sender, EventArgs e)
{
[...]
for (int i = 0; i < pnl_Added.Controls.Count; i++)
{
[...]
}
[...]
}
But I think it is too late because of the loaded ViewState? Is that possible?
I am working with VS 2013, ASP c#, with the .Net Framework 4.
On server, controls tree doesn't created from actual client HTML. Actually, server doesn't know anything about client HTML besides input tags values in scope of submitted form. In general, all controls available in Page_Load method, created on server side from aspx file markup.
To implement your scenario, you need to add hidden field for each label, added from client and save label's inner text into hidden field's value. Then you'll can get these labels texts as below:
var labels = Request.Form["hiddenField's name"] as string[];
You should go one lever deeper and take the added elements from Request variable, because the control pnl_Added doesn't know about them as there was no postback.
Something like this:
Request.Form["field_id"]
I suggest to run the page in debug mode, review Request.Form collection and find what you need. You should see your label elements there.
I am using the following code to update a listbox, this recieving a list from a Web service:
client.userKeywordsCompleted += new EventHandler<userKeywordsCompletedEventArgs>(client_userKeywordsCompleted);
client.userKeywordsAsync();
Using:
void client_userKeywordsCompleted(object sender, userKeywordsCompletedEventArgs e)
{
string result = System.Convert.ToString(e.Result);
for (int i = 0; i < e.Result.Count; i++)
{
ListBoxItem lbitem = new ListBoxItem();
lbitem.Name = "lb_" + i;
lbitem.Content = e.Result[i];
lbitem.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(ListBoxItem_DoubleClickEvent), true);
listBox1.Items.Add(lbitem);
}
This works fine, as I use it when the Child window loads, so the ListBox gets the list from the database, however, when a user selects one of the items in the ListBox, they have the option to edit the selected item. So once the edit is in place, there is an edit button, which updates the column in the table in the database. So then on the button click, I am again calling the aforementioned code to update the ListBox with the new credentials. However, this brings back the error:
"Value does not fall within the expected range."
Why can I not call the Web method on the button click, as all it is doing is refreshing the ListBox?
This might be due to the fact that you are trying to add a ListBoxItem with the same name to the page.
If you want to refresh the content of the listbox with the newly retrieved values you will have to first manually remove the content of the listbox other wise your loop will try to create lb_1 again and add it to the same list.
Look here for a similar problem that occurred Silverlight: Value does not fall within the expected range exception
I had from a totaly different reason the same notice "Value does not fall within the expected range" from the Visual studio 2008 while trying to use the:
Tools -> Windows Embedded Silverlight Tools -> Update Silverlight For Windows Embedded Project.
After spending many ohurs I found out that the problem was that there wasn't a resource file and the update tool looks for the .RC file
Therefor the solution is to add to the resource folder a .RC file and than it works perfectly.
I hope it will help someone out there
In case of WSS 3.0 recently I experienced same issue. It was because of column that was accessed from code was not present in the wss list.
I want to add check box inside comboBox in C#. My purpose is that the user can select multiple values from one ComboBox ( Check all and Uncheck all ).
Please Help
You have to extend the ComboBox control by providing your own rendering strategy, and "manually" adding a CheckBox.
Theses open source project are ready to use :
http://www.codeproject.com/KB/combobox/CheckComboBox.aspx
http://www.codeproject.com/KB/combobox/extending_combobox.aspx
It is a wrong usage of a ComboBox control, because the user has no possibility to see his choices. For multiple selection, I recommend you to consider this CheckedListBox control:
link to MSDN
There is an ASP.NET open source control at http://dropdowncheckboxes.codeplex.com/ that I've used and been very happy with. There is also a WinForms open source control at http://www.codeproject.com/KB/combobox/extending_combobox.aspx that doesn't look quite as strong but maybe somebody could combine the best of both. If well implemented this is really a great addition to your toolkit. The above 2 implementations show all of the items selected and give you a number of related checkboxes in a reduced area and with excellent grouping. My addition to the ASP.NET version was to allow a list of checked files to use just file names instead of full paths if this gets too long. See above link for full code. Below is just my addition which is called instead of UpdateSelection in your postback handler:
// Update the caption assuming that the items are files
// If the caption is too long, eliminate paths from file names
public void UpdateSelectionFiles(int maxChars) {
StringBuilder full = new StringBuilder();
StringBuilder shorter = new StringBuilder();
foreach (ListItem item in Items) {
if (item.Selected) {
full.AppendFormat("{0}; ", item.Text);
shorter.AppendFormat("{0}; ", new FileInfo(item.Text).Name);
}
}
if (full.Length == 0) Texts.SelectBoxCaption = "Select...";
else if (full.Length <= maxChars) Texts.SelectBoxCaption = full.ToString();
else Texts.SelectBoxCaption = shorter.ToString();
}
How do I get the id's of all the checkboxes generated by a checkboxlist, with datatable as its data source?
I think I have to use the "OnDataBinding" event of the checkbox list, but I don't see how that will help me.
I am using C#
I don't think getting the id's of all the check boxes generated by the check box list is possible, so I think going the moo tools way is the right thing to do.
Any ideas?
Thanks
Ideally you would want to just attach the click event handlers to all your checkbox lists in the domReady event and this will create a much simpler function with MooTools. However, you can keep your code as-is if you prefer and just make your 2 functions a little simpler.
function ToggleSelection(ctrl, sender) {
var checkboxes = $(ctrl).getElements('input[type=checkbox]');
checkboxes.set('checked', sender.checked);
}
function ToggleSelectAll(ctrl, sender) {
var fAllChecked = ($(sender).getElements('input:checked').length == $(sender).getElements('input[type=checkbox]').length)
$(ctrl).set('checked', fAllChecked);
}
You can set the properties of your entire ELements array at once, you don't need to loop through them. In the 2nd function, I'm checking the number of elements that are checked against the total number of checkboxes and if they match that means they are all checked.
Are you using VB or C#..please tag accordingly
You can have List or string called strchklist
VB.NET
For Each li In CheckBoxList1.Items
If li.Selected Then
strchklist += li.Id
End If
Next
C#
foreach (ListItem li in CheckBoxList1.Items){
If li.Selected
strchklist += li.Id ;}
The <asp:ListItem> is not really a Control itself therefore has no ID. If you want to access it in client script add a new attribute you can reference. (yes, during OnDataBinding) Remember that these are not persisted in the ViewState however!
What exactly are you trying to accomplish? May help to elaborate somewhat.
Completely different answer...
The CheckBoxList is a bit of an odd duck... unlike other controls, it does not really have a logical mapping to an obvious HTML construct. It actually renders multiple checkboxes with derivative IDs. Those IDs seem to be generated as CheckBoxList.ClientID + "_" + ItemIndex.
You can verify this by looking at the page source. Internally, it seems the ID of the individual checkbox control is just its index, and then it's rendered with the CheckBoxList as its NamingContainer. You can use Reflector is see how the CheckBoxList control renders the output.
Still a good spot for jQuery. Just easier now you know the IDs.
I woke up this morning and thought of doing this (cblUSEquities is a checkbox list)
cblUSEquities.Attributes.Add("onclick", "javascript:alert('Clicked');");
This adds the alert to the table that the checkbox list generates. One thing to note is, the alert shows up twice If I click the text of the checkbox and once if I click the checkbox. I think this solution will work for me.
BTW, I never thought that the above code would work...
P.S Answering my own question because I wanted to write some code, which I cannot do using the comment box.
In the spirit of StackOverFlow, I arrived to something which works in my scenario but the description of the question is different? What do I do? Edit the question and mark this as answer?
I was able to do this by using mootools and this is the code.
function ToggleSelection(ctrl, sender)
{
var cblCtrl = $(ctrl);
var Allcbs = cblCtrl.getElements('input');
for(var i=0; i<Allcbs.length; i++)
Allcbs[i].checked = sender.checked;
}
function ToggleSelectAll(ctrl, sender)
{
var AllTrueCount = 0;
var cblCtrl = $(ctrl);
var Allcbs = sender.getElements('input');
for(var i=0; i<Allcbs.length; i++)
if(Allcbs[i].checked)
AllTrueCount++;
if(AllTrueCount == Allcbs.length)
cblCtrl.checked = true;
else
cblCtrl.checked = false;
}
C# code which calls the javascript functions
//Binding event to the checkbox list
cblUSEquities.Attributes.Add("onclick", string.Format("javascript:ToggleSelectAll('{0}', this);", chkAllUSEquities.ClientID));
//binding event to the select all checkbox
chkAllUSEquities.Attributes.Add("onclick", string.Format("javascript:ToggleSelection('{0}', this);", cblUSEquities.ClientID));
As it turns out I did not need to know the id's of all the checkboxes generated by the checkbox list. I was able to add the onclick javascript to those checkboxes by this line
cblUSEquities.Attributes.Add("onclick", string.Format("javascript:ToggleSelectAll('{0}', this);", chkAllUSEquities.ClientID));
Which will add the onclick event to the table that the checkbox list generates.