• .NET常用的数据绑定代码实例:


     

    .NET常用的数据绑定代码实例:

    public class norke

    {

        public norke()

        {

               // myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);

        }

        public static DataSet GreatDs(string sql)

        {

            SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);

            SqlDataAdapter Dar = new SqlDataAdapter(sql, myConnection);

            DataSet ds = new DataSet();

            Dar.Fill(ds);

            return ds;

        }

        public static void DoSql(string sql)

        {

            SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);

            myConnection.Open();//打开数据库

            SqlCommand cmd = new SqlCommand(sql, myConnection);

            cmd.ExecuteNonQuery();//

            myConnection.Close();//关闭数据库

        }

    }

    DropDownList绑定

    void bind()

        { 

    string sql6;

            sql6 = "select * from dbo.friendlink order by id desc ";

            DropDownList1.DataSource = norke.GreatDs(sql6);

            DropDownList1.DataTextField = "web_name";

            DropDownList1.DataValueField = "web_address";

            DropDownList1.DataBind()

       }

    对应事件处理代码:

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

        {

            Response.Redirect(DropDownList1.SelectedValue);

        }

    }

     

    DataList绑定

    前台页面现实的源代码

    <asp:DataList  Width="371px" ID="DataList1" runat="server" CellSpacing="10" RepeatDirection="Horizontal" RepeatColumns="5">

                                            <ItemTemplate>

                                                <table width="100" height="110" border="1" cellpadding="0" cellspacing="0" bordercolorlight="#000000"

                                                    bordercolordark="#ffffff">

                                                    <tr>

                                                        <td>

                                                            <a href='ProductShow.aspx?id=<%# DataBinder.Eval(Container.DataItem, "id").ToString()%>'>

                                                                <img width="100" height="100" border="0" src="<%# DataBinder.Eval(Container.DataItem, "Product_picture").ToString()%>" />

                                                            </a>

                                                        </td>

                                                    </tr>

                                                </table>

                                            </ItemTemplate>

                                        </asp:DataList>

    后台绑定datalist.cs页面现实的源代码

    string sql4;

            sql4 = "select id,Product_picture from Product where Product_picture<>'' order by id desc";

            DataList1.DataSource = norke.GreatDs(sql4);

            DataList1.DataBind();

     

    GridView绑定

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging"

                                AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" DataKeyNames="ID"

                                OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"

                                OnRowUpdating="GridView1_RowUpdating" Width="620px" BorderStyle="Double">

                                <Columns>

                                    <asp:BoundField DataField="id" ReadOnly="True" HeaderText="序号">

                                        <HeaderStyle Font-Size="Small" />

                                    </asp:BoundField>

                                    <asp:BoundField DataField="class1" HeaderText="类名">

                                        <HeaderStyle Font-Size="Small" />

                                    </asp:BoundField>

                                    <asp:CommandField ShowDeleteButton="True" HeaderText="删除" DeleteText="&lt;div id=&quot;de&quot; onclick=&quot;JavaScript:return confirm('你确定要删除?')&quot;&gt;删除&lt;/div&gt;">

                                        <HeaderStyle Font-Size="Small" />

                                    </asp:CommandField>

                                    <asp:CommandField ShowEditButton="True" HeaderText="编辑" EditText="&lt;div id=&quot;de&quot; onclick=&quot;JavaScript:return confirm('你确定要编辑?')&quot;&gt;编辑&lt;/div&gt;">

                                        <HeaderStyle Font-Size="Small" />

                                    </asp:CommandField>

                                </Columns>

                            </asp:GridView>

    protected void Button1_Click(object sender, EventArgs e)

        {

            string sql;

            sql = "insert into news_class(class1)values('"+(TextBox_class1.Text)+"') ";

            norke.DoSql(sql);

            Page.RegisterStartupScript("msgOnlyAlert", "<script>alert('添加分类成功!');</script>");

            bind();

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

        {

            GridView1.PageIndex = e.NewPageIndex;

            bind();

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

        {

            //得到编号

            int sid = (int)GridView1.DataKeys[e.RowIndex].Value;

            Response.Write(sid);

            //从数据库中删除

            string sql = "DELETE FROM news_class where id=" + sid;

            norke.DoSql(sql);

            bind();

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

        {

            GridView1.EditIndex = -1;

            bind();

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

        {

            GridView1.EditIndex = e.NewEditIndex;

            bind();

        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

        {

            int upid = (int)GridView1.DataKeys[e.RowIndex].Value;

            string u = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;

            string str = "update news_class set class1='" + (u) + "' where id=" + upid;

            norke.DoSql(str);

            Page.RegisterStartupScript("msgOnlyAlert", "<script>alert('更新成功!');</script>");

        }

     

  • 相关阅读:
    angularjs+webapi2 跨域Basic 认证授权(二)
    angularjs+webapi2 跨域Basic 认证授权(一)
    记录一些日常windows命令或操作技巧
    unable to locate nuget.exe
    Adobe Acrobat XI Pro闪退原因及解决办法
    货物移动BAPI:BAPI_GOODSMVT_CREATE报错提示“不能执行功能模块 MB_CREATE_GOODS_MOVEMENT”的原因
    ABAP中将Unicode字符串转换成中文的方法
    ABAP中时间戳的处理
    科学吵架修炼指南(摘自凤凰网)
    SAP Web Service简介与配置方法
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1180572.html
Copyright © 2020-2023  润新知