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;
}
}
}
ممنون می شوم اساتید من را به صورت مشخص راهنمایی کنند و اگر نمونه کد و ... دارند در اختیارم بذارند
سوالی که من دارم تکراری هست و با وجود اینکه درسایت هم جستجو کردم نتونستم مشکلم را حل کنم
من یک 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;
}
}
}
ممنون می شوم اساتید من را به صورت مشخص راهنمایی کنند و اگر نمونه کد و ... دارند در اختیارم بذارند