ofcourse there are many posts available on to generate rows dynamically on button click..
I would like to share the code of scenario where in the clickevent (commandname) of buttonfield in gridview would dynamically generate the rows ....
The buttonfield is binded to gridview whose commandname is accessed in the RowBound event of gridview as shown below...
protected void gvStores_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "myCommandAdd")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = gvStores.Rows[index];
TableCell store = selectedRow.Cells[6];
al.Add(Convert.ToInt16(store.Text));
CreateTextBoxControls1(al);
}
method CreateTextBoxControls1() takes input of various click events of gridview which are stored in a static arraylist.
private void CreateTextBoxControls1(ArrayList al)
{
int rowCount = al.Count;
Table tbl = new Table();
if (tbl.GetType().ToString().Equals("System.Web.UI.WebControls.Table") && Placeholder1.FindControl("tbl") == null)
{
tbl.ID = "tbl";
}
tbl.EnableViewState = true;
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
Label _lblBookStore = new Label();
Button _btnDelete = new Button();
_lblBookStore.ID = "lblBookStore" + (rowIndex + 1).ToString();
_lblBookStore.Text =""+al[rowIndex];
tc.Controls.Add(_lblBookStore);
_btnDelete.ID = "btnDelete" + (rowIndex + 1).ToString();
_btnDelete.Text = "MyBookstore";
_btnDelete.Click += new EventHandler(_btnDelete_Click);
tc.Controls.Add(_btnDelete);
tr.Cells.Add(tc);
tblHelp.Rows.Add(tr);
}
Placeholder1.Controls.Add(tbl);
}
thus the place holder gets various rows added to the page dynamically and can associate an AJAX effect by binding the place holder and gridview inside an update panel.
Tuesday, June 26, 2007
code to generate a file based on the number of users logged in specific time
I happened to write a console application to generate a file based on the number of users logged in specific time. I hope this might be useful since its interesting to work with console application after a long time.
.. I hope this makes sense for all whose looking for a starting point for writing such applications..
static void Main(string[] args)
{
String[] commandLineArgs = System.Environment.GetCommandLineArgs();
int duration = System.Convert.ToInt32(commandLineArgs[1]);
int delay = System.Convert.ToInt32(commandLineArgs[2]);
String str = commandLineArgs[3];
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=myserver;uid=sa;password=sa;Initial Catalog=AdventureWorks;";
conn.Open();
FileStream fs = File.Create("c:\\ " + str + ".log");
StreamWriter sw = new StreamWriter(fs);
while (duration > 0)
{
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [AdventureWorks].[dbo].[myTestTable]", conn);
DataSet ds = new DataSet();
StringBuilder stb = new StringBuilder();
da.Fill(ds);
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Columns.Count == 2 && i == 0)
{
sw.WriteLine(ds.Tables[0].Columns[0].ColumnName + " " + ds.Tables[0].Columns[1].ColumnName);
sw.WriteLine("=====================================");
}
string info = ds.Tables[0].Rows[i][0].ToString() + " " + ds.Tables[0].Rows[i][1].ToString();
sw.WriteLine(info);
}
sw.WriteLine();
}
}
Thread.Sleep(delay * 1000);
duration = duration - 1;
}
sw.Close();
fs.Close();
}
}
To run the application..
goto commandprompt of visual studio and change the directory to debug of the current application and type
myapplication.exe 10 10 mylog
this refers to run the exe of the application with arguments duration of 1min and delay of 10 sec and name of the file to log the needed information.
.. I hope this makes sense for all whose looking for a starting point for writing such applications..
static void Main(string[] args)
{
String[] commandLineArgs = System.Environment.GetCommandLineArgs();
int duration = System.Convert.ToInt32(commandLineArgs[1]);
int delay = System.Convert.ToInt32(commandLineArgs[2]);
String str = commandLineArgs[3];
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=myserver;uid=sa;password=sa;Initial Catalog=AdventureWorks;";
conn.Open();
FileStream fs = File.Create("c:\\ " + str + ".log");
StreamWriter sw = new StreamWriter(fs);
while (duration > 0)
{
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [AdventureWorks].[dbo].[myTestTable]", conn);
DataSet ds = new DataSet();
StringBuilder stb = new StringBuilder();
da.Fill(ds);
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Columns.Count == 2 && i == 0)
{
sw.WriteLine(ds.Tables[0].Columns[0].ColumnName + " " + ds.Tables[0].Columns[1].ColumnName);
sw.WriteLine("=====================================");
}
string info = ds.Tables[0].Rows[i][0].ToString() + " " + ds.Tables[0].Rows[i][1].ToString();
sw.WriteLine(info);
}
sw.WriteLine();
}
}
Thread.Sleep(delay * 1000);
duration = duration - 1;
}
sw.Close();
fs.Close();
}
}
To run the application..
goto commandprompt of visual studio and change the directory to debug of the current application and type
myapplication.exe 10 10 mylog
this refers to run the exe of the application with arguments duration of 1min and delay of 10 sec and name of the file to log the needed information.
Subscribe to:
Posts (Atom)