ASPNET服务端控件练习(一个机试题)

简单记录:

模糊查询的select语句的拼写

        public List<Model.Student> GetWhereStudent(string name, string sub, string isG)
{
List<Web.Model.Student> lt = new List<Model.Student>();
string sql = "select * from SC_Student where studentName like @n and Subjects=@sub and IsGoodStudent=@is";
SqlParameter[] param = {
new SqlParameter("@n","%"+name+"%"),
new SqlParameter("@sub",sub),
new SqlParameter("@is",isG)};
using (SqlDataReader reader = SqlHelper.ExecuteDataReader(sql, param))
{
if (reader.HasRows)
{
while (reader.Read())
{
Web.Model.Student stu = new Model.Student();
stu.ID = reader.GetInt32();
stu.Name = reader.GetString();
stu.SubId = this.GetSubName(reader.GetInt32());
stu.Score = reader.GetInt32();
stu.IsGoodStudent = reader.GetBoolean();
lt.Add(stu);
}
}
}
return lt;
}

reader拿取 ROW_NUMBER() over(order by studentName)产生的值

        public List<Model.StudentAvg> EditStudent(string name, string sco)
{
List<Web.Model.StudentAvg> lst = new List<Model.StudentAvg>();
int s = ;
SqlParameter[] param =
{
new SqlParameter("@likes","%"+name+"%"),
new SqlParameter("@sco",int.TryParse(sco,out s)==true?s:)
};
using (SqlDataReader reader = SqlHelper.ExecuteProcedure("pro_Student_Avg", param))
{
if (reader.HasRows)
{
while (reader.Read())
{
Web.Model.StudentAvg stuavg = new Model.StudentAvg();
//拿取 ROW_NUMBER() over(order by studentName)产生的值
stuavg.ID = int.Parse(reader.GetSqlValue(0).ToString());
stuavg.Name = reader.GetString();
stuavg.SSum = reader.GetInt32();
stuavg.SAvg = reader.GetInt32();
lst.Add(stuavg);
}
}
}
return lst;
}

对于动态的对ObjectDataSource控件添加参数之前先清空参数:不然参数数据源的SelectParameters会叠加递增的

    protected void Page_Load(object sender, EventArgs e)
{
this.ObjectDataSource1.SelectParameters.Clear();
this.ObjectDataSource1.SelectParameters.Add("name", "");
this.ObjectDataSource1.SelectParameters.Add("sco", "");
this.ObjectDataSource1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
this.ObjectDataSource1.SelectParameters.Clear();
string name = this.txtName.Text;
string sco = this.txtSco.Text;
this.ObjectDataSource1.SelectParameters.Add("name", name);
this.ObjectDataSource1.SelectParameters.Add("sco", sco);
//Web.BLL.Tran tran = new Web.BLL.Tran();
//List<Web.Model.StudentAvg> lst = tran.GetStudentAvg(name, sco);
this.ObjectDataSource1.DataBind();
}

Repeater应用实例:里面的table tr项模版不能是服务端的

    <form id="form1" runat="server">
<div>
<%-- <table id="lst">
<tr>
<td>学号</td>
<td>姓名</td>
<td>科目</td>
<td>分数</td>
<td>好学生</td>
<td>操作</td>
</tr>
<%=sb.ToString() %>>
</table><asp:LinkButton Visible="false" OnClientClick="Del()" ID="isG" runat="server">设置为好学生</asp:LinkButton>--%>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAllStudent" TypeName="Web.BLL.Tran"></asp:ObjectDataSource>
<table id="lst">
<tr>
<td>学号</td>
<td>姓名</td>
<td>科目</td>
<td>分数</td>
<td>好学生</td>
<td>操作</td>
</tr>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
<ItemTemplate>
<tr>

// Parameters:
// expression:
// The navigation path from the container to the public property value to place
// in the bound control property.

                            <td><%# Eval("ID") %></td>
<td><%# Eval("Name") %></td>
<td><%# ((Web.Model.Subject)Eval("SubId")).StuId %></td>
<td><%# Eval("Score") %></td>
<td>
<asp:LinkButton ID="LinkButton1" Enabled='<%#!(bool)Eval("IsGoodStudent") %>' CommandArgument='<%#Eval("ID")%>' OnClick="Unnamed_Click" runat="server">
<%#(bool)Eval("IsGoodStudent") ==true ? "好学生" : "设置为好学生" %></asp:LinkButton></td>
<td><a href='Edit_Student.aspx?id=<%#Eval("ID") %>'>编辑</a>&nbsp;&nbsp;<a href='javascript:Del(<%#Eval("ID") %>);'>删除</a></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<br /><br /><br />
<asp:Panel ID="Panel1" runat="server" GroupingText="学生查询">
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllSubject" TypeName="Web.BLL.Tran"></asp:ObjectDataSource>
姓名:<asp:TextBox ID="txtSName" runat="server"></asp:TextBox><br />
科目:<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource2" DataTextField="StuId" DataValueField="Id"></asp:DropDownList><br />
是否是好学生:<asp:CheckBox ID="CheckBox1" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="查询" OnClick="Button1_Click" />
</asp:Panel>
</div>
</form>
    protected void Unnamed_Click(object sender, EventArgs e)
{
//还原 参数
this.ObjectDataSource1.SelectParameters.Clear();
LinkButton lb = ((LinkButton)sender);
if (lb.CommandArgument != "")
{
if (true)//YEs No 对话框
{
string id = lb.CommandArgument; Web.BLL.Tran tran = new Web.BLL.Tran();
if (tran.SetGood(int.Parse(id)) == )
{
this.Repeater1.DataBind();
}
} }
}
protected void Button1_Click(object sender, EventArgs e)
{
this.ObjectDataSource1.SelectParameters.Clear();
this.ObjectDataSource1.SelectParameters.Add("name", this.txtSName.Text);
this.ObjectDataSource1.SelectParameters.Add("sub", this.DropDownList1.SelectedIndex + .ToString());
this.ObjectDataSource1.SelectParameters.Add("isG", this.CheckBox1.Checked == true ? "" : "");
this.ObjectDataSource1.SelectMethod = "GetWhereStudent";
this.ObjectDataSource1.DataBind();
//this.Page.DataBind();
//this.ObjectDataSource1.SelectMethod = "GetWhereStudent";
}

js界面删除表格行

        function DelRow() {
var ta = document.getElementById("lst");
var len=ta.rows.length;
for (var i = ; i < len; i++) {
var ro = ta.rows.item(i);
if (ro.childNodes[].innerHTML = arguments[]) {
ta.deleteRow(i);
return;
}
}
};

ListView下拿后台拿服务端控件值

    protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string id = Request.QueryString["id"];
if (id != null)
{
//this.ObjectDataSource1.SelectParameters.Add("id", id);
this.ObjectDataSource1.SelectParameters[].DefaultValue = id;
this.ObjectDataSource1.DataBind();
}
} }
protected void Button1_Click(object sender, EventArgs e)
{
Web.BLL.Tran tran = new Web.BLL.Tran();
string id = ((TextBox)this.ListView1.Items[].Controls[]).Text;
string name = ((TextBox)this.ListView1.Items[].Controls[]).Text;
string sub = ((TextBox)this.ListView1.Items[].Controls[]).Attributes["MySubID"];
string sco = ((TextBox)this.ListView1.Items[].Controls[]).Text;
bool isG = ((CheckBox)this.ListView1.Items[].Controls[]).Checked;
if ( == tran.EditStudent(id, name, sub, sco, isG))
{
this.ObjectDataSource1.DataBind();
Response.Redirect("Manage_Student.aspx");
}
}
<body>
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetStudent" TypeName="Web.BLL.Tran">
<SelectParameters>
<asp:Parameter Name="id" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<br />
<table> <tr>
<td>学号</td>
<td>姓名</td>
<td>科目</td>
<td>分数</td>
<td>好学生</td>
</tr>
<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1">
<ItemTemplate>
<tr>
<td>
<asp:TextBox ID="TextBox4" disabled="disabled" runat="server" Text='<%#Eval("ID") %>'></asp:TextBox>
</td> <td>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox></td> <td>
<asp:TextBox ID="TextBox3" disabled="disabled" MySubID='<%#((Web.Model.Subject)Eval("SubId")).Id %>' Text='<%#((Web.Model.Subject)Eval("SubId")).StuId %>' runat="server"></asp:TextBox></td> <td>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("Score") %>'></asp:TextBox></td> <td>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Eval("IsGoodStudent") %>' /></td> </tr>
</ItemTemplate> </asp:ListView>
</table>
<asp:Button ID="btnSure" OnClick="Button1_Click" runat="server" Text="确定" />
</div>
</form>

服务端控件DropDownList绑定数据

            <tr>
<td><input type="text" name="txtName" /></td>
<td>
<asp:DropDownList runat="server" ID="scoName" DataSourceID="ObjectDataSource1" DataTextField="StuId" DataValueField="Id">
</asp:DropDownList></td>
<td><input type="text" name="txtSco" /></td>
<td>
<asp:CheckBox Text="" runat="server" ID="IsGoodStudent" /></td>
</tr>

后台获取服务端控件CheckBox的选择值

        bool isGS = this.IsGoodStudent.Checked;

项目文件:http://pan.baidu.com/s/1gdJxjvt

上一篇:.Net Mvc3框架调用服务端控件解决方案


下一篇:python---基础知识回顾(七)迭代器和生成器