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");
}
Related
I have combobox in my form which display value from database. I want one default value so I use .text property also and on selected index changed event the label display the corresponding value of the database the code is not working
string cstr = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
private void AddStock_Load(object sender, EventArgs e)
{
bindcombo();
}
private void bindcombo()
{
SqlConnection con = new SqlConnection(cstr);
SqlDataAdapter da = new SqlDataAdapter("SELECT Dname FROM dealer", con);
DataTable dt = new DataTable();
da.Fill(dt);
cmbdealer.DataSource = dt;
cmbdealer.DisplayMember = "Dname";
cmbdealer.Text = "select-Dealer";
con.Close();
}
private void cmbdealer_SelectedIndexChanged(object sender, EventArgs e)
{
dealerdetails();
}
private void dealerdetails()
{
SqlConnection con = new SqlConnection(cstr);
SqlCommand cmd = new SqlCommand("select * from dealer where Dname='" + cmbdealer.Text + "'", con);
con.Open();
SqlDataReader re = cmd.ExecuteReader();
while (re.Read())
{
lbdl.Text = re["Ddlno"].ToString();
lbgst.Text = re["Dgstno"].ToString();
lbadd.Text = re["Daddress"].ToString();
}
}
the above code is working fine but when the form is load all the 3 label shows the database value of the first entry of my dealer table without selecting and value from combobox.
I have flow layout panel with SQL and I know how to load items from SQL to flow layout panel as Buttons, but now I need your help every button have click event So how can I control on event of every Button that I load it
items_Panles.Controls.Clear();
SqlConnection con = new SqlConnection("Data Source=DESKTOP-6HNIPQ5;Initial Catalog=Anass;Persist Security Info=True;User ID=sa;Password=123");
SqlDataAdapter sda = new SqlDataAdapter("select * from Table_Employee", con);
DataTable dt = new DataTable();
sda.Fill(dt);
for(int i=0;i<dt.Rows.Count;i++)
{
Button btn = new Button();
btn.Name = "btn" + dt.Rows[i][0].ToString();
btn.Text = dt.Rows[i][1].ToString();
btn.Height = 80;
btn.Width = 75;
items_Panles.Controls.Add(btn);
}
this code is fire when I click on the Individual button
Stub out your click method, for example:
void btn_Click(object sender, EventArgs e) {
Button b = sender as Button;
if (b != null) {
MessageBox.Show(b.Name);
}
}
then when you create the button, attach the handler:
Button btn = new Button();
btn.Click += btn_Click;
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 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.