如何使用 asp.net 在数据库中保存多个动态文本框和下拉列表值?
为此,我使用了以下代码:
public partial class StockEntry : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList Ddl_Item;
protected System.Web.UI.WebControls.TextBox Txt_Quantity;
protected System.Web.UI.WebControls.DropDownList Ddl_Unit;
protected System.Web.UI.WebControls.TextBox Txt_Available;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GoodMasterDetails();
UnitMasterDetails();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState["countTimes"] == null)
{
countTimes = 1;
}
else
{
countTimes = Convert.ToInt32(ViewState["countTimes"]);
}
for (int i = 0; i < countTimes; i++)
{
Ddl_Item = new DropDownList();
Ddl_Item.ID = "Ddl_Item";
Ddl_Item.Width = 150 + i;
Ddl_Item.Style[" margin-right"] = "14px";
Ddl_Item.Style[" margin-left"] = "10px";
BLL_StockEntry BLL = new BLL_StockEntry();
DataTable dt = new DataTable();
dt = BLL.GoodsMasterDetails();
if (dt.Rows.Count > 0)
{
Ddl_Item.DataSource = dt;
Ddl_Item.DataTextField = "GM_NAME";
Ddl_Item.DataValueField = "GM_ID";
Ddl_Item.DataBind();
Ddl_Item.Items.Insert(0, new ListItem("---Select---", string.Empty));
}
Txt_Quantity = new TextBox();
Txt_Quantity.ID = "Txt_Quantity" + countTimes;
Txt_Quantity.Width = 150 + i;
Txt_Quantity.Style[" margin-right"] = "14px";
//Txt_Quantity.Style[" margin-left"] = "0px";
Ddl_Unit = new DropDownList();
Ddl_Unit.ID = "Ddl_Unit" + countTimes;
Ddl_Unit.Width = 150 + i;
Ddl_Unit.Style[" margin-right"] = "14px";
Ddl_Unit.Style[" margin-left"] = "10px";
BLL_StockEntry BLLL = new BLL_StockEntry();
DataTable dt1 = new DataTable();
dt1 = BLLL.UnitsMasterDetails();
if (dt1.Rows.Count > 0)
{
Ddl_Unit.DataSource = dt1;
Ddl_Unit.DataTextField = "UM_UNITNAME";
Ddl_Unit.DataValueField = "UM_ID";
Ddl_Unit.DataBind();
Ddl_Unit.Items.Insert(0, new ListItem("---Select---", string.Empty));
}
Txt_Available = new TextBox();
Txt_Available.ID = "Txt_Available" + countTimes;
Txt_Available.Width = 150 + i;
Txt_Available.Style[" margin-right"] = "14px";
//Txt_Available.Style[" margin-left"] = "10px";
dvContainer.Controls.Add(Ddl_Item);
dvContainer.Controls.Add(Txt_Quantity);
dvContainer.Controls.Add(Ddl_Unit);
dvContainer.Controls.Add(Txt_Available);
}
countTimes = countTimes + 1;
ViewState.Add("countTimes", countTimes);
}
private void StockEntryBind(string mode)
{
PL.RESTURENT.PL_StockEntry objpl = new PL.RESTURENT.PL_StockEntry();
objpl.StockEntryId = HiddenField1.Value.ToString();
objpl.Date = DateTime.Parse(txt_date.Text);
objpl.Time = txt_time.Text;
objpl.Godown = Ddl_Godown.SelectedItem.Text;
objpl.Item = Ddl_Items.SelectedItem.Text;//static
string str = string.Empty;//dynamic
DropDownList ddlitem = (DropDownList)dvContainer.FindControl("Ddl_Item") as DropDownList;
str = ddlitem.SelectedItem.Text;
objpl.Quantity = Txt_Quantity.Text;
objpl.Unit = Ddl_Unit.SelectedItem.Text;
objpl.AvailableStock = Txt_Available.Text;
objpl.mode = mode;
BLL_StockEntry objbll = new BLL_StockEntry();
if (objbll.StockEntryTransaction(objpl))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "Alert('Pay Slip Inserted Succesfully...')", true);
}
}
我只尝试 (dropdownlist)Ddl_Item 在数据库中插入数据,但是
str = ddlitem.SelectedItem.Text;
错误:Object reference not set to an instance of an object
。
您的帮助将不胜感激。
我正在声明使用动态存储过程通过模式类型调用按钮事件的方法。