我正在创建动态文本框和按钮但是当我添加值时,它不会保存任何东西,我进入数据库。当我输入值时,它不会给出任何错误。如果我做错了,请告诉我。
protected void Page_Load(object sender, EventArgs e)
{
Session["clicks"] = "";
}
protected void btnCU_Click(object sender, EventArgs e)
{
Button Ad_AB = new Button();
Ad_AB.ID = "btnAd_add";
Ad_AB.Text = "Add";
Ad_AB.Click += new EventHandler(Ad_AB_Click);
TextBox txtAd_AUN = new TextBox();
TextBox txtAd_AP = new TextBox();
txtAd_AUN.ID = "txtAd_AUN".ToString() ;
txtAd_AP.ID = "txtAd_AP".ToString() ;
Label lblAd_AEUN = new Label();
Label lblAd_AEP = new Label();
lblAd_AEUN.Text = "Enter User Name :";
lblAd_AEP.Text = "Enter Passowrd :";
pnlCNU.Controls.Add(Ad_AB);
pnlCNU.Controls.Add(lblAd_AEUN);
pnlCNU.Controls.Add(txtAd_AUN);
pnlCNU.Controls.Add(lblAd_AEP);
pnlCNU.Controls.Add(txtAd_AP);
if(Session["clicks"].ToString() == "G"){
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Omer\\Documents\\Visual Studio 2010\\WebSites\\WAPPassignment\\App_Data\\LoginStuff.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd;
SqlDataReader dr;
con.Open();
cmd = new SqlCommand("Insert into WhatTypes(UserName, Password) Values ('" + txtAd_AUN.Text + "', '" + txtAd_AP.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
}
void Ad_AB_Click(object sender, EventArgs e)
{
//throw new NotImplementedException();
Session["clicks"] = "G";
}
答案 0 :(得分:1)
除非在init或preinit阶段添加动态添加的控件,否则它们不会超出回发。在此之后添加的控件需要在每个帖子上重新创建。
但是在你的情况下,我建议你只是在设计时在div或面板中创建控件,并将其Visible属性设置为false,然后单击该按钮时,只需将Visible属性更改为true即可。看起来你只想在点击按钮时显示一些登录框。