2013-08-23 53 views
0

我已經創建了動態控件,但通過動態控件插入數據時出現錯誤。 下面是我嘗試過但無法修復的代碼。 您的合作將受到高度讚賞。如何通過動態控件將數據保存在數據庫中

public int NumberOfControls 
{ 
    get 
    { 
     if (ViewState["Count"] == null) 
     { 
      return 0; 
     } 
     return (int)ViewState["Count"]; 
    } 
    set 
    { 
     ViewState["Count"] = value++; 
    } 
} 

private void CreateTextBoxes() 
{ 
    for (int counter = 0; counter <= NumberOfControls; counter++) 
    { 
     DropDownList ddl = new DropDownList(); 
     ddl.Width = 150; 
     ddl.Height = 18; 
     ddl.ID = "Ddl_Item" + (counter + 1).ToString(); 
     dvContainer.Controls.Add(ddl); 
     string sql = "select * from INV.GOODS_MASTER"; 
     SqlDataAdapter adp = new SqlDataAdapter(sql,cc); 
     DataTable dt=new DataTable(); 
     adp.Fill(dt); 
     if (dt.Rows.Count > 0) 
     { 
      ddl.Items.Clear(); 
      ddl.DataSource = dt; 
      ddl.DataTextField = "GM_NAME"; 
      ddl.DataValueField = "GM_ID"; 
      ddl.DataBind(); 
      ddl.Items.Insert(0, new ListItem("---Select---", string.Empty)); 
     } 

     TextBox tb = new TextBox(); 
     tb.Width = 150; 
     tb.Height = 18; 
     tb.TextMode = TextBoxMode.SingleLine; 
     tb.ID = "Txt_Quantits" + (counter + 1).ToString(); 
     // add some dummy data to textboxes 
     tb.Text = "Enter Title " + counter; 
     dvContainer.Controls.Add(tb); 
     //phTextBoxes.Controls.Add(new LiteralControl("<br/>")); 


     DropDownList ddl1 = new DropDownList(); 
     ddl1.Width = 150; 
     ddl1.Height = 18; 
     ddl1.ID = "Ddl_Unit" + (counter + 1).ToString(); 
     dvContainer.Controls.Add(ddl1); 
     string sql1 = "select * from INV.UNITMASTER"; 
     SqlDataAdapter adp1 = new SqlDataAdapter(sql1, cc); 
     DataTable dt1 = new DataTable(); 
     adp1.Fill(dt1); 
     if (dt1.Rows.Count > 0) 
     { 
      ddl1.Items.Clear(); 
      ddl1.DataSource = dt1; 
      ddl1.DataTextField = "UM_UNITNAME"; 
      ddl1.DataValueField = "UM_ID"; 
      ddl1.DataBind(); 
      ddl1.Items.Insert(0, new ListItem("---Select---", string.Empty)); 
     } 

     TextBox tb1 = new TextBox(); 
     tb1.Width = 150; 
     tb1.Height = 18; 
     tb1.TextMode = TextBoxMode.SingleLine; 
     tb1.ID = "Txt_Availabl" + (counter + 1).ToString(); 
     // add some dummy data to textboxes 
     tb.Text = "Enter Title " + counter; 
     dvContainer.Controls.Add(tb1); 
     //phTextBoxes.Controls.Add(new LiteralControl("<br/>")); 
    } 
} 

private void ReadTextBoxes() 
{ 
    strValue = string.Empty; 

    int n = NumberOfControls; 

    for (int i = 0; i <= NumberOfControls; i++) 
    { 
     string boxName = "Ddl_Item" + (i + 1).ToString(); 
     DropDownList ddl = dvContainer.FindControl(boxName) as DropDownList; 
     strValue += ddl.Text + "\n"; 

     string boxName1 = "Txt_Quantits" + (i + 1).ToString(); 
     TextBox tb = dvContainer.FindControl(boxName1) as TextBox; 
     strValue += tb.Text + "\n"; 

     string boxName2 = "Ddl_Unit" + (i + 1).ToString(); 
     DropDownList ddl2 = dvContainer.FindControl(boxName2) as DropDownList; 
     strValue += ddl2.Text + "\n"; 

     string boxName3 = "Txt_Availabl" + (i + 1).ToString(); 
     TextBox tb1 = dvContainer.FindControl(boxName3) as TextBox; 
     strValue += tb1.Text + "\n"; 
    } 
    Response.Write(strValue); 
} 

protected override void CreateChildControls() 
{ 
    // Here we are recreating controls to persist the ViewState on every post back 
    if (Page.IsPostBack) 
    { 
     //NumberOfControls += 1; 
     CreateTextBoxes(); 
    } 
    else 
    { 
     CreateTextBoxes(); 
     // Increase the control value to 1 
     NumberOfControls = 0; 
    } 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    NumberOfControls += 1; 
    //CreateTextBoxes(); 
} 

private void StockEntryBind(string mode) 
{ 
    string sql = "[INV].[SP_StockEntry]"; 
    SqlCommand cmd = new SqlCommand(sql, cc); 
    cmd.Parameters.AddWithValue("@StockEntryId", HiddenField1.Value); 
    cmd.Parameters.AddWithValue("@Date", DateTime.Parse(txt_date.Text)); 
    cmd.Parameters.AddWithValue("@Time", txt_time.Text); 
    cmd.Parameters.AddWithValue("@Godown", Ddl_Godown.SelectedItem.Text); 

    //Error coming below 
    DropDownList ddl = new DropDownList(); 
    ddl.ID = "Ddl_Item"; 
    cmd.Parameters.AddWithValue("@Item", ddl.SelectedItem.Text); 

    TextBox tb = new TextBox(); 
    tb.ID = "Txt_Quantits"; 
    cmd.Parameters.AddWithValue("@Quantity", tb.Text); 

    DropDownList ddl1 = new DropDownList(); 
    ddl1.ID = "Ddl_Unit"; 
    cmd.Parameters.AddWithValue("@Unit", ddl1.SelectedItem.Text); 

    TextBox tb1 = new TextBox(); 
    tb1.ID = "Txt_Availabl"; 
    cmd.Parameters.AddWithValue("@AvailableStock", tb1.Text); 

    cmd.Parameters.AddWithValue("@mode", mode); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.ExecuteNonQuery(); 
} 
protected void Button2_Click(object sender, EventArgs e) 
{ 
    StockEntryBind("Insert"); 
    string jScript = @"javascript: alert('Data has been Inserted successfully');"; 
    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "clientScript", jScript, true); 
} 

未找到動態控件ID來保存數據並顯示對象引用未設置爲對象的錯誤。

+0

你的錯誤是什麼? –

回答

0

如果您正在創建動態控件,則需要確保在回發後重新創建它們。這需要在視圖狀態和事件連接之前完成。創建控件的最佳位置是在Page_Init函數中,而不是Page_Load。

調查ASP.Net Page Life Cycle以查看頁面的構建方式。

相關問題