在項(xiàng)目中主要用了gridview來顯示數(shù)據(jù),其中的方法都是常用的。
1、利用sqldatasource動態(tài)邦定數(shù)據(jù),其中sqldatasource1是托放在頁面的sqldatasource控件,將數(shù)據(jù)邦定到gridview1上面
void gridviewband() {
try
{
SqlDataSource1.ConnectionString = ConfigurationSettings.AppSettings[ "sqlconn"];
SqlDataSource1.SelectCommand = squerysql;
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
//看有沒有數(shù)據(jù) 如果沒有數(shù)據(jù) 提示錯誤 lblresult的text的內(nèi)容是錯誤信息
int count = GridView1.Rows.Count;
if (count == 0) { lblresult.Visible = true; }
else { lblresult.Visible = false; }
}
catch (SqlException ex5)
{
//提示錯誤信息
}
finally {
}
}
2、選擇一行,獲取關(guān)鍵字,然后傳到其他頁面
protected void GridView1_SelectedIndexChanged( object sender, EventArgs e)
{//選擇某一行
Session[ "selectkey"] = GridView1.SelectedDataKey.Value;
Response.Redirect( "~/employee/employeedetail.aspx");
}
3、獲取編輯行的關(guān)鍵字
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//編輯修改
Session[ "editkey"] = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
Response.Redirect( "~/employee/employeeedit.aspx");
}
4、刪除一行,獲取關(guān)鍵字。這個地方還是用rowdeleting 事件。
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String idno111 = GridView1.DataKeys[e.RowIndex].Value.ToString();
SqlDataSource1.DeleteCommand = "delete from empinfo where empnum=' " + idno111 + "'"
+ "delete from employee where empnum=' " + idno111 + "'";
//刪除行
if (e.RowIndex >= 0)
{
String connectstr = ConfigurationSettings.AppSettings[ "sqlconn"];
SqlConnection sqlconn = new SqlConnection(connectstr);
sqlconn.Open();
SqlCommand comm = new SqlCommand(SqlDataSource1.DeleteCommand, sqlconn);
comm.ExecuteNonQuery();
sqlconn.Close();
}
}
5、刪除前提示關(guān)鍵字,不僅僅是提示“確認(rèn)刪除嗎?” 而是提示“確認(rèn)刪除關(guān)鍵字是***的數(shù)據(jù)嗎?”
首先在aspx文件腳本里面要設(shè)置 重要看的是gridview的標(biāo)簽里面要有
DatakeyNames="關(guān)鍵字" 同時 CommandArgument='<%# Eval("departno") %>' 這兩處要填寫
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
CellPadding ="4" DataKeyNames="departno" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines ="None" OnDataBound="CustomersGridView_DataBound" OnRowDeleting="GridView1_RowDeleting"
OnRowEditing ="GridView1_RowEditing" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
PageSize ="15" Width="800px" OnRowDataBound="GridView1_RowDataBound">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="departno" HeaderText="部門編號" SortExpression="departno" />
<asp:BoundField DataField="father" HeaderText="父級部門" SortExpression="father" />
<asp:BoundField DataField="departname" HeaderText="部門名稱" SortExpression="departname" />
<asp:BoundField DataField="person" HeaderText="聯(lián)系人" SortExpression="person" />
<asp:BoundField DataField="tel" HeaderText="固定電話" SortExpression="tel" />
<asp:BoundField DataField="mobiletel" HeaderText="手機(jī)" SortExpression="mobiletel" />
<asp:CommandField ButtonType="Image" HeaderText="查看" SelectImageUrl="~/icons/look.gif"
SelectText ="查看" ShowSelectButton="True">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
<asp:CommandField ButtonType="Image" EditImageUrl="~/icons/edit.gif" EditText="編輯"
HeaderText ="編輯" ShowEditButton="True">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
<asp:TemplateField HeaderText="刪除" ShowHeader="False">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" CommandName="Delete"
ImageUrl ="~/icons/delete.gif" CommandArgument='<%# Eval("departno") %>' Text="刪除" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerTemplate>
<table width="100%">
<tr>
<td width="70%">
<asp:Label ID="MessageLabel" runat="server" Font-Size="Larger" ForeColor="Black"
Text ="頁碼:"></asp:Label>
<asp:DropDownList ID="PageDropDownList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged" >
</asp:DropDownList>
<asp:LinkButton ID="linkBtnFirst" runat="server" CommandArgument="First" CommandName="Page"
ForeColor ="Black">首頁</asp:LinkButton>
<asp:LinkButton ID="linkBtnPrev" runat="server" CommandArgument="Prev" CommandName="Page"
ForeColor ="Black">上一頁</asp:LinkButton>
<asp:LinkButton ID="linkBtnNext" runat="server" CommandArgument="Next" CommandName="Page"
ForeColor ="Black">下一頁</asp:LinkButton>
<asp:LinkButton ID="linkBtnLast" runat="server" CommandArgument="Last" CommandName="Page"
ForeColor ="Black">末頁</asp:LinkButton>
</td>
<td align="right" width="70%">
<asp:Label ID="CurrentPageLabel" runat="server" ForeColor="black"></asp:Label>
</td>
</tr>
</table>
</PagerTemplate>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
然后在cs頁面里填入事件:
//刪除某一行的時候要提示關(guān)鍵字 不僅是提示確認(rèn)刪除,departno是關(guān)鍵字
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton l = (ImageButton)e.Row.FindControl( "ImageButton1" );
l.Attributes.Add( "onclick", "javascript: return " +
"confirm('確認(rèn)要刪除部門編號為 " +
DataBinder.Eval(e.Row.DataItem, "departno") + " 的部門嗎?" + "')");
}
}
}
這樣刪除的時候就能提示關(guān)鍵字了。
6、顯示頁碼
顯示頁碼要在aspx代碼里填入幾行數(shù)據(jù),上面的代碼已經(jīng)有了 重新寫一下:
代碼放在 </Columns>下面
<PagerTemplate>
<table width="100%">
<tr>
<td width="70%">
<asp:Label ID="MessageLabel" runat="server" Font-Size="Larger" ForeColor="Black"
Text ="頁碼:"></asp:Label>
<asp:DropDownList ID="PageDropDownList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged" >
</asp:DropDownList>
<asp:LinkButton ID="linkBtnFirst" runat="server" CommandArgument="First" CommandName="Page"
ForeColor ="Black">首頁</asp:LinkButton>
<asp:LinkButton ID="linkBtnPrev" runat="server" CommandArgument="Prev" CommandName="Page"
ForeColor ="Black">上一頁</asp:LinkButton>
<asp:LinkButton ID="linkBtnNext" runat="server" CommandArgument="Next" CommandName="Page"
ForeColor ="Black">下一頁</asp:LinkButton>
<asp:LinkButton ID="linkBtnLast" runat="server" CommandArgument="Last" CommandName="Page"
ForeColor ="Black">末頁</asp:LinkButton>
</td>
<td align="right" width="70%">
<asp:Label ID="CurrentPageLabel" runat="server" ForeColor="black"></asp:Label>
</td>
</tr>
</table>
</PagerTemplate>
然后在gridview的標(biāo)簽里面填入
OnDataBound="CustomersGridView_DataBound"
位置看 5 的代碼就可以了。
然后再 cs代碼區(qū)加入如下代碼
//顯示頁碼
protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[ 0].FindControl("PageDropDownList");
GridView1.PageIndex = pageList.SelectedIndex;
}
protected void CustomersGridView_DataBound(Object sender, EventArgs e)
{
try
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
LinkButton linkBtnFirst = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnFirst");
LinkButton linkBtnPrev = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnPrev");
LinkButton linkBtnNext = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnNext");
LinkButton linkBtnLast = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnLast");
if (GridView1.PageIndex == 0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
}
else if (GridView1.PageIndex == GridView1.PageCount - 1)
{
linkBtnLast.Enabled = false;
linkBtnNext.Enabled = false;
}
else if (GridView1.PageCount <= 0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
linkBtnNext.Enabled = false;
linkBtnLast.Enabled = false;
}
DropDownList pageList = (DropDownList)pagerRow.Cells[ 0].FindControl("PageDropDownList");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
if (pageList != null)
{
for (int i = 0; i < GridView1.PageCount; i ++)
{
int pageNumber = i + 1;
ListItem item = new ListItem(pageNumber.ToString() + "/" + GridView1.PageCount.ToString(), pageNumber.ToString());
if (i == GridView1.PageIndex)
{
item.Selected = true;
}
pageList.Items.Add(item);
}
}
if (pageLabel != null)
{
int currentPage = GridView1.PageIndex + 1;
pageLabel.Text = "當(dāng)前頁: " + currentPage.ToString() +
" / " + GridView1.PageCount.ToString();
}
}
catch
{
Response.Write( "現(xiàn)在還沒有該部門的記錄!!");
}
}
如果發(fā)生異常,是應(yīng)為沒有數(shù)據(jù)。這時候可以在catch里面提示沒有數(shù)據(jù)。這樣就可以綁定頁碼了。
但是這樣做的條件是沒有把綁定數(shù)據(jù)放在 (!ispostback)里面,如果放在里面了,就會出問題
因?yàn)轫撁娴拇a實(shí)際上是控制顯示第幾頁。但是綁定數(shù)據(jù)放在了!ispostback里面了,所以不能重新綁定。
這樣的情況,要寫那個綁定的方法。
然后在cs文件里寫下面的代碼:
protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[ 0].FindControl("PageDropDownList");
GridView1.PageIndex = pageList.SelectedIndex;
gridviewband(); ///重新綁定數(shù)據(jù),這是特殊的地方
}
protected void CustomersGridView_DataBound(Object sender, EventArgs e)
{
try
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
LinkButton linkBtnFirst = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnFirst");
LinkButton linkBtnPrev = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnPrev");
LinkButton linkBtnNext = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnNext");
LinkButton linkBtnLast = (LinkButton)pagerRow.Cells[ 0].FindControl("linkBtnLast");
if (GridView1.PageIndex == 0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
}
else if (GridView1.PageIndex == GridView1.PageCount - 1)
{
linkBtnLast.Enabled = false;
linkBtnNext.Enabled = false;
}
else if (GridView1.PageCount <= 0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
linkBtnNext.Enabled = false;
linkBtnLast.Enabled = false;
}
DropDownList pageList = (DropDownList)pagerRow.Cells[ 0].FindControl("PageDropDownList");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
if (pageList != null)
{
for (int i = 0; i < GridView1.PageCount; i ++)
{
int pageNumber = i + 1;
ListItem item = new ListItem(pageNumber.ToString() + "/" + GridView1.PageCount.ToString(), pageNumber.ToString());
if (i == GridView1.PageIndex)
{
item.Selected = true;
}
pageList.Items.Add(item);
}
}
if (pageLabel != null)
{
int currentPage = GridView1.PageIndex + 1;
pageLabel.Text = "當(dāng)前頁: " + currentPage.ToString() +
" / " + GridView1.PageCount.ToString();
}
}
catch //(Exception ex5)
{
Response.Write( "現(xiàn)在還沒有該部門的記錄!!");
}
}
protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
gridviewband(); //重新綁定數(shù)據(jù)
}
這樣就可以顯示了 如果按照前面的方法會提示沒有 pageindexchanging事件。