2012-07-08 68 views
0

我有兩個用戶控件:dsucTopLeft和dsucTopRight在一個aspx頁面中。每個用戶控件都有一個下拉列表來從中選擇值。在autopostback上保留用戶控件asp.net

aspx頁面有一個「保存」按鈕。

在按鈕的OnClickEvent中,我從這些用戶控件(從下拉列表返回值)中獲取數據。我需要將這些值插入到數據庫中。但是,單擊該按鈕後,這些值設置爲0。

我將如何保存這些值?

代碼:

ParentTemplate.aspx

<div class="leftDashboard"> 
    <uc:dsuc ID="dsucTopLeft" runat="server" /> 
</div> 

<div id="rightDashboard"> 
    <uc:dsuc ID="dsucTopRight" runat="server" /> 
</div> 

它也有一個按鈕:

<asp:Button ID="SaveButton" runat="server" OnClick="SaveButton_Click" Text="Save Dashboard" /> 

這是該按鈕的隱藏代碼:

protected void SaveButton_Click(object sender, EventArgs e) 
    { 
     //If the mode is "CREATE", then it needs to insert the information about  dashboard and the charts it contains 
     for (int i = 0; i < dsuc.Length; i++) 
     { 
      dsuc[i].dashboardId = dashboardId; 
      if (dsuc[i].chartId != 0) //Here, the chartId remains 0 
       dsuc[i].insert(); 
     } 
    } 

dsuc是數組。它被填充以如下方式:

​​

用戶控件:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      initializeDropDownList();//It initializes the drop down list 
     } 
    } 

public void insert() 
{ 
    //It inserts into database 
} 

protected void chartDDLSelectedIndexChanged(object sender, EventArgs e) 
    { 
     oldChartId = chartId; 
     String chartTitle = chartDropDownList.SelectedItem.Text; 
     if (chartTitle != dropDownMessage) 
     { 
      chartId = int.Parse(chartDropDownList.SelectedItem.Value); 
      //Chart user control should be retrieved from the database and drawn. 
      chartTitleLabel.Text += "chart id : " + chartId.ToString() + " title: " + chartTitle; 
      //chartUserControl.Visible = true; 
     } 
    } 

用戶控件即類dsuc的[]當選擇在其下拉列表中的項目改變其chartId。我打印的價值,它的作品。但是當點擊按鈕時,相同的值變爲0。

+0

如何添加控件?你可以把代碼? – ivowiblo 2012-07-08 06:59:32

+0

我添加了代碼。 – coolscitist 2012-07-08 07:11:08

+0

你如何填充'dsuc'數組? – ivowiblo 2012-07-08 07:13:08

回答

0

取出if (!Page.IsPostBack)

if (!Page.IsPostBack) 
    { 
     dsuc[0]=dsucTopLeft; 
     dsuc[1]=dsucTopRight; 
    } 

如果沒有,那就只能填充它時,不回發。當點擊產生一個回貼,你會想要執行該代碼:)

此外,它可能是你的clickp event is being called before selectedindexchanged`。在這種情況下,設置chartId的代碼在觸發click事件時不會被執行(尚)。你可以解決這個問題:

public int ChartId { get { return int.Parse(chartDropDownList.SelectedItem.Value); }} 

而是調用該屬性。另外,您可以在您的selectedindexchanged事件處理程序中使用該屬性。

+0

chartId保持爲0.不保留用戶控件的屬性值。我將如何保存它們? – coolscitist 2012-07-08 08:47:51

+0

查看我的更新,我添加了一個處理該問題的方法 – ivowiblo 2012-07-08 18:08:09