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 [email protected] and [email protected]";
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(0);
stu.Name = reader.GetString(1);
stu.SubId = this.GetSubName(reader.GetInt32(2));
stu.Score = reader.GetInt32(3);
stu.IsGoodStudent = reader.GetBoolean(4);
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 = 0;
SqlParameter[] param =
{
new SqlParameter("@likes","%"+name+"%"),
new SqlParameter("@sco",int.TryParse(sco,out s)==true?s:0)
};
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(1);
stuavg.SSum = reader.GetInt32(2);
stuavg.SAvg = reader.GetInt32(3);
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", "0");
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)) == 1)
{
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 + 1.ToString());
this.ObjectDataSource1.SelectParameters.Add("isG", this.CheckBox1.Checked == true ? "1" : "0");
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 = 1; i < len; i++) {
var ro = ta.rows.item(i);
if (ro.childNodes[0].innerHTML = arguments[0]) {
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[0].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[0].Controls[1]).Text;
string name = ((TextBox)this.ListView1.Items[0].Controls[3]).Text;
string sub = ((TextBox)this.ListView1.Items[0].Controls[5]).Attributes["MySubID"];
string sco = ((TextBox)this.ListView1.Items[0].Controls[7]).Text;
bool isG = ((CheckBox)this.ListView1.Items[0].Controls[9]).Checked;
if (1 == 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

ASPNET服务端控件练习(一个机试题),码迷,mamicode.com

时间: 2024-08-26 04:01:55

ASPNET服务端控件练习(一个机试题)的相关文章

如何通过JavaScript构建Asp.net服务端控件

摘要 虽然ASP.NET的服务器控件一直被大家所诟病,但是用户控件(ACSX)在某些场景下还是非常有用的. 在一些极特珠的情况下,我们会使用JavaScript动态的构建页面中的控件,但假设遇到了我要用JavaScript构建一个服务端控件.用户控件时,该怎么办? 我们常常说,服务端控件运行在服端器上,那么这话是什么意思呢? 服务端控件,其本质是一构建HTML语句的封装,以事先编排好的方式,生成一套HTML并通过Http协议返回给客户端.因此,我们所写的服务端控件,在响应HTTP请求时,早已不存

atitit.Atitit. Gui控件and面板-----服务端控件 java struts的实现最佳实践

atitit.Atitit.  Gui控件and面板-----服务端控件 java struts的实现最佳实践 1. 服务器控件的类别 1 1.1. 数据控件:该类控件可细分为两种类型:数据源控件和数据绑定控件. 1 1.2. 站点导航控件: 1 1.3. WebParts控件: 1 1.4. 登录控件: 1 1.5. Ajax 控件 1 2. jsf 2 3. Apache  ecs 2 4. Custom ui control... 2 5. Struts UI Tags display d

GridView 服务端控件添加 js

针对服务端控件的 CommandField “Delete” 添加 js $("#GridView1").find("a").each( function() { var text = $(this).html(); if (text == "Delete") { var td = $(this).parent().parent().children().eq(0); $(this).on("click", function(

atitit.组件化事件化的编程模型--服务端控件(1)---------服务端控件与标签的关系

1. 服务器控件是可被服务器理解的标签.有三种类型的服务器控件: 1 1.1. HTML 服务器控件 - 传统的 HTML 标签 1 1.2. Web 服务器控件 - 新的 ASP.NET 标签 1 1.3. Validation 服务器控件 - 用于输入验证 1 2. HTML 服务器控件 1 3. - Web 服务器控件 2 4. 标准服务器控件可分为以下6种类型: 2 4.1. (1)标准控件:主要是指传统的Web窗体控件,例如TextBox.Button.Panel等控件.它们有一组标准

05-19Web端控件,页面传值

一.记忆Web端控件需要配合HTML 中的Form表单元素 1.Label 会被编译成span标签 属性: Text:文本内容 CssClass:CSS样式 Enlabled:是否可用 Visible:是否可见 2.Literal 空的,C#会把里面的Text内容直接作为网页代码传过去,比如Text里面写上<input type="button" />会直接在网页中插入一个按钮 属性: Text:内容 文本类 文本框      <input type="te

Web端控件,页面传值

一.记忆Web端控件需要配合HTML 中的Form表单元素 Label - 在HTML中被编译成<span> Literal - 在HTML中被编译成空 文本类 文本框      <input type="text">                        TextBox 密码框      <input type="password">                 TextBox 属性TextMode="pa

Xamarin.Forms自定义用户界面控件实现一个HybridWebView(混合webview)

原文:Implementing a HybridWebView呈现一个特定于平台的视图 Xamarin.Forms自定义用户界面控件应该来自视图类(View class),用于在屏幕上放置布局和控件.本文演示了如何为HybridWebView(混合webview)自定义控件创建自定义渲染器,该控件演示了如何增强特定平台的web控件,以允许从JavaScript调用c#代码. 每一个Xamarin.Forms视图为每个创建本地控件实例的平台提供了相应的渲染器.当一个视图被Xamarin.Forms

VC ON_CONTROL_RANGE多个控件响应一个方法

步骤/方法 分三个步骤 在头文件中声明函数例如 afx_msg void onNum(UINT uID) 在.cpp文件中添加函数体 void CCalculatorDlg::OnNum(UINT uID) { UINT index=uID-IDC_NUM_0; CString num; num.Format(_T("%d"),index); AfxMessageBox(num); } 4 添加消息映射 ON_CONTROL_RANGE(BN_CLICKED,IDC_NUM_0,IDC

给easyui datebox时间框控件扩展一个清空的实例

给easyui datebox扩展一个清空的实例 步骤一:拓展插件 /** * 给时间框控件扩展一个清除的按钮 */ $.fn.datebox.defaults.cleanText = '清空'; (function ($) { var buttons = $.extend([], $.fn.datebox.defaults.buttons); buttons.splice(1, 0, { text: function (target) { return $(target).datebox("o