第一次發佈在這裏,所以如果有東西不適合,讓我知道。此外,我對ASP和C#不是很有經驗,所以如果我忽略了一些明顯的東西,我很抱歉。訪問所選項目後回發ASP
問題: 在Page_Load中,我調用我自己的類MyGridViewExtension。在這個類的構造函數中,將創建一個Gridview。問題是:在這個Gridview的頭文件中,不僅是一個文字,而且還是一個列表框。這些Listbox的使用是過濾顯示的數據,這是通過標記Listbox中的一個(或多個,但並不重要)選項,然後單擊回發按鈕來實現的。
我嘗試使用SelectedIndexChanged事件,但只有在Page_Load已完成後觸發,並且在我的GridView已創建完畢後調用構造函數。
//this is the selectedIndexChanged Event Handler
private void AddToFilterList(Object sender, EventArgs e){
ListBox source=sender as ListBox;
string attributeName=source.Parent.ID; //get column name
List<string> filterList=new List<string>();
foreach(ListItem singleFilter in Source.Items){
if(singleFilter.Selected==true){
filterList.Add(singleFilter.Text);
}
}
}
//This works
的問題是,該構造將完成AddToFilterList甚至被調用之前,事後也沒有幫助了,因爲我需要的時候filterList在構造函數中。
至於其他的代碼,它看起來有點像這樣:
public Class MyGridViewExtension(Array Data){
checkForSelectedOptions(); //how can I have my filterList here already?
List<string> columnNames=getAllColumnNamesByCheckingData(Data);
//-create the template field to show the data
foreach (string col in columnNames)
{
TemplateField tfield = new TemplateField();
//In here, the listboxes are created
tfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col, this);
tfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col, this);
this.Columns.Add(tfield);
}
this.DataSource=Data; //I actually transform the array to a datatable before, but that shouldn't matter here
}
protected void Page_Load(object sender, EventArgs e){
Array data=getDataFromWebserver(); //works
MyGridViewExtension thisNewGridView=new MyGridViewExtension(data);
thisNewGridView.DataBind();
divName.Controls.Add(thisNewGridView); //add the gridview to a div on the page
}
一切工作正常,獲取數據並顯示它,但我就是塊,我只是不能得到列表框的所選項目(filterList變量)添加到構造函數中。
編輯:我應該補充一點,我應該在page_load儘可能小的代碼,因爲我的工作只是擴展類和page_load中的每個條目必須(每次)當我的課是所謂的,這應該保持在最低限度。
在此先感謝您的答案,評論(和編輯,因爲我的帖子可能不如我希望的那樣好)。 我已經編輯過很多,因爲我忽略了一些重要的東西;對於那些已經試圖理解/回答的人感到抱歉。
最後編輯:我有點解決了問題,通過重新啓用整個GridView的ViewState,這會導致一些壓倒一切的問題。但是這些比這裏描述的問題更容易處理,所以這可能是更好的路線。
感謝大家給了小費。
創建自己定製的'GridView'的任何理由? – Bharadwaj
添加更多的過濾,排序和顯示功能,並給它一些特殊的變量(例如,能夠告訴它,它應該讓第3列顯示只是通過去「my ... thisview = new我的...(); thisview.columnToHide = 3;) 總體原因是一個webtool,程序員厭倦了爲每個不同的頁面/數據集創建這樣的函數,所以需要一個泛型類,可用 – LindenRathan
發生了什麼事情,你創建了自己的'GridView',這很好:)但是你重新創建並添加了每個'Page_Load',所以前面的'GridView'值你所想要的會丟失。 't get what what before before。 – Bharadwaj