我已经创建了动态控制,但是在通过动态控件插入数据时出错。 下面是我尝试但无法解决的代码。 您的合作将受到高度赞赏。
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)
如果要创建动态控件,则需要确保在帖子后面重新创建它们。这需要在viewstate和事件连接之前完成。创建控件的最佳位置是Page_Init函数,而不是Page_Load。
查看ASP.Net Page Life Cycle,了解网页的构建方式。