如何在WebGridView中,讓User自訂OrderBy的次序呢? (我在InfoCommand用了Order By,就固定住了)
Client端的資料是依據server 端的SQL選擇取來的,所以會依據InfoComamd的order by來排序,Client端如果需要重新排序,則要通過WebGridView.Sort() 方法來實現。可在WebGridView_Load事件中撰寫以下程式:
GridView.Sort(string sortExpression, SortDirection sortDirection);
WebGridView可以自已寫code篩選要呈現的資料嗎?哪個事件下可以運用呢?
WebGridView的資料來源是WebDataSource,您可以直接對WebDataSource的innerDataSet的Table資料做過濾就可以.
或者換一種方法:
1. 加一個WebDataSource,關聯到一個WebDataSet(Server要對應一個InfoCommand)
2. 您可以在適當的時機對WebDataSource執行SetWhere,比如SetWhere("orderid =’18259’")例如:Formview的PageIndexChanged事件。
如何在WebGridView自行帶值的方法。
可以利用RowDataBound的事件來設定欄位的初值, 如下:
protected void WebGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//做Default操作(Insert時使用)
if (e.Row.RowType == DataControlRowType.Footer && WebGridView1.ShowFooter)
{
Control ctrl = e.Row.FindControl("TextBox1");
if (ctrl != null && ctrl is TextBox)
{
TextBox txt = (TextBox)ctrl;
txt.Text = "abc";
}
}
//做Default操作(Update時使用)
if (e.Row.RowType == DataControlRowType.DataRow)
{
Control ctrl = e.Row.FindControl("TextBox1");
if (ctrl != null && ctrl is TextBox)
{
TextBox txt = (TextBox)ctrl;
txt.Text = "abc";
}
}
}
GridView 的Insert 事件要寫在那裡,如何去判斷兩個欄位結束日期是否比開始日期還要大?
可以通過WebGridView的RowInserting來寫
WebGridView1_RowInserting(Object sender, GridViewUpdateEventArgs e)
{
DateTime timestart = e.NewValues["timestart"];
DateTime timeend = e.NewValues["timeend"];
if(timeend < tiemstart)
{
e.Cancel = true;
}
}
WebGridView刪除的確認.
可以在WebGridView的OnClientClick屬性中,設定以下Java Script內容即可做到。return confirm('sure to return?')
DataGirdView中如何控制欄位的輸入長度限制
可通過設定GridView的Columns中的MaxInputLength屬性來限定長度。
如何設定 GridView下方頁數靠左邊對齊呢?
WebGridView的PagerStyle屬性的HorizontalAlign可完成此功能。
如何在WebGridView網頁中以HyperLink方式傳遞參數到另一個網頁?
在WebGridView中將Columns內的欄位, 改用HyperLinkFields的型態,設定DataTextField為綁定的欄位,如果你要HyperLink網頁沒有要傳參數,直接使用 NavigateURL即可;如果你要動態傳遞參數,則須設定DataNavigateURLFormatString連結到你要的HyperLink頁面,如: ~/TEST/W000F.aspx?CUST={0} ,連結到 W00F.asp,傳一個變數名稱CUST,內容則定義在DataNavigateURLFields,如設為CustomerID代表要傳CustomerID到CUST變數中。
最後在W000F.aspx中可以取得Cust變數並處理資料的對應:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitializeComponent();
Master.DataSource = WMaster;
string str = Page.Request.QueryString["Cust"]; //取得Cust的變數
Master.SetWhere("CustomerID='" + str + "'"); //設定為相對的資料.
}
}
WebDataSource的EnableViewState可以設為False嗎?
WebDataSource的EnableViewState是一定要設定為True的,這是因為WebDataSource的一些屬性是存在ViewState中,如果將ViewState設為False,會導致一些屬性在網頁返回時無法保存上一次的狀態,而導致意想不到的結果。
當Detail有資料列時,如何讓A欄位值為0時,設置該筆資料列為無法編輯?
可以在RowDataBound事件中處理,先將CommandField轉換成TemplateField,找到編輯按鈕的ID,參考代碼:
protected void wgvDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowIndex != wgvDetail.EditItemIndex)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string text = e.Row.Cells[1].Text; //如果A欄位不是TemplateField;
string text = (e.Row.FindControl("labelA") as Label).Text; //如果A欄位是TemplateField
if (text == "0")
{
ImageButton edit = (ImageButton)e.Row.FindControl("ImageButtonEdit");
if (edit != null)
{
edit.Visible = false;
}
}
}
}
}
是否可以依資料欄位內容,將該筆資料的顯示行背景顏色設定為指定顏色EX:欄位內容文字為A則為紅色,為B則為藍色?
可以在gridview的RowDataBound事件寫程式:
if (e.Row.RowType == DataControlRowType.DataRow)
{
//取得當前筆資料的某欄位元值以便比較.其中Cells[i]的i為欄位在GridView中的次序
string ss = e.Row.Cells[3].Text;
if(ss == "your value")
{
e.Row.BackColor = Color.Red;
}
}
如何讓WebGridView即時刷新?
撰寫WebGridView.DataBind();
請問為何在WebGridView中新增日期資料存檔後會無法顯示出來?
請檢查以下地方:
1.是否有正確的Binding(DateTime型別請Binding到text,Varchar(8)請Binding到DateString) 。
2.AddNewRowControls是否有加入此欄位。
WebGridView自帶的有一個跳轉頁的功能,如果要做一個輸入框輸入頁碼,然後就跳轉到這一頁,該怎麼實現?
如果是想在網頁上放置一個Textbox用於輸入頁簽,改變gridview的當前頁,可以使用下面的方法:
webGridView.PageIndex = Convert.Int16(Textbox1.Text) - 1;
如果想嚴謹一點,可以先判斷一下Textbox的Text內容是否合法。
要如何使當WebCheckBox == True 時, GridView 不要進入修改狀態?
可以寫在WebGridView的RowDataBound事件中
public void WebGridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
WebCheckBox cb = (WebCheckBox)e.Row.FindControl("WebCheckBox1");
if (cb.Checked == true)
{
ImageButton btn = (ImageButton)e.Row.FindControl("ImageButton1");
btn.OnClientClick = "alert('單頭狀態巳確認,無法修改單身資料');return false;";
}
}
WebGridView排序後按下編輯跳新視窗內容會錯?
請檢查一下GridView的OpenEditUrlInServerMode的屬性是否為true,如果是請設為false。
請問一下如何取得WebgGidView裡被選擇的某一資料列?
WebGridView.SelectedRow就是選中的資料,可以用ebGridView.SelectedRow.FindControls(contrilid)(取得Template中的元件),或者WebGridView.SelectedRow.Cells[index]來取值。
Web的Detail如何隱藏關聯key欄位?
有兩種方法:
1. 將關聯key轉為TemplateField,然後使用VS本身的HidenField來bind。
2. 直接將WebGridView的該欄位的Visible設為false,然後設定一下WebGridView的DataKeyNames,只要設定Master跟Detail之間的關聯key就可以,不要設定Detail的其他key欄位。
如何在WebGridView 裡抓取欄位值?
1.更改可以在RowUpdating和RowUpdated事件中用:e.NewValues[fieldname], e.OldValues[fieldname]
2.RowDataBound事件也有e.Row.Cells[index]
3.其他地方可利用WebGridView.Rows[WebGridView.SelectedIndex].FindControls(controlname)讀到控制項,再轉換成相應類型來取值
注意:FindControls適用於比如轉換成TemplateField的欄位,你自己會重新貼控制項,比如TextBox,這裏的ControlName,就是TextBox的名字。
另外,也可以WebGridView.Rows[WebGridView.SelectedIndex].Cells[index],只要是Row,都可以採取Cells,或者findControls的方式。
如何設定 GridView 下方頁數 靠左邊對齊?
WebGridView的PagerStyle屬性HorizontalAlign。
怎樣用WebGridView實現類似AJAX的功能.即把WebGridView快速換頁或排序,且其他的元件都不會變動到?
方法:
1. 先把那些button的templateField 換成commandField。
2. 然後設定WebGridView的EnableSortingAndPagingCallbacks屬性、AllowPaging屬性和AllowSorting設為true。
3. 在html的第一行最後的%前,加上下面這行:SmartNavigation="True"。
WebGridView將範本列的TextBox設為ReadOnly後,保存時卻獲取不到這個欄位的值?
可以使用HiddenField控制項,也可以把原來的TextBox,改成HTML控制項,Input(Text),或者在Textbox的keyPress事件中用程式控制輸入無效,比如在Load事件寫:
(sender as TextBox).Attributes["onkeydown"] = "if(event.keyCode!=9) return false;";
(sender as TextBox).Attributes["oncontextmenu"] = "return false;";
以上是設定除tab外,其他輸入都無效,以及粘貼功能也要禁止。
如何實現在使用WebClientQuery1時,在Page_Load時先讓 GridView1 隱藏,等使用者按下Button1(查詢)後才讓GridView1 顯示.
1.WebGridView的visible=false
2.Page_Load撰寫:
WebClientQuery1.Show(Panel1)
3.Button1.OnClick撰寫:
WebClientQuery1.Execute(Panel1);
WebGridView1.Visible = true;
WebGridView1.DataBind();
是否有方法可以在 WebGridView 上按下Detail檔的 Update 鈕後就自動儲存?
不可以,.Master+Detail的機制就是只能由Master來存儲,Detail不能單獨存檔,不然會破壞整個機制。如果一定要這樣做,可以在GridView的RowUpdated的地方,執行WebDataSet.ApplyUpdates();如果目的只在於新增、修改及刪除 Detail 的資料而已,可以把 Master 的屬性設為唯讀即可。