您的位置:首页 > 编程语言

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

2008-05-03 20:38 465 查看
.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="<div id="de" onclick="JavaScript:return confirm('你确定要删除?')">删除</div>">

<HeaderStyle Font-Size="Small" />

</asp:CommandField>

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

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: