• ASP.net杂谈(一)


    最近自学了ASP.net框架,C#做后台。从中有了一些学习体会,所以写成博客与大家分享一下。

    1. 向GridView中嵌入按钮。

      (前台的代码)

      

    代码
    <asp:TemplateField ShowHeader="False">
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
    CommandName="Select" Text="进入投票"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>

      

      (后台相应的事件代码)

    代码
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
     /*进入投票*/
    {

    Response.Redirect(
    "vote.aspx"); /*跳转页面*/
    }

      2.  当所要用RadioButton控件个数不能确定时,可用RadioButtonList控件,可以用它绑定数据库,由数据库中数据决定RadioButton的个数。

      (前台的代码)

    <asp:DropDownList ID = "DDL1" runat ="server" Width = "150px" AutoPostBack="True"
    onselectedindexchanged="DDL1_SelectedIndexChanged"></asp:DropDownList>

      (后台的事件代码)

    代码
    protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
    {
    string str = "select * from users where use_name = '" + DDL1.SelectedValue.ToString() + "'";
    GridView2.DataSource
    = conn.execsql(str);
    GridView2.DataBind();
    }

      <注意1>要激发RadioButtonList的SelectIndexChanged事件,必须把RadioButtonList的AutoPostBack属性设为"True"

      <注意2>如果你想在打开RadioButtonList控件的时候,第一项为空。

           可以向RadioButtonList中插入一个空项,但必须是在绑定完数据库之后。

          

    代码
    string tem1 = "select *theme where the_title = '" + Session["result_the_title"] + "'";
    DDL1.DataSource
    = conn.execsql(tem1);
    DDL1.DataTextField
    = "sub_title";
    DDL1.DataBind();
    DDL1.Items.Insert(
    0, new ListItem()); //插入空项,此举必须放到数据绑定之后

      3.  关于Session对象、Cookie对象

        3.1  Session对象用于存储在多个页面调用之间的特定用户的信息、传递参数信息等。

        3.2  Cookie对象用于保存客户端浏览器请求的服务器页面,也可用它存放非敏感性的用户信息。

           在这次我做的实验中,我就用它保存了客户端用户的IP地址。

    Response.Cookies["res_ip"].Value = Request.UserHostAddress.ToString();
    string res_ip = Request.Cookies["res_ip"].Value;

      4.  收获了一条“特别的”SQL语句

    代码
    string str = "select a.sub_id,a.sub_choose,isnull((select count(res_id) from result
    where result.sub_id = a.sub_id),0) as vcount from subject a where a.sub_title = '网速评价'";

      <注意>这是一条嵌套的SQL查询语句:因为子查询也是要作为输出项的,同时子查询的where条件要用到父查询中的一个输出项,

          同时在其中用到了一个isnull函数判断子查询结果是否为空。

  • 相关阅读:
    vs2017 项目调试浏览器网页闪退Bug
    “WebPageBase”在未引用的程序集中定义。必须添加对程序集“System.Web.WebPages, Version=1.0.0.0,Culture=neutral....."的引用
    SQL Server判断日期是否为周六 周日
    获取各国的日期时间
    .net Api项目初步搭建并移除XML格式
    鼠标滚轮事件
    js中if表达式判断规则
    原生轮播图
    C# uri
    SqlServer 备份还原教程
  • 原文地址:https://www.cnblogs.com/guolebin7/p/1836554.html
Copyright © 2020-2023  润新知