友好连接

2009年4月28日星期二

ASP.NET C# Disable/Enable ListItems

A Brief Introduction
Have you ever wanted to disable/enable individual list items in a ListControl Server Control in ASP.NET? You might have tried to add attributes to the individual ListItems in a Server Control such as a CheckBoxList or DropDownList? It's impossible and a known bug. I needed to implement a way to disable/enable individual items of a CheckBoxList when a certain item was checked/unchecked. The problem was solved by adding a JavaScript function that is invoked by the onclick event of the CheckBoxList.

Instructions
Add a CheckBoxList to your ASP.NET WebForm and give it an ID of checkBoxListTest.
Add a call to the LoadCheckBoxList in the Page_Load event.
Add the LoadCheckBoxList method to your webpage class.
Add the JavaScript function inside the head of the HTML.
ASP.NET CodeBehind
The following method adds the onclick function disableListItems to the CheckBoxList attributes collection. The function will disable all the items except for the last item in the list when it is checked. The method also adds three items to the CheckBoxList.

private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
LoadCheckBoxList();
}
}

private void LoadCheckBoxList()
{
this.checkBoxListTest.Attributes.Add("onclick",
"disableListItems('checkBoxListTest', '2', '3')");

// Add three items to the CheckBoxList.
for(int i=0; i < 3; i++)
{
ListItem item = new ListItem(i.ToString(), i.ToString());
this.checkBoxListTest.Items.Add(item);
}
}
The following code is the JavaScript function that is invoked by the onclick event of the CheckBoxList.

JavaScript Function
/*******************************************************************
This is the javascript function that is invoked when the checkboxlist
is clicked.

Function: disableListItems.
Inputs: checkBoxListId - The id of the checkbox list.

checkBoxIndex - The index of the checkbox to verify.
i.e If the 4th checkbox is clicked and
you want the other checkboxes to be
disabled the index would be 3.

numOfItems - The number of checkboxes in the list.
Purpose: Disables all the checkboxes when the checkbox to verify is
checked. The checkbox to verify is never disabled.
********************************************************************/
function disableListItems(checkBoxListId, checkBoxIndex, numOfItems)
{
// Get the checkboxlist object.
objCtrl = document.getElementById(checkBoxListId);

// Does the checkboxlist not exist?
if(objCtrl == null)
{
return;
}

var i = 0;
var objItem = null;
// Get the checkbox to verify.
var objItemChecked =
document.getElementById(checkBoxListId + '_' + checkBoxIndex);

// Does the individual checkbox exist?
if(objItemChecked == null)
{
return;
}

// Is the checkbox to verify checked?
var isChecked = objItemChecked.checked;

// Loop through the checkboxes in the list.
for(i = 0; i < numOfItems; i++)
{
objItem = document.getElementById(checkBoxListId + '_' + i);

if(objItem == null)
{
continue;
}

// If i does not equal the checkbox that is never to be disabled.
if(i != checkBoxIndex)
{
// Disable/Enable the checkbox.
objItem.disabled = isChecked;
// Should the checkbox be disabled?
if(isChecked)
{
// Uncheck the checkbox.
objItem.checked = false;
}
}
}
}
Conclusion
The above code shows that it is possible to enable/disable individual items within a ListBox Server Control in ASP.NET. You can modify the C# and JavaScript code to meet your needs.


License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

How to group RadioButtons

Introduction to the problem
'Where is the problem? Haven't you heard about GroupName property?' - you can ask me. Of course, you are right! But...
Let's take an ordinary DataGrid, add a TemplateColumn to its Columns collection, and place a RadioButton control within this column (it can be useful when you would like to provide the user with selection from the DataGrid items). See the code below:< !-- Countries for selection -->
< asp:DataGrid id="countriesGrid" runat="server"
DataKeyField="ID" AutoGenerateColumns="False">
< columns>
< asp:templatecolumn>
< itemtemplate>
< !--
Draw attention at this control.
We would like to use radio-buttons to
select single country from the list.
-->
< asp:RadioButton id="selectRadioButton"
runat="server" GroupName="country" />
< /itemtemplate>
< /asp:TemplateColumn>
< asp:BoundColumn DataField="Country" HeaderText="Country"
HeaderStyle-Font-Bold="True" />
< asp:BoundColumn DataField="Capital" HeaderText="Capital"
HeaderStyle-Font-Bold="True" />
< /columns>
< /asp:DataGrid>
Now, bind to the DataGrid some data and run your ASP.NET application. Try to click at the radio buttons in the Countries list. You can select one country!... and another one... and another... Hmm-m! Didn't we really want to get this effect?
Where is a mistake? We have specified GroupName for the RadioButtons to treat them as from the single group, haven't we? Look at the piece of HTML code that has been generated from our web form. You will see something like this:< !-- Countries for selection -->
< table cellspacing="0" rules="all" border="1" id="countriesGrid"
style="border-collapse:collapse;">
< tr>
< td> < /td>
< td style="font-weight:bold;">Country< /td>
< td style="font-weight:bold;">Capital< /td>
< /tr>
< tr>
< td>< input id="countriesGrid__ctl2_selectRadioButton"
type="radio" name="countriesGrid:_ctl2:country"
value="selectRadioButton" />< /td>
< td>USA< /td>
< td>Washington< /td>
< /tr>
< tr>
< td>< input id="countriesGrid__ctl3_selectRadioButton"
type="radio" name="countriesGrid:_ctl3:country"
value="selectRadioButton" />< /td>
< td>Canada< /td>
< td>Ottawa< /td>
< /tr>
< !-- etc. -->
The 'name' attributes of the radio-buttons are different. Why? Here is the answer.
When rendering RadioButton control, ASP.NET uses concatenation of GroupName and UniqueID for the value of 'name' attribute. So, this attribute depends on the UniqueID of the control which depends on the owner's UniqueID etc. It is the standard solution of ASP.NET to avoid naming collisions. As the value of the 'name' attribute of the < input type="radio"> is used for identification of postback data of the radio-button group when the from is submitting, ASP.NET developers decided to isolate radio-button groups within the bounds of the single owner control (i.e., any two radio-buttons from the same group can not have different direct owners), otherwise it can occur that you will use two third party controls that both contain radio-button groups with the same GroupName - in this case, all radio-buttons will be treated as from the single group and that will bring undesirable behavior.
Now you have understood the cause of error, but how to implement the feature we want? In the next section, I'll provide you the solution.
Solution of the problem
To solve the problem I have stated above, I've created a new GroupRadioButton web control derived from the RadioButton.
In this control, I have changed the rendering method so that 'name' attribute of the resulting HTML radio-button now depends on the GroupName only.
Another one modification is postback data handling (IPostBackDataHandler interface has been overridden).
Other functionality of the GroupRadioButton is equivalent to RadioButton.
See the source code of the GroupRadioButton for details.
Using the code
Now, let's modify the initial form. Use the following script for the Countries list:< %@ Register TagPrefix="vs" Namespace="Vladsm.Web.UI.WebControls"
Assembly="GroupRadioButton" %>
...
< !-- Countries for selection -->
< asp:DataGrid id="countriesGrid" runat="server" DataKeyField="ID"
AutoGenerateColumns="False">
< columns>
< asp:templatecolumn>
< itemtemplate>
< vs:GroupRadioButton id="selectRadioButton"
runat="server" GroupName="country" />
< /itemtemplate>
< /asp:TemplateColumn>
< asp:BoundColumn DataField="Country" HeaderText="Country"
HeaderStyle-Font-Bold="True" />
< asp:BoundColumn DataField="Capital" HeaderText="Capital"
HeaderStyle-Font-Bold="True" />
< /columns>
< /asp:DataGrid>
Add reference to the GroupRadioButton assembly, bind data for the countriesGrid, and execute this form. You will find that all radio-buttons are in the single group (i.e., user can check only one of them).
It remained only to show how to determine which of the countries have been selected:using Vladsm.Web.UI.WebControls;
...
private void selectButton_Click(object sender, System.EventArgs e)
{
// for each grid items...
foreach(DataGridItem dgItem in countriesGrid.Items)
{
// get GroupRadioButton object...
GroupRadioButton selectRadioButton =
dgItem.FindControl("selectRadioButton") as GroupRadioButton;
// if it is checked (current item is selected)...
if(selectRadioButton != null && selectRadioButton.Checked)
{
// sample data that was boud to the countriesGrid
DataTable dataSource = DataSource.CreateSampleDataSource();
// get country corresponding to the current item...
DataRow row =
dataSource.Rows.Find(countriesGrid.DataKeys[dgItem.ItemIndex]);
// ...and show selected country information
selectedCountryInfo.Text =
String.Format("Selected country: {0}, Capital: {1}",
row["Country"], row["Capital"]);
return;
}
}
// there are no selected countries
selectedCountryInfo.Text = String.Empty;
}

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

东拉西扯:那些“老”互联网公司

中国的互联网向公众开放,满打满算只有10来年的历史,最“老”的互联网公司,也不过是10年老店而已。如果按狗年纪年,1年等于7年,这些10年老店就差不多相当于70年老店了。不过,我这里所说的“老”,并非完全是一个时间概念,主要是说肌体的生命力。
成立于1997-1999年之间的那些互联网公司,比如三大门户新浪、搜狐、网易,比如做电子商务的阿里巴巴、网盛、当当,比如做网络广告的好耶,等等,如今都成了中国互联网上的权贵,德高望重,颐指气使。更重要的是,与他们占有的资源、资金和人才相比,他们能为中国互联网带来的创新和价值,已经今非昔比,他们日新月异的青年时代已经成为过去,每年百分之几百的增长也早就成了一个遥远的回忆(网盛甚至已经陷入负增长)。
不光是中国,在美国,很多人认为Yahoo!也老了,其实Yahoo!不过才13岁,但跟9岁的Google相比,Yahoo!确实老态毕现。在一个变动快速的行业,新的涌现得快,老去得也快,所谓新,只是瞬间的事。
不过好玩儿的是,这几年一些“老”公司,在他们已经失去锐气,成功地沦落为互联网上的传统行业之后,却成为资本市场的宠儿,比如网盛科技。还有很多“老”公司也在盘算着投资者口袋里的钱,比如阿里巴巴和当当。阿里巴巴的百亿美元市值似乎指日可待,当当网的10亿美元估值竟也不怕风大闪了舌头。
也许在别的行业,老就是一种资历,但在互联网不是,创新的活力才是。Yahoo!从被投资者追捧,到被投资者抛弃,也就那么几年时间。投资者发现,尽管它很能吃,但消化机能在衰退;尽管它有引以为傲的庞大的用户数字,但收入几乎不再增长。
并非所有“老”公司,都失去了活力。不过我发现,所有失去活力的“老”公司,几乎都是非常高调的公司,而那些既“老”又保持着活力的公司,反倒大都十分低调,比如腾讯。或许,高调不过是伪装色,用来掩盖失去了活力的“老”。
本文来自: keso's blog

幽默:唐僧与婚介所大妈的精彩斗嘴

猪八戒背着媳妇来看望文明用户,唐僧瞅见徒弟和老婆卿卿我我,甚是嫉妒,于是去婚介所相亲,婚介所大妈接待了他。婚介:姓名?唐僧:唐僧,三藏,也可以叫我小唐,玄奘。婚介:到底叫什么?唐僧:TOM。婚介:年龄?唐僧:不大。婚介:不大?哪一年生的?唐僧:唐朝。婚介:职业?唐僧:化缘。婚介:什么叫化缘?唐僧:就是要饭。婚介:有住房吗?唐僧:没有。婚介:那你住哪?唐僧:天上。婚介:有交通工具吗?唐僧:有。婚介:什么类型的?唐僧:白马。婚介:之前有过女朋友吗?唐僧:有。婚介:都是做什么的?唐僧:白骨精,蜘蛛精,牛魔王……婚介:你玩的挺深啊!唐僧:那都是逢场作戏,感情破裂了。婚介:为什么分手呢?唐僧:他们都想吃我的肉。婚介:对择偶有什么要求吗?唐僧:首先要不喜欢吃肉。婚介:那尼姑好不好?唐僧:不好,本是同根生,相煎何太急。婚介:那观音好不好?唐僧:不好,观音是我顶头上司,我不想发生办公室恋情。婚介:那王母娘娘好不好?唐僧:不好,后台太硬了,我也不喜欢熟女。婚介:那我好不好?唐僧:这……还是要尼姑好了。婚介:你有什么爱好才艺吗?唐僧:我会RAP说唱。婚介:表演一下。唐僧:等我叫个人。(拿起手机,“悟空快来,文明用户有好事找你”)轻轻的悟空来了,带着一片云彩。唐僧:悟空,我们来表演HIPHOP,我来RAP,你来街舞。悟空:又来……(汗)唐僧:COME ON COME ON。当里个当,当里个当,YEAH,YEAH,阿弥……阿弥托付(紧箍咒说唱版)悟空:文明用户饶命,文明用户饶命。(满地板打滚表演了BREAKING地板动作)婚介:汗!原来是虐待动物。

幽默:少男少女性事尴尬爆笑集

1、大二时,全宿舍的女生都喜欢周华建的歌,一盘磁带被大家借来借去的。一日,上铺的女生问:我的周华建呢?下铺的女生回答:在我床上呢!两秒钟寂静无声,然后全体翻倒在床。2、呵呵,俺也想起来了。那年与女友刚谈一个月。一天她对我说腰好酸,好累。我关切地说,怎么啦,要不要上医院看看去?她说不用,我是来例假了。我纳闷:什么例假?寒假还早啦。女友大窘。到现在还常拿此事开我玩笑3、在家看电视,放到紫禁城的画面,我一口就说出“子宫”——半天没敢吭声,不知爸妈和姐姐在想啥?4、和同事出去吃饭,男男女女一堆,喝白酒,从里面翻出一个新式的打火机,是有防止小孩子玩的那种,一群人好奇,把玩,有人问这有什么作用,我脱口而出:“不就是带了个保险套嘛!”说完,我就意识到不妙。果然饭桌上,数秒沉寂,我当时恨不得钻到桌子下面去……尴尬!!5、上初三时有段时间每天早上都有点拉肚子 课间和后面的男同学聊天时很得意的爆出一个专业词汇“我这几天有点生病 可能是早泄” (那时候几乎每个电线杆子上都有这词儿) 男同学惊呆半晌因为我是MM。 6、去年认识一18的小弟弟,他最强的事迹是在考场上没事干,就用大腿夹住DD摩擦,射了竟然!!还有,有一天早上他醒来对着同宿舍的一位哥们狂笑,大家不解。他忍住笑说:“真他妈郁闷,我昨晚做梦把他干了!我想起他叫床的声音了!”我浑身都是汗啊!!!也不知道他一天到晚在想些什么,手足相残啊!7、记得以前他戴过一阵子矫正牙齿的金属牙套。一次我们在一起复习功课,我实在无聊,让他亲亲我下面。结果他也是一时性气,在下面乱拱一气,结果我的毛全都勾在他的牙套上,疼的我大呼小叫,手边又没有剪刀,我们只能在家里保持那个委琐姿势去找剪刀。好在是在家里,若是发生在什么公共场合,后果实在不堪设想!8、我上大学时,我们宿舍隔壁发生过真实的故事。我们宿舍的结构是主楼梯在楼的中间,洗手间在楼的两侧拐角处。夏天冲完凉后,离洗手间近的那些兄弟们,经常冲完凉后直接就把内裤一块儿夜洗了,然后光着身子端脸盆往宿舍走(其实那时候很多兄弟都这样,不过一般都是晚上居多)。有一次,一兄弟中午冲完凉后居然也如此施施然地往宿舍走去,从拐角处转过来,忽然发现对面走来一MM,兄弟大急,随手拽开旁边一宿舍的门(那时候,我们一个系住在一起,临近的宿舍都是自己系的)。此老兄,近宿舍以后,仍把头探在外面看对面那个MM,直到看着那个MM进了一个宿舍,才长舒了一口气,边说:“不好意思,刚才对面来了个女生”,边转过头来,结果,#$%^&&$%^$%^&*$,嘿嘿,宿舍里面三个MM脸色通红,头低的看不见相貌,呵呵9、高三时,我们寝室两个男孩经常学习ML的声音。有一次,一个男孩躺在床上,另一个男孩在他旁边做动作。躺着的男生学女的叫床,跪在床上的男生学男的呼哧的声音。正好班主任进来了,问:你们在干什么?10、上大学了,刚换一手机,可以免提的那种。正跟宿舍里人炫耀着时女朋友来电话了,我立刻使用了免提功能,结果一屋子人都听见我女朋友冲我嚷道:“你呀!上次弄的我到现在还疼呢!”……从此不敢再用免提功能了~~ 11、上了大学以后。一次和同学去学校游泳馆游泳,我们正靠在池边说话,恰好一女生练习潜水,不经意见她的手伸向偶的裆部,一下握住偶的dd。吓得偶赶紧把她的手推开,然后立即游走。后来听同学说,那个女生浮上来后还抱怨:刚才那个人真讨厌,我要把扶手,他却把我的手给推开,害我差点呛到水!12、一次跟几个同学去网吧玩游戏,玩着玩着就进入角色了,开始讲粗话,比如:我CAO,CAO,等之类的,在我们进去后有两个我们不认识的女生也到网吧上网,呆了一会,可能是听我们的话太难听,就走了,网吧老板急了,跟我们几个说:cao cao什么cao,讲话文明点,你们看,刚来几个小姑娘让你们cao跑了,当时我们几个就翻了。13、和老公结婚前一天晚上到他妹妹家玩(他妹妹结婚比我们早)正好他妹夫单位的一个男的也在。聊了一会后说到手机,我说前几天我看到了一个手机模型的打火机,很逼真,显示屏还是精液的。我正说的兴高采烈,那个男的说“还带精液?”我楞住了,老半天才反应过来,是液晶的。晕,就是找不到地缝。14、哈哈,又想起来一个,我和特别好的男生一同去小商品批发站,我看见透明的胸罩带。就趁他不注意时偷偷问多少钱,结果这个家伙是耳聪目明啊,听见了。我一看,反正你也看见了,决定买一个。结果在我讲价钱的时候,这个家伙问了一句:这个是干吗的啊?头花吗?也不好看啊。我晕死。结果老板问(老板是女生哦):原来你不明白啊?
show_item("24943","iscommend");

2009年4月26日星期日

快要51了

经济危机,连工资都打折