1,資料繫結查詢
這個些代碼比較通用.主要是為了理清思路.可以有不同的實現.SqlConnection sqlcon;
SqlCommand sqlcom;
string strConn = "Data Source=(local);Database=TestDB;Uid=sa;Pwd=123456"; //連接字串
string strsql = "select * from tablename where";
sqlcon = new SqlConnection(strConn);
//開啟資料連線
SqlDataAdapter myda = new SqlDataAdapter(strsql, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
//將結果綁定到指定DataSet中
myda.Fill(myds, "cuabase");
gridview1.DataSourceID = null;
gridview1.DataSource = myds;
gridview1.DataKeyNames = new string[] { "欄位名" };
//顯示並關閉串連
gridview1.DataBind();
sqlcon.Close();
2.自訂超級串連
GridView內建的編輯功能感覺很弱.一般都是點擊編輯後跳轉到相應的頁面進行修改.
首先編輯欄位添加列...
然後在屬性->DataNavigateUrlFeild添加要Get傳遞資料的欄位標誌
在DataNavgateUrlFromat 添加要掉轉頁面的格式
例如:page.aspx?cusid={0} 0為預留位置
在行為->NavigateUrl添加要跳轉頁面
要想在相應的介面接收資料則代碼如下.
string strName = HttpContext.Current.Request.QueryString["cusid"];
//long id = long.Parse(this.Request.QueryString["cusid"].ToString());
string strRes = "This is the response from the server:\r\n" + "Hello, " + strName + "!";
HttpContext.Current.Response.Clear();//清除緩衝區流中的所有內容輸出。
HttpContext.Current.Response.Write(strRes); //將資訊寫入 HTTP 響應輸出資料流。
HttpContext.Current.Response.Flush();//向用戶端發送當前所有緩衝的輸出。
HttpContext.Current.Response.End();//將當前所有緩衝的輸出發送到用戶端,停止該頁的執行,並引發EndRequest 事件(停止請求)。
3,在GridView中CheckBox的處理
(1)記錄的Check的選擇判斷,得到選擇記錄的索引protected void Button2_Click(object sender, EventArgs e)
{
TextBox2.Text = "";
for (int i = 0; i < gridview1.Rows.Count;i++ )
{
//i為GridView1 的第i行,j為GridView1的第j列
//CheckBox chk = (CheckBox)gridview1.Rows[i].Cells[j].FindControl("CheckBox2");
CheckBox chk = (CheckBox)gridview1.Rows[i].Cells[0].FindControl("CheckBox2");
if (chk.Checked==true)
{
TextBox2.Text = TextBox2.Text + (i+1).ToString();
}
}
}
(2)Check的全部選擇
添加Check改變事件,如果為選中狀態則將所有記錄的CheckBox全部選中,配合ajax效果最好
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
int i;
if (((CheckBox)sender).Checked)
{
for (i = 0; i < gridview1.Rows.Count; i++)
{
((CheckBox)gridview1.Rows[i].FindControl("CheckBox2")).Checked = true;//找到那一列模板的ID值
}
}
else
{
for (i = 0; i < gridview1.Rows.Count; i++)
{
((CheckBox)gridview1.Rows[i].FindControl("CheckBox2")).Checked = false;
}
}
}
今天就到這裡...這隻是上篇...不過隨著慢慢的瞭解可能還有中篇..下篇1,下篇2,下篇N......