PDA

View Full Version : کنترلهای داینامیک و Viewstate



maryam_80
پنج شنبه 09 اسفند 1386, 09:33 صبح
با سلام

سوالی که من دارم تکراری هست و با وجود اینکه درسایت هم جستجو کردم نتونستم مشکلم را حل کنم

من یک usercontrol ایجاد کرده ام که شامل کنترلهای داینامیک می باشد به این صورت که با load شدن فرم من یک سطر به جدولم اضافه میکنم که ستونهای آن شامل dropdownlist و button هایی می باشد که به صورت داینامیک ایجاد شده اند .
اگر کاربر بر روی یکی از این button ها (btn_add) کلیک بکند من می خواهم یک سطر دیگر به جدولم اضافه کنم و درون ستونهای آن مثل سطر اول کنترلهای داینامیک قرار بدهم (در اصل به ازا هر کلیک بر روی Btn-add من می خوام یک کپی از سطر اول جدول ایجاد کنم )

در مورد چگونگی ایجاد کنترل داینامیک مشکلی ندارم ،مشکل من زمانی هست که من به button ها رویداد click را اضافه می کنم .
با کلیک بر روی این دکمه نه تنها سطر جدیدی ایجاد نمی شود بلکه تمام کترلهای داینامیک من از روی صفحه پاک می شوند .
من این موضوع را در سایت جستجو کردم ،در اغلب آنها به Viewstate اشاره شده است
مثل تاپیک زیر:
http://barnamenevis.org/forum/showthread.php?t=91389

مشکل من (من بیسواد:ناراحت: ) این هست که نمی دونم
اولا در کجای کدم و چجوری باید مقادیر جاری فرمم را در Viewstate ذخیره کنم
و ثانیا چه جوری از LoadViewState باید استفاده کنم
این کد کامل من برای usercontrol مربوطه می باشد



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;
namespace LangleySunWorx.Controls
{
public partial class WorkOrderSearch : System.Web.UI.UserControl
{
int number = 1;
DropDownList[] _SearchByArray = new DropDownList[20]; //SearchBy Dropdown List
DropDownList[] _FilterByArray = new DropDownList[20]; //FilterBy DropDown List

DropDownList[] _DdlValueArray = new DropDownList[20]; //for fields which are drop and selected from SearchBy
TextBox[] _TxtValueArray = new TextBox[20];//for fields which are text and selected from SearchBy
TextBox[] _TxtDateValueArray = new TextBox[20];//for fields which are Date and selected from SearchBy
Image[] _ImgBtnCalendarArray = new Image[20];//Image for fields which are Date and selected from SearchBy
CalendarExtender[] _CalendarExtenderArray = new CalendarExtender[20];
Button[] _BtnAddArray = new Button[20];
Button[] _BtnRemoveArray = new Button[20];



public string [] SearchByText= new string[] {
"Txt_Order#","Cmb_Job#", "Cmb_Status", "Dat_Date Created","Dat_Date Required",
"Dat_Date Started","Dat_Date Completed","Txt_Pole#","Txt_House#","Txt_Location",
"Txt_Priority","Txt_Parent Order#","Txt_System User","Txt_Originator","Cmb_Originator Type",
"Txt_Originator Name","Cmb_Problem Code","Cmb_Billing Rate","Cmb_City Area","Txt_Map Reference",
"Txt_Occurence#","Cmb_Request Locates","Cmb_Request Permits","Cmb_Restoration","Cmb_Police Services" };

public string[] SearchByValue= new string[] {
"WorkorderID","JobID", "StatusID", "DateCreated","DateRequired",
"DateStarted","DateCompleted","PoleNumber","HouseNumber","Location",
"PriorityID","ParentWorkOrderID","SystemUserID","OriginatorID","OriginatorType",
"OriginatorName","ProblemCodeID","BillingRateID","CityArea","MapReference",
"OccurenceNumber","flagRequestLocates","flagRequestPermits","flagIsRestoration","flagPoliceServices" };

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InsertComponet(number);
this.Controls.Add(Table11);
}

}
protected void InsertComponet(int Index)
{
InsertRow(number);
FillSearchBy(number);
FillFilterBy(number);
FillAddRemoveButton(number);
}
protected void InsertRow(int RowNumber)
{
HtmlTableRow tRow = new HtmlTableRow();
HtmlTableCell[] _CellArray = new HtmlTableCell[6];

for (int i = 0; i <= 5; i++)
{
_CellArray[i] = new HtmlTableCell();

}

Table11.Rows.Insert(RowNumber, tRow);
Table11.Rows[RowNumber].Cells.Insert(0, _CellArray[0]);
Table11.Rows[RowNumber].Cells.Insert(1, _CellArray[1]);
Table11.Rows[RowNumber].Cells[1].InnerText = "Search By ";
Table11.Rows[RowNumber].Cells.Insert(2, _CellArray[2]);
Table11.Rows[RowNumber].Cells.Insert(3, _CellArray[3]);
Table11.Rows[RowNumber].Cells.Insert(4, _CellArray[4]);
Table11.Rows[RowNumber].Cells.Insert(5, _CellArray[5]);

}
protected void FillSearchBy(int Index)
{
_SearchByArray[Index] = new DropDownList();
_SearchByArray[Index].ID = _SearchByArray[Index].ToString();
_SearchByArray[Index].Width = 150;
_SearchByArray[Index].EnableViewState = true;
_SearchByArray[Index].AutoPostBack = true;
_SearchByArray[Index].Items.Clear();
for (int i = 0; i <= SearchByText.Length - 1; i++)
{
_SearchByArray[Index].Items.Add(new ListItem(SearchByText[i].Substring(4), SearchByValue[i]));
}
_SearchByArray[Index].SelectedIndexChanged += new System.EventHandler(this.SelectedIndexChanged);
Table11.Rows[Index].Cells[1].Controls.Add(_SearchByArray[Index]);
}
protected void FillFilterBy(int Index)
{
_FilterByArray[Index] = new DropDownList();
_FilterByArray[Index].ID = _FilterByArray[Index].ToString();
_FilterByArray[Index].Width = 150;
_FilterByArray[Index].Items.Clear();

Table1.Rows[Index].Cells[2].Controls.Add(_FilterByArray[Index]);
}
protected void FillAddRemoveButton(int Index)
{
_BtnAddArray[Index] = new Button();
_BtnAddArray[Index].ID = _BtnAddArray[Index].ToString();
_BtnAddArray[Index].CssClass = "button";
_BtnAddArray[Index].Text = "ADD";
_BtnAddArray[Index].Width = 60;
_BtnAddArray[Index].Click += new System.EventHandler(this.ClickHandler);

Table11.Rows[Index].Cells[4].Controls.Add(_BtnAddArray[Index]);

_BtnRemoveArray[Index] = new Button();
_BtnRemoveArray[Index].ID = _BtnRemoveArray[Index].ToString();
_BtnRemoveArray[Index].Text = "Remove";
_BtnRemoveArray[Index].Width = 60;
_BtnRemoveArray[Index].CssClass = "button";
Table11.Rows[Index].Cells[4].Controls.Add(_BtnRemoveArray[Index]);


}

public void ClickHandler(object sender, System.EventArgs e)
{
number += 1;
InsertComponet(number );
}


protected void ddlSearchBy_SelectedIndexChanged(object sender, EventArgs e)
{
Table1.Rows[1].Cells[3].Controls.Clear();
switch (SearchByText[ddlSearchBy.SelectedIndex].Substring(0, 3))
{
case "Txt":

TextBox Txt = new TextBox();
Txt.ID = "Txt";
Table1.Rows[1].Cells[3].Controls.Add(Txt);
Txt.Text = SearchByText[ddlSearchBy.SelectedIndex].Substring(4);
Txt.Width = 150;
break;
case "Cmb":
DropDownList Cmb = new DropDownList();
Cmb.ID = "Cmb";
Table1.Rows[1].Cells[3].Controls.Add(Cmb);
Cmb.Text = "fdfdfdfd";// SearchByText[ddlSearchBy.SelectedIndex].Substring(4);
Cmb.Width = 150;
break;
case "Dat":
//Create TextBox for Date
TextBox TxtDate = new TextBox();
TxtDate.ID = "TxtDate";
Table1.Rows[1].Cells[3].Controls.Add(TxtDate);

//Create Image for Calendar Image
Image ImgBtnCalendar = new Image();
ImgBtnCalendar.ID = "ImgBtnCalendar";
ImgBtnCalendar.ImageUrl = "~/imgs/Calendar.ico";
Table1.Rows[1].Cells[3].Controls.Add(ImgBtnCalendar);

//Create CalendarExtender
AjaxControlToolkit.CalendarExtender CalendarExtender = new AjaxControlToolkit.CalendarExtender();
CalendarExtender.ID = "CalendarExtender";
CalendarExtender.TargetControlID = "TxtDate";
CalendarExtender.CssClass = "RedCalendar";
CalendarExtender.Format = "d";
CalendarExtender.PopupButtonID = "ImgBtnCalendar";
Table1.Rows[1].Cells[3].Controls.Add(CalendarExtender);
break;
}
}

//Dynamic Events
protected void SelectedIndexChanged(object sender, EventArgs e)
{
switch (SearchByText[((DropDownList)sender).SelectedIndex].Substring(0, 3))
{
case "Txt":
TextBox Txt = new TextBox();
Txt.ID = "Txt";
Table11.Rows[number].Cells[3].Controls.Add(Txt);
Txt.Text = SearchByText[((DropDownList)sender).SelectedIndex].Substring(4);
Txt.Width = 150;
break;

}
}

}


ممنون می شوم اساتید من را به صورت مشخص راهنمایی کنند و اگر نمونه کد و ... دارند در اختیارم بذارند

maryam_80
پنج شنبه 09 اسفند 1386, 09:45 صبح
من هر کاری کردم نتونستم تاپیک را ویرایش کنم تا کدم را تصحیح کنم
فرم من به شکل زیر هست



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;
namespace LangleySunWorx.Controls
{
public partial class WorkOrderSearch : System.Web.UI.UserControl
{
int number = 1;
DropDownList[] _SearchByArray = new DropDownList[20]; //SearchBy Dropdown List
DropDownList[] _FilterByArray = new DropDownList[20]; //FilterBy DropDown List
DropDownList[] _DdlValueArray = new DropDownList[20]; //for fields which are drop and selected from SearchBy
TextBox[] _TxtValueArray = new TextBox[20];//for fields which are text and selected from SearchBy
TextBox[] _TxtDateValueArray = new TextBox[20];//for fields which are Date and selected from SearchBy
Image[] _ImgBtnCalendarArray = new Image[20];//Image for fields which are Date and selected from SearchBy
CalendarExtender[] _CalendarExtenderArray = new CalendarExtender[20];
Button[] _BtnAddArray = new Button[20];
Button[] _BtnRemoveArray = new Button[20];

public string [] SearchByText= new string[] {
"Txt_Order#","Cmb_Job#", "Cmb_Status", "Dat_Date Created","Dat_Date Required",
"Dat_Date Started","Dat_Date Completed","Txt_Pole#","Txt_House#","Txt_Location",
"Txt_Priority","Txt_Parent Order#","Txt_System User","Txt_Originator","Cmb_Originator Type",
"Txt_Originator Name","Cmb_Problem Code","Cmb_Billing Rate","Cmb_City Area","Txt_Map Reference",
"Txt_Occurence#","Cmb_Request Locates","Cmb_Request Permits","Cmb_Restoration","Cmb_Police Services" };
public string[] SearchByValue= new string[] {
"WorkorderID","JobID", "StatusID", "DateCreated","DateRequired",
"DateStarted","DateCompleted","PoleNumber","HouseNumber","Location",
"PriorityID","ParentWorkOrderID","SystemUserID","OriginatorID","OriginatorType",
"OriginatorName","ProblemCodeID","BillingRateID","CityArea","MapReference",
"OccurenceNumber","flagRequestLocates","flagRequestPermits","flagIsRestoration","flagPoliceServices" };
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InsertComponet(number);
this.Controls.Add(Table11);
}

}
protected void InsertComponet(int Index)
{
InsertRow(number);
FillSearchBy(number);
FillFilterBy(number);
FillAddRemoveButton(number);
}
protected void InsertRow(int RowNumber)
{
HtmlTableRow tRow = new HtmlTableRow();
HtmlTableCell[] _CellArray = new HtmlTableCell[6];
for (int i = 0; i <= 5; i++)
{
_CellArray[i] = new HtmlTableCell();
}
Table11.Rows.Insert(RowNumber, tRow);
Table11.Rows[RowNumber].Cells.Insert(0, _CellArray[0]);
Table11.Rows[RowNumber].Cells.Insert(1, _CellArray[1]);
Table11.Rows[RowNumber].Cells[1].InnerText = "Search By ";
Table11.Rows[RowNumber].Cells.Insert(2, _CellArray[2]);
Table11.Rows[RowNumber].Cells.Insert(3, _CellArray[3]);
Table11.Rows[RowNumber].Cells.Insert(4, _CellArray[4]);
Table11.Rows[RowNumber].Cells.Insert(5, _CellArray[5]);
}
protected void FillSearchBy(int Index)
{
_SearchByArray[Index] = new DropDownList();
_SearchByArray[Index].ID = _SearchByArray[Index].ToString();
_SearchByArray[Index].Width = 150;
_SearchByArray[Index].EnableViewState = true;
_SearchByArray[Index].AutoPostBack = true;
_SearchByArray[Index].Items.Clear();
for (int i = 0; i <= SearchByText.Length - 1; i++)
{
_SearchByArray[Index].Items.Add(new ListItem(SearchByText[i].Substring(4), SearchByValue[i]));
}
_SearchByArray[Index].SelectedIndexChanged += new System.EventHandler(this.SelectedIndexChanged);
Table11.Rows[Index].Cells[1].Controls.Add(_SearchByArray[Index]);
}
protected void FillFilterBy(int Index)
{
_FilterByArray[Index] = new DropDownList();
_FilterByArray[Index].ID = _FilterByArray[Index].ToString();
_FilterByArray[Index].Width = 150;
_FilterByArray[Index].Items.Clear();
Table1.Rows[Index].Cells[2].Controls.Add(_FilterByArray[Index]);
}
protected void FillAddRemoveButton(int Index)
{
_BtnAddArray[Index] = new Button();
_BtnAddArray[Index].ID = _BtnAddArray[Index].ToString();
_BtnAddArray[Index].CssClass = "button";
_BtnAddArray[Index].Text = "ADD";
_BtnAddArray[Index].Width = 60;
_BtnAddArray[Index].Click += new System.EventHandler(this.ClickHandler);
Table11.Rows[Index].Cells[4].Controls.Add(_BtnAddArray[Index]);
_BtnRemoveArray[Index] = new Button();
_BtnRemoveArray[Index].ID = _BtnRemoveArray[Index].ToString();
_BtnRemoveArray[Index].Text = "Remove";
_BtnRemoveArray[Index].Width = 60;
_BtnRemoveArray[Index].CssClass = "button";
Table11.Rows[Index].Cells[4].Controls.Add(_BtnRemoveArray[Index]);

}
public void ClickHandler(object sender, System.EventArgs e)
{
number += 1;
InsertComponet(number );
}

protected void ddlSearchBy_SelectedIndexChanged(object sender, EventArgs e)
{
Table1.Rows[1].Cells[3].Controls.Clear();
switch (SearchByText[ddlSearchBy.SelectedIndex].Substring(0, 3))
{
case "Txt":
TextBox Txt = new TextBox();
Txt.ID = "Txt";
Table1.Rows[1].Cells[3].Controls.Add(Txt);
Txt.Text = SearchByText[ddlSearchBy.SelectedIndex].Substring(4);
Txt.Width = 150;
break;
case "Cmb":
DropDownList Cmb = new DropDownList();
Cmb.ID = "Cmb";
Table1.Rows[1].Cells[3].Controls.Add(Cmb);
Cmb.Text = "fdfdfdfd";// SearchByText[ddlSearchBy.SelectedIndex].Substring(4);
Cmb.Width = 150;
break;
case "Dat":
//Create TextBox for Date
TextBox TxtDate = new TextBox();
TxtDate.ID = "TxtDate";
Table1.Rows[1].Cells[3].Controls.Add(TxtDate);
//Create Image for Calendar Image
Image ImgBtnCalendar = new Image();
ImgBtnCalendar.ID = "ImgBtnCalendar";
ImgBtnCalendar.ImageUrl = "~/imgs/Calendar.ico";
Table1.Rows[1].Cells[3].Controls.Add(ImgBtnCalendar);
//Create CalendarExtender
AjaxControlToolkit.CalendarExtender CalendarExtender = new AjaxControlToolkit.CalendarExtender();
CalendarExtender.ID = "CalendarExtender";
CalendarExtender.TargetControlID = "TxtDate";
CalendarExtender.CssClass = "RedCalendar";
CalendarExtender.Format = "d";
CalendarExtender.PopupButtonID = "ImgBtnCalendar";
Table1.Rows[1].Cells[3].Controls.Add(CalendarExtender);
break;
}
}
//Dynamic Events
protected void SelectedIndexChanged(object sender, EventArgs e)
{
switch (SearchByText[((DropDownList)sender).SelectedIndex].Substring(0, 3))
{
case "Txt":
TextBox Txt = new TextBox();
Txt.ID = "Txt";
Table11.Rows[number].Cells[3].Controls.Add(Txt);
Txt.Text = SearchByText[((DropDownList)sender).SelectedIndex].Substring(4);
Txt.Width = 150;
break;
}
}
}

maryam_80
پنج شنبه 09 اسفند 1386, 17:38 عصر
اساتید من منتظر راهنمایی شما هستم

asgari2005
پنج شنبه 09 اسفند 1386, 18:18 عصر
دوست گرامی اگر شما User Control را به صورت داینامکی به فرم اضافه می کنید نیاز هستش که خارج از PostBack قرار دهید تا درهر بار عمل کلیک کردن بر دکمه یوزر کنترل فوق به فرم اضافه کنه این مشکل زمانی ایجاد می شود که یوزر کنترل به صورت داینامیکی اضافه گردد و ViewState آن حفظ نمی شود اگر همین یوزر کنترل را در زمان طراحی اضافه کنید می بینید که ViewState اون حفظ می شود و مشکل ندارد

maryam_80
پنج شنبه 09 اسفند 1386, 19:14 عصر
ممنون از توجه شما
من نمی دونم که چه طورمقادیر کنترلها را در VIEWSTATE ذخیره کنم و منظور از فراخوانی خارج از POSTBACKچی هست

maryam_80
جمعه 10 اسفند 1386, 08:36 صبح
یعنی هیچ کسی نمیتونه به من کمک کند من تازه از vb.net windows application به c# web application سوئیچ کرده ام و در این یک مورد شدیدا به راهنمایی احتیاج دارم

maryam_80
یک شنبه 12 اسفند 1386, 19:26 عصر
با سلام

من مشکل نگه داشتن کنترلها در زمان postback را برطرف کردم

ولی الان یک مشکل جدید دارم
من 1 dropdown دارم که به صورت داینامیک روی فرم قرار می گیرد .با تغییر selectedvalue آن من ddl2 را ایجاد می کنم حالا زمانیکه فرم postback میشه من روی فرم dd1 را با مقدار انتخاب شده دارم ولی ddl2 از بین می رود (چون زمانی ساخت می شود که من dd1.selectedvalue را اجرا کنم )

چه طور می توان رویدادهایی مثل selectedvalue را با هر postback روی فرم نگه داشت .

maryam_80
جمعه 17 اسفند 1386, 09:33 صبح
با سلام

من قبلا در همین تاپیک مشکلم را مطرح کرده بودم .
با کمی تغییر در نحوه ساخت کنترلها ، الان با کلیک بر دکمه add می توانم به راحتی به html table سطر اضافه کنم و با هر postback هم کنترلها و مقادیرشان بر روی فرم باقی می ماند

و ام__________________________________________ا مشکلات جدید:

1)) من علاوه بر دکمه add یک دکمه هم برای حذف سطر از جدول دارم .به این ترتیب که هر زمان کاربر بخواهد به search یک شرط دیگر اضافه کند کلید add را می زند و اگر بخواهد شرط مربوطه را حذف کند بر روی remove کلیک می کند.
با هر بار کلیک بر روی remove یک عمل postback اتفاق می افتد که در نتیجه برنامه ابتدا به سراغ روال LoadViewState رفته و تمام کنترلها را recreate میکند بعد می رود و دستورات درون event remove را اجرا می کند که نتیجه آن حذف کنترل و تولید یک سری کنترل اضافی می باشد.
راه حل چیست؟؟

2)) از آنجاییکه تمام کنترلها dynamic هستند ،در viewstate ذخیره می شوند ،در نتیجه زمانیکه کاربر به یک صفحه دیگر برود و دوباره به این صفحه برگردد تمام اطلاعات از بین می رود.آیا میتوان تمام کنترلها و مقادیر آنها را در session ذخیره کرد (منظورم مثلا ذخیره dropdownlist control و مقدار selectedvalue آن و .....)

این هم سورس آخرین تغییرات چون زیاد بود attach کردم

ممنون میشوم یک نظر به کد داشته باشید و ببینید مشکل از کجاست؟موضوع برای من خیلی حیاتی هست

anubis_ir
جمعه 17 اسفند 1386, 09:53 صبح
سلام
در مورد حفظ view state که هنوز با آن مشکل دارید:

Property values are only saved in Viewstate if control was added to parent
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx