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.
Related
i am generating buttons dynamically on page load and on button click event i calling LoadSubClause event which is working fine and with in this event i am creating new buttons. These newly created buttons have LoadDescription event assigned on button click but once i click on any of these buttons it clears the SubClause created buttons and nothing shows up from LoadDescription event.
public void LoadSubClause(object sender, EventArgs e)
{
Button btn = sender as Button;
string ClauseName = btn.Text.ToString();
int counter = 100;
string query = "select SubClause from Output_DocumentContent where Clause ='" + ClauseName.ToString() + "'";
Response.Write(query);
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Legal;Integrated Security=true");
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
Button newButton = new Button();
newButton.Attributes["class"] = "buttonVertical";
newButton.Text = sdr[0].ToString();
newButton.ID = "SubClause"+counter.ToString();
newButton.Click += new EventHandler(LoadDescription);
//buttons.Add(newButton);
PanelSubClause.Controls.Add(newButton);
counter++;
}
con.Close();
}
public void LoadDescription(object sender, EventArgs e)
{
Response.Write("Load Description Event");
}
I need to use a single event handler for multiple buttons. I generated those buttons via while loop according to the database query. I created a single method
void MyButtonClick(object sender, EventArgs e)
{
}
I'm new to the C#. How can I bind all button's events to a single handler.
Code for generating the buttons:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
MydbConnection db = new MydbConnection();
MySqlConnection con = db.connection();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "select * from categories where online = 1";
cmd.Connection = con;
MySqlDataReader rd;
con.Open();
rd = cmd.ExecuteReader();
int i = 1;
while (rd.Read())
{
Button btn = new Button();
btn.Name = "btn-" + i.ToString();
btn.Tag = i;
btn.Text = rd.GetString(2).ToString();
btn.Height = 60;
btn.Width = 100;
btn.Location = new Point(900, 60 * i + 10);
this.Controls.Add(btn);
i++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Welcome to SO. You can add this right before this.Controls.Add(btn);
btn.Click += MyButtonClick;
You need to set all of their CLICK properties to the same event handler:
btn.Click += new EventHandler(MyButtonClick);
That should make it so that anytime you click any of the buttons, MyButtonClick() is the event that triggers. Of course, if you need to figure out WHICH button was clicked, then you need to check the sender parameter within the event, where 'i' is one of the number values you assigned to a button in the loop:
(if sender == btn-i) then ....
I am devleoping a online airline reservation system in which i have two dropdownlists to select source and destinations and a label .this label will show " there are no flights" if there are no matching routes retrieved from the database (in this case its sqlserver 2008).i have written the following code which tries to do so, but when i postback or refresh the page the label with " there are no flights" is till visible.what is wrong with my code please anyone help me with that.
public partial class Dropdndemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con=new SqlConnection("Data Source=KUNDAN-PC\\SQLEXPRESS;Initial Catalog=Ars2.1.2;Integrated Security=True");
//string Sqlcmnd="select Source from Ars2.1.2.dbo.Scheduling";
con.Open();
if (!Page.IsPostBack)
{
SqlCommand com = new SqlCommand("select distinct Source from Schedulings", con);
SqlCommand comn=new SqlCommand("select distinct Destination from Schedulings", con);
//SqlDataReader readr;
DropDownList1.DataSource = com.ExecuteReader();
DropDownList1.DataTextField = "Source";
// DropDownList1.DataTextField = "Destination";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "select Source");
con.Close();
con.Open();
DropDownList2.DataSource = comn.ExecuteReader();
DropDownList2.DataTextField = "Destination";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, "select Destination");
con.Close();
}
//con.Close();
// DropDownList1.DataBind();
//con.Close();
if (IsPostBack)
Label3.Text = "";
//Label1.Visible = false;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// string Source = DropDownList1.SelectedValue.ToString();
// Label1.Text = Source;
}
protected void Button1_Click(object sender, EventArgs e)
{
string src = DropDownList1.SelectedItem.ToString();
string desti = DropDownList2.SelectedItem.ToString();
if ((src == desti) && IsPostBack)
{
Label1.Text = "Source And Destination cant be same!";
}
SqlConnection lop = new SqlConnection("Data Source=KUNDAN-PC\\SQLEXPRESS;Initial Catalog=Ars2.1.2;Integrated Security=True");
lop.Open();
SqlCommand cmd = new SqlCommand("select * from Schedulings where Source=#Source and Destination=#Destination", lop);
cmd.Parameters.AddWithValue("Source", DropDownList1.SelectedItem.Text);
cmd.Parameters.AddWithValue("Destination", DropDownList2.SelectedItem.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 0)
{
Label3.Text = "No planes available in this route!!!";
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
I suppose you are refreshing page using F5 or by right clicking on page and opting refresh option or using refresh button of browser. If this is the case then I am scared it's not refresh, it's repeating previous action. That means if you were searching for flight and refreshing using above options, it will again search for flight using same search criteria. You can confirm it by putting debugger on search event. You can avoid this behavior by setting and clearing Session or ViewState and manage label text using it.
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();
}
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");
}