what is wrong with the code here... i am trying to update the values fetched in a row of a grid view by an edit,update,cancel command field button.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lblid = (Label)GridView1.Rows[e.RowIndex].FindControl("Elblid");
TextBox txtuser = (TextBox)GridView1.Rows[e.RowIndex].FindControl("EtxtUserName");
TextBox txtpass = (TextBox)GridView1.Rows[e.RowIndex].FindControl("EPassword");
if (lblid.Text != "" || lblid.Text != null)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Update Login set UserName='" + txtuser.Text + "',Password='" + txtpass.Text + "' Where Id=" + lblid.Text, con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
FillGrid();
}
This was the error thrown :
PS: when is set the event validation to be false in the page directives. this error disappeared but the code did nothing it was intended to.
what error it is showing??
try this
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblid = (Label)row.FindControl("Elblid");
TextBox txtuser = (TextBox)row.FindControl("EtxtUserName");
TextBox txtpass = (TextBox)row.FindControl("EPassword");
}
Related
I am having problems getting the selected index and selected value of a ListBox. I am trying to fill the list box with values from a database and after this, on selected index change event, I cannot extract the selected index and selected value. Instead, I get a selected index of -1 and the selected value does not contain a value.
Here is a screenshot before clicking any item in the ListBox:
And this is a screen shot taken after clicking an item:
Here is the c# code:
public partial class std_Course_dashboard : System.Web.UI.Page
{
int index;
string value;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["uid"].ToString();
Label2.Text = Session["crs_id"].ToString();
ListBox1.Items.Clear();
SqlDataReader r;
SqlCommand cmd = new SqlCommand("select lecture_text, lecture_Title,lecture_No from Lecture where course_ID='" + Convert.ToInt32(Session["crs_id"]) + "'", con);
con.Open();
ListBox1.DataSource = cmd.ExecuteReader();
ListBox1.DataTextField = "lecture_Title";
ListBox1.DataValueField = "lecture_No";
ListBox1.DataBind();
con.Close();
ListBox1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
value = ListBox1.SelectedValue;
index = ListBox1.SelectedIndex;
Label3.Text = value;
Label4.Text = index.ToString();
}
}
This is because of clearing the items each time the page makes request
ListBox1.Items.Clear();
you can remove this line of code and adding if(!IsPostBack) so that data loads only for the first time when page loads
if(!IsPostBack)
{
Label1.Text = Session["uid"].ToString();
Label2.Text = Session["crs_id"].ToString();
SqlDataReader r;
SqlCommand cmd = new SqlCommand("select lecture_text, lecture_Title,lecture_No from Lecture where course_ID='" + Convert.ToInt32(Session["crs_id"]) + "'", con);
con.Open();
ListBox1.DataSource = cmd.ExecuteReader();
ListBox1.DataTextField = "lecture_Title";
ListBox1.DataValueField = "lecture_No";
ListBox1.DataBind();
con.Close();
ListBox1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
}
If you place a breakpoint within your Page_Load, you will notice that it hits on every single PostBack. Since this happens, your code is designed such that your ListBox gets rebound with data every time you PostBack - including when you select a new index in your ListBox. This is what is causing your SelectedIndex to get reset.
What you need to do is only bind your ListBox once. You can achieve this by checking the Page.IsPostBack condition. If the PostBack is not caused by the client, bind the ListBox.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Label1.Text = Session["uid"].ToString();
Label2.Text = Session["crs_id"].ToString();
ListBox1.Items.Clear();
SqlDataReader r;
SqlCommand cmd = new SqlCommand("select lecture_text, lecture_Title,lecture_No from Lecture where course_ID='" + Convert.ToInt32(Session["crs_id"]) + "'", con);
con.Open();
ListBox1.DataSource = cmd.ExecuteReader();
ListBox1.DataTextField = "lecture_Title";
ListBox1.DataValueField = "lecture_No";
ListBox1.DataBind();
con.Close();
ListBox1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
}
}
I try to develop a web based survey creation and filling app on ASP.NET and C# getting data from Sql Server. I use DataBind to bind my question names and answers. Below in the picture, I need to show only the question number (like 1, 2, 3 etc.) in RadioButtonList (not with the question name) and only the question name in a label lblQuestion as;
protected void rbtnQuestions_SelectedIndexChanged(object sender, EventArgs e)
{
General.myquestionid = System.Convert.ToInt32(rbtnQuestions.SelectedValue);
lblQuestion.Text = rbtnQuestions.SelectedItem.Text;
}
I also need the "id" of survey_question table to display the answers below the question since I use it in SelectedIndexChanged event.
But since I use DataTextField, both RadioButtonList text and lblQuestion.Text show the same thing. My way of binding the data is below:
string sqlStr = "SELECT id, question_no, question, survey_answer_type_id FROM survey_question WHERE survey_id = '" + General.mysurveyid + "'";
cmd = new SqlCommand(sqlStr, connection);
da.SelectCommand = cmd;
da.Fill(data);
rbtnQuestions.DataSource = data;
rbtnQuestions.DataTextField = "question";
rbtnQuestions.DataValueField = "id";
rbtnQuestions.DataBind();
cmd.Dispose();
How can I achieve this?
Thanks.
On Page load event Set
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sqlStr = "SELECT id,question_no, CONVERT(varchar(10),(ROW_NUMBER() over (order by question desc))) AS QueNo,question, survey_answer_type_id FROM survey_question WHERE survey_id = '" + General.mysurveyid + "'";
cmd = new SqlCommand(sqlStr, connection);
da.SelectCommand = cmd;
da.Fill(data);
ViewState["data"] = data;
rbtnQuestions.DataSource = data;
rbtnQuestions.DataTextField = "QueNo";
rbtnQuestions.DataValueField = "id";
rbtnQuestions.DataBind();
cmd.Dispose();
}
}
And Question Set On SelectedIndexChanged Event
protected void rbtnQuestions_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = (DataSet)ViewState["data"];
DataRow[] row = ds.Tables[0].Select("id = '" + rbtnQuestions.SelectedValue + "'");
lblQuestion.Text = row[0]["question"].ToString();
}
I'm trying to let the gridview auto-select the first row of data upon page load. However, in the gridview, it shows that the first row is being highlighted
but no data is being displayed in my textbox. The data only appears when i click the select button in my gridview again.
This is how i added the auto-select gridview row in my page load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvnric.SelectedIndex = 0;
}
}
This is how i get my data from my gridview to my textbox
protected void gvnric_SelectedIndexChanged(object sender, EventArgs e)
{
Session["nric"] = gvnric.SelectedRow.Cells[1].Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
SqlCommand cm = new SqlCommand("Select fullname, contact, address, email From MemberAccount Where nric = '" + Session["nric"] + "'", con);
SqlDataReader dr;
dr = cm.ExecuteReader();
if (dr.Read())
{
txtFullName.Text = dr["fullname"].ToString();
txtAddress.Text = dr["contact"].ToString();
txtContact.Text = dr["address"].ToString();
txtEmail.Text = dr["email"].ToString();
}
con.Close();
Image1.Attributes["src"] = "MemberNricCard.aspx?";
Image1.Attributes["height"] = "200";
Image1.Attributes["width"] = "200";
}
But what could possibly caused the data not to be displayed when the first row already being selected upon page load.
I would Re Factor the code as below :
PageLoad
if (!IsPostBack)
{
gvnric.SelectedIndex = 0;
LoadFormFields();
}
gvnric_SelectedIndexChanged
protected void gvnric_SelectedIndexChanged(object sender, EventArgs e)
{
LoadFormFields();
}
and create LoadFormFields with what you have in gvnric_SelectedIndexChanged
You can just call your gridview code in the page load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvnric.SelectedIndex = 0;
gvnric_SelectedIndexChanged(this, EventArgs.Empty);
}
}
Hi coder i have a search user page where i have a texbox, button and a gridview. Now when i enter name of the user and click on search button i get the detail of the user.
My database is like this
I already bind my gridview on page load so that i can also view the details of user in gridview like this:
Now in this gridview you can see a IsEnable header which have checkbox control. That Enable is a bit which is always true when user is created.
What i want is when user click on that checkbox that bit become false i try this on the rowdatabound but it give me object reference error can you guys tell me what i do to make my bit false when user select that checkbox
Please help me on this thanks in advance
Now this what i done now with my code now tell me how to update my enable bit from true to false on row update
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.GetGridData();
}
}
protected void BindGrid()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from CreateUser", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
private void GetGridData()
{
con.Open();
string query = "Select * from CreateUser";
da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void btnSearchUser_Click(object sender, EventArgs e)
{
this.BindGrid();
}
protected void lnkdelete_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
//getting particular row linkbutton
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
//getting userid of particular row
int UserID = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());
string FirstName = gvrow.Cells[0].Text;
con.Open();
SqlCommand cmd = new SqlCommand("delete from CreateUser where UserID=" + UserID, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindGrid();
//Displaying alert message after successfully deletion of user
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + FirstName + " details deleted successfully')", true);
this.GetGridData();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//if (e.Row.RowType == DataControlRowType.DataRow)
//{
// //getting username from particular row
// string FirstName = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "FirstName"));
// //identifying the control in gridview
// LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
// //raising javascript confirmationbox whenver user clicks on link button
// lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox('" + FirstName + "')");
//}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GetGridData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label userid= (Label)row.FindControl("lblUserID");
con.Open();
SqlCommand cmd = new SqlCommand("delete FROM CreateUser where UserID='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
string query = string.Empty;
string userid = GridView1.DataKeys[e.RowIndex].Values["UserID"].ToString();
//Label id = GridView1.Rows[e.RowIndex].FindControl("lblUserID") as Label;
TextBox FirstName = GridView1.Rows[e.RowIndex].FindControl("txtFirstName") as TextBox;
TextBox LastName = GridView1.Rows[e.RowIndex].FindControl("txtLastName") as TextBox;
TextBox DomainID = GridView1.Rows[e.RowIndex].FindControl("txtDomainID") as TextBox;
TextBox EmailID = GridView1.Rows[e.RowIndex].FindControl("txtEmailID") as TextBox;
TextBox Password = GridView1.Rows[e.RowIndex].FindControl("txtPassword") as TextBox;
TextBox ConfirmPassword = GridView1.Rows[e.RowIndex].FindControl("txtConfirmPassword") as TextBox;
TextBox RoleType = GridView1.Rows[e.RowIndex].FindControl("txtRoleType") as TextBox;
CheckBox IsEnable = GridView1.Rows[e.RowIndex].FindControl("chkIsEnableEdit") as CheckBox;
//TextBox textadd = (TextBox)row.FindControl("txtadd");
//TextBox textc = (TextBox)row.FindControl("txtc");
GridView1.EditIndex = -1;
con.Open();
//SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
SqlCommand cmd = new SqlCommand("update CreateUser set FirstName='" + FirstName.Text + "',LastName='" + LastName.Text + "',DomainID='" + DomainID.Text + "',EmailID='" + EmailID.Text + "',Password='" + Password.Text + "',ConfirmPassword='" + ConfirmPassword.Text + "',RoleType='" + RoleType.Text + "',Enable='" + IsEnable.Checked + "' where UserID='" + userid + "'", con);
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
//GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
}
Put some defensive coding in first, and find out where the object reference error is happening:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var chk = (CheckBox)e.Row.FindControl("chkEnable");
if (chk == null || !chk.checked) return;
var userId =GridView1.DataKeys[row.RowIndex].Value as string;
if (string.IsNullOrWhiteSpace(userId)) return;
var query = "update CreateUser set Enable='False' where UserID='" + userid + "'";
cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();
}
}
You can use the CheckChanged event:
<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkEnable" runat="server" AutoPostBack="true" OnCheckedChanged="ChkEnable_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And then update the database in CheckChanged event:
protected void ChkEnable_CheckedChanged(object sender, EventArgs e)
{
int selRowIndex = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex;
var userId = gridView.DataKeys[selRowIndex].Value as string;
if (string.IsNullOrWhiteSpace(userId)) return;
var checkBox = gridView.Rows[selRowIndex].FindControl("chkEnable") as CheckBox;
if (checkBox == null) return;
var query = "update CreateUser set Enable= #Enabled where UserID='" + userid + "'";
cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("Enabled", checkBox.Checked);
cmd.ExecuteNonQuery();
con.Close();
}
First of all why are you doing it on RowDataBound event ? RowDataBound occurs when a row is
bounded to gridview but here we have to update gridview rows.
If you have to update your rows you can do it on RowUpdating command .
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Checkbox chkbx = ((Checkbox)(row.Cells[8].Controls[0]));
bool IsChecked =chkbx.Checked ;
//You can write your update command here.
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox MyName= (TextBox)e.Row.FindControl("FirstName");
string name = Myname.text;
//For checkbox
Checkbox chkbx = (Checkbox)e.Row.FindControl("IsEnabled");
if(chkbx.Checked ==true)
{
// do your stuff for checkbox checked.
}
else
{
// do your stuff if checkbox is not selected
}
}
}
Finally i got my answer thanks for your reply guys
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.GetGridData();
}
}
protected void BindGrid()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from CreateUser", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
private void GetGridData()
{
con.Open();
string query = "Select * from CreateUser";
da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void btnSearchUser_Click(object sender, EventArgs e)
{
this.BindGrid();
}
protected void lnkdelete_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
//getting particular row linkbutton
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
//getting userid of particular row
int UserID = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());
string FirstName = gvrow.Cells[0].Text;
con.Open();
SqlCommand cmd = new SqlCommand("delete from CreateUser where UserID=" + UserID, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindGrid();
//Displaying alert message after successfully deletion of user
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + FirstName + " details deleted successfully')", true);
this.GetGridData();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GetGridData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label userid= (Label)row.FindControl("lblUserID");
con.Open();
SqlCommand cmd = new SqlCommand("delete FROM CreateUser where UserID='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
string query = string.Empty;
string userid = GridView1.DataKeys[e.RowIndex].Values["UserID"].ToString();
//Label id = GridView1.Rows[e.RowIndex].FindControl("lblUserID") as Label;
TextBox FirstName = GridView1.Rows[e.RowIndex].FindControl("txtFirstName") as TextBox;
TextBox LastName = GridView1.Rows[e.RowIndex].FindControl("txtLastName") as TextBox;
TextBox DomainID = GridView1.Rows[e.RowIndex].FindControl("txtDomainID") as TextBox;
TextBox EmailID = GridView1.Rows[e.RowIndex].FindControl("txtEmailID") as TextBox;
TextBox Password = GridView1.Rows[e.RowIndex].FindControl("txtPassword") as TextBox;
TextBox ConfirmPassword = GridView1.Rows[e.RowIndex].FindControl("txtConfirmPassword") as TextBox;
TextBox RoleType = GridView1.Rows[e.RowIndex].FindControl("txtRoleType") as TextBox;
CheckBox IsEnable = GridView1.Rows[e.RowIndex].FindControl("chkIsEnableEdit") as CheckBox;
//TextBox textadd = (TextBox)row.FindControl("txtadd");
//TextBox textc = (TextBox)row.FindControl("txtc");
GridView1.EditIndex = -1;
con.Open();
//SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
SqlCommand cmd = new SqlCommand("update CreateUser set FirstName='" + FirstName.Text + "',LastName='" + LastName.Text + "',DomainID='" + DomainID.Text + "',EmailID='" + EmailID.Text + "',Password='" + Password.Text + "',ConfirmPassword='" + ConfirmPassword.Text + "',RoleType='" + RoleType.Text + "',Enable='" + IsEnable.Checked + "' where UserID='" + userid + "'", con);
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
//GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
}
I am creating the dynamic text boxes and buttons but when I add values it does not save any thing with I enter into the data base. Well it does not give any error when I enter the values. please tell me if I am doing any thing wrong.
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";
}
Unless the dynamically added controls are added at the init or preinit stage, they will not persist beyond a postback. Controls added after this need to be recreated on each post back.
But in your case, I would suggest that you just create the controls at design time inside a div or panel with its Visible property set to false and then when the button is clicked, just change the Visible property to true. It looks like you just want to show some login boxes when the button is clicked.