A
Alice Lee
Hi,
My web from has one button and by clicking this button a list of
linkbuttons must be dynamically displayed based on information in
database. Then click any one of these linkbuttons another set of
linkbuttons will be displayed, and so forth... My code works, but I have
to click twice every time to get it running correctly. I can not figure
out what's wrong in my code.
My code:
public class WebForm1 : System.Web.UI.Page
{
static string conn = ConfigurationSettings.AppSettings.Get("ConnStr");
protected SqlConnection mysqlConn = new SqlConnection(conn);
protected System.Web.UI.WebControls.Button button;
protected System.Web.UI.WebControls.Panel panel;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
Session["parentId"] = 0;
else
CreateDynamicButtons();
}
private void CreateDynamicButtons()
{
int parentId = (int)Session["parentId"];
SqlCommand cmd = mysqlConn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select categoryname from mycategory where
parentid=" + parentId;
SqlDataAdapter myAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
myAdapter.SelectCommand = cmd;
myAdapter.Fill(ds, "mycategory");
DataTable dt = new DataTable();
dt = ds.Tables[0];
if(panel.Controls.Count!=0)
panel.Controls.Clear();
Table table = new Table();
for (int i=0; i<dt.Rows.Count; i++)
{
LinkButton lbt = new LinkButton();
TableCell cell = new TableCell();
TableRow row = new TableRow();
lbt.Text = dt.Rows.ItemArray[0].ToString();
lbt.Font.Size = FontUnit.Medium;
lbt.CommandName = "CategoryName";
lbt.CommandArgument = lbt.Text;
lbt.Command += new
System.Web.UI.WebControls.CommandEventHandler(lbt_click);
cell.Controls.Add(lbt);
row.Cells.Add(cell);
table.Rows.Add(row);
}
panel.Controls.Add(table);
panel.Visible = true;
}
private void lbt_click(object sender, CommandEventArgs e)
{
LinkButton lbtSender = (LinkButton)sender;
string query = "Select CategoryId from mycategory where
categoryname='" + lbtSender.CommandArgument + "'";
SqlCommand cmd = new SqlCommand ( query, mysqlConn );
cmd.Connection.Open();
Session["parentId"] = (int)cmd.ExecuteScalar();
cmd.Connection.Close();
CreateDynamicButtons();
}
private void button_Click(object sender, System.EventArgs e)
{
Session["parentId"] = 0;
CreateDynamicButtons();
}
}
Thank you in advance.
Alice
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
My web from has one button and by clicking this button a list of
linkbuttons must be dynamically displayed based on information in
database. Then click any one of these linkbuttons another set of
linkbuttons will be displayed, and so forth... My code works, but I have
to click twice every time to get it running correctly. I can not figure
out what's wrong in my code.
My code:
public class WebForm1 : System.Web.UI.Page
{
static string conn = ConfigurationSettings.AppSettings.Get("ConnStr");
protected SqlConnection mysqlConn = new SqlConnection(conn);
protected System.Web.UI.WebControls.Button button;
protected System.Web.UI.WebControls.Panel panel;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
Session["parentId"] = 0;
else
CreateDynamicButtons();
}
private void CreateDynamicButtons()
{
int parentId = (int)Session["parentId"];
SqlCommand cmd = mysqlConn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select categoryname from mycategory where
parentid=" + parentId;
SqlDataAdapter myAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
myAdapter.SelectCommand = cmd;
myAdapter.Fill(ds, "mycategory");
DataTable dt = new DataTable();
dt = ds.Tables[0];
if(panel.Controls.Count!=0)
panel.Controls.Clear();
Table table = new Table();
for (int i=0; i<dt.Rows.Count; i++)
{
LinkButton lbt = new LinkButton();
TableCell cell = new TableCell();
TableRow row = new TableRow();
lbt.Text = dt.Rows.ItemArray[0].ToString();
lbt.Font.Size = FontUnit.Medium;
lbt.CommandName = "CategoryName";
lbt.CommandArgument = lbt.Text;
lbt.Command += new
System.Web.UI.WebControls.CommandEventHandler(lbt_click);
cell.Controls.Add(lbt);
row.Cells.Add(cell);
table.Rows.Add(row);
}
panel.Controls.Add(table);
panel.Visible = true;
}
private void lbt_click(object sender, CommandEventArgs e)
{
LinkButton lbtSender = (LinkButton)sender;
string query = "Select CategoryId from mycategory where
categoryname='" + lbtSender.CommandArgument + "'";
SqlCommand cmd = new SqlCommand ( query, mysqlConn );
cmd.Connection.Open();
Session["parentId"] = (int)cmd.ExecuteScalar();
cmd.Connection.Close();
CreateDynamicButtons();
}
private void button_Click(object sender, System.EventArgs e)
{
Session["parentId"] = 0;
CreateDynamicButtons();
}
}
Thank you in advance.
Alice
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!