• .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>");

        }

     

  • 相关阅读:
    JS使用Enter事件将输入的字符倒叙输出
    博客园 页面定制CSS代码
    C#将DataTable数据导出CSV文件
    js时间格式转换
    将十进制数据转换为二进制AND将二进制数据转换为十进制!
    ref和out的使用与区别
    形参和实参的区别
    为啥Underlay才是容器网络的最佳落地选择
    你问我答:容器篇(1)
    切忌一步到位,谈谈DevOps实施落地
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1180572.html
Copyright © 2020-2023  润新知