62,041
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Response.Write("ok");
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
Response.Write(GridView1.EditIndex);
GridViewBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GridViewBind();
}
}
private void GridViewBind()
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string SqlStr = "SELECT * From [Msgs] Order By Id desc";
DataSet ds = new DataSet();
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "Msgs");
if (conn.State.ToString() == "Open") conn.Close();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
公告管理
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>公告管理</h2>
<form id="Form1" runat=server>
<asp:GridView ID="GridView1" Runat="Server"
AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False"
BackColor="#CCFDCC" BorderColor="Black" BorderStyle="Dashed" BorderWidth="1px"
Caption="" DataKeyNames="ID" Width="720px" CaptionAlign="Left"
Font-Size="14px" OnRowEditing="GridView1_RowEditing">
<RowStyle Height="30px" HorizontalAlign="Left" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" FooterStyle-BorderStyle="NotSet"
ControlStyle-BorderStyle="Solid" >
<ControlStyle BorderStyle="Solid"></ControlStyle>
</asp:BoundField>
<asp:BoundField DataField="title" HeaderText="内容" SortExpression="title" />
<asp:BoundField DataField="CreateTime" HeaderText="更新时间"
SortExpression="CreateTime" />
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="编辑"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete" Text="删除"/></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#4F6F04" ForeColor="White" HorizontalAlign="Left"
BorderColor="#046004" BorderStyle="Solid" BorderWidth="1px" Font-Bold="False"
Font-Underline="False" Height="30px" />
<EditRowStyle BackColor="#FAF09A" BorderColor="#B6A50A" ForeColor="White" />
<PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText="" />
<RowStyle Height="20px" BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</asp:GridView>
</form>
</asp:Content>