首页 > 代码库 > ASPNET服务端控件练习(一个机试题)

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(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=http://www.mamicode.com/Edit_Student.aspx?id=<%#Eval("ID") %>>编辑</a>&nbsp;&nbsp;<a href=http://www.mamicode.com/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 =http://www.mamicode.com/ 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