1、建一个ascx文件,取名为txtBoxRejectChk.ascx(一个TextBox与Check的排他功能),code如下:
(1.1).ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="txtBoxRejectChk.ascx.cs" Inherits="txtBoxRejectChk" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<table id="tbbody" runat="server" cellspacing="10">
<tr>
<td>
<asp:TextBox ID="txtBox" runat="server" Width="600" TextMode="MultiLine" AutoPostBack="true" OnTextChanged="txtBox_TextChanged"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:CheckBox ID="chkBox" runat="server" Text="No improvement required" AutoPostBack="true" OnCheckedChanged="chkBox_CheckedChanged"/>
</td>
</tr>
</table>
(1.2).ascx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class txtBoxRejectChk : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public String Comment
{
get
{
return txtBox.Text;
}
set
{
txtBox.Text = value;
}
}
public string check
{
get
{
return chkBox.Text;
}
set
{
chkBox.Text = value;
}
}
public CheckBox chek
{
get
{
return chkBox;
}
set
{
chkBox = value;
}
}
protected void txtBox_TextChanged(object sender, EventArgs e)
{
if (txtBox.Text.Length > 0)
{
chkBox.Checked = false;
chkBox.Enabled = false;
}
else
{
chkBox.Enabled = true;
}
}
protected void chkBox_CheckedChanged(object sender, EventArgs e)
{
if (chkBox.Checked == true)
{
txtBox.Text = "";
}
else
{
chkBox.Checked = false;
}
}
}
2.投票的0分-10分的功能
(2.1)MitiSelect.ascx <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MitiSelect.ascx.cs" Inherits="MitiSelect" %><table width="800px">
<tr>
<td colspan="4" align="left">
Completed satisfied
</td>
<td colspan="4" align="center">
Acceptable
</td>
<td colspan="4" align="right">
Dissatisfied
</td>
</tr>
<tr>
<td colspan="12">
<asp:RadioButtonList ID="radlist1" runat="server" RepeatDirection="Horizontal"
Width="800px" onselectedindexchanged="radlist1_SelectedIndexChanged">
<asp:ListItem Text="10"></asp:ListItem>
<asp:ListItem Text="9"></asp:ListItem>
<asp:ListItem Text="8"></asp:ListItem>
<asp:ListItem Text="7"></asp:ListItem>
<asp:ListItem Text="6"></asp:ListItem>
<asp:ListItem Text="5"></asp:ListItem>
<asp:ListItem Text="4"></asp:ListItem>
<asp:ListItem Text="3"></asp:ListItem>
<asp:ListItem Text="2"></asp:ListItem>
<asp:ListItem Text="1"></asp:ListItem>
<asp:ListItem Text="0"></asp:ListItem>
<asp:ListItem Text="DK"></asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
</table> (2.2)MitiSelect.ascx.cs using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class MitiSelect : System.Web.UI.UserControl
{
string strV = "";
protected void Page_Load(object sender, EventArgs e)
{ }
public RadioButtonList radblist
{
get
{
return radlist1;
} set
{
radlist1 = value;
}
} public string strValue
{
get
{
return strV;
} set
{
strV = value;
} }
protected void radlist1_SelectedIndexChanged(object sender, EventArgs e)
{ strV=GetRadButtonListValue();
} private string GetRadButtonListValue()
{
string str = "";
if (radlist1.Items[0].Selected == true)
{
str = "10";
}
if (radlist1.Items[1].Selected == true)
{
str = "9";
}
if (radlist1.Items[2].Selected == true)
{
str = "8";
}
if (radlist1.Items[3].Selected == true)
{
str = "7";
}
if (radlist1.Items[4].Selected == true)
{
str = "6";
}
if (radlist1.Items[5].Selected == true)
{
str = "5";
}
if (radlist1.Items[6].Selected == true)
{
str = "4";
}
if (radlist1.Items[7].Selected == true)
{
str = "3";
}
if (radlist1.Items[8].Selected == true)
{
str = "2";
}
if (radlist1.Items[9].Selected == true)
{
str = "1";
}
if (radlist1.Items[10].Selected == true)
{
str = "0";
}
if (radlist1.Items[11].Selected == true)
{
str = "99";
}
return str;
}
}
3.引用webusercontrol,取名为UserWebControl.aspx,code如下:
(3.1)UserWebControl.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserWebControl.aspx.cs" Inherits="UserWebControl" %>
<%@ Register src="txtBoxRejectChk.ascx" tagname="WebUserControl_textRejchk" tagprefix="uc1" %>
<%@ Register src="MitiSelect.ascx" tagname="WebUserControl_mutiselect" tagprefix="uc2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Label ID="lbl1" runat="server"></asp:Label>
<asp:Label ID="lbl2" runat="server"></asp:Label>
<uc1:WebUserControl_textRejchk ID="txtBoxRechk" runat="server" />
</div>
<div>
<uc1:WebUserControl_textRejchk ID="WebUserControl_textRejchk1" runat="server" />
</div>
<div>
<uc2:WebUserControl_mutiselect ID="WebUserControl_mutiselect1" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="Submit" />
<br />
</div>
</div>
</form>
</body>
</html>
(3.2)UserWebControl.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UserWebControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lbl1.Text=txtBoxRechk.Comment;
lbl2.Text = txtBoxRechk.check;
lbl2.Text = WebUserControl_textRejchk1.check;
lbl2.Text = WebUserControl_mutiselect1.strValue;
}
protected void Button1_Click(object sender, EventArgs e)
{
lbl2.Text = WebUserControl_mutiselect1.strValue;
}
}