Reputation: 8079
I'm trying to convert a WinForms project to an ASP.Net project. Currently I'm struggling with a basic problem. I need to create a Button
dynamically an display it on the page after the user selected a row in a GridView
. Before I add the Button
to the page I set an Click
event handler. The problem is, that this event handler is never fired. I've tried to create the Button dynamically when the SelectedIndexChanged
event of the GridView
is fired and to create the Button
as an instance member and set the event handler in the OnInit
method of the class. Neither worked. Here is my code for the first attempt:
protected void dgvReports_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.dgvReports.SelectedIndex >= 0)
{
Report rpt = (Report)bs.Current;
Control parameterCaption = this.divParameters.Controls[0];
Button btnAccept = new Button() { Text = "Get results" };
bool newLine = false;
this.divDescription.Visible = true;
this.divParameters.Visible = true;
this.divParameters.Controls.Clear();
this.divParameters.Controls.Add(parameterCaption);
this.txtDescription.Text = rpt.Description;
btnAccept.Click += new EventHandler(btnAccept_Click);
foreach (ReportParameter parameter in rpt.Parameters)
{
if (parameter.Visible)
{
this.divParameters.Controls.Add(new Label() { Text = parameter.Description, Width = 150, CssClass = "parameter" });
this.divParameters.Controls.Add(new TextBox() { Text = parameter.DefaultValue, Width = 300, ID = parameter.Name });
if (newLine)
{
this.divParameters.Controls.Add(new LiteralControl("<br />"));
}
newLine = !newLine;
}
}
this.divParameters.Controls.Add(new LiteralControl("<br /> <div style='text-align:center'>"));
this.divParameters.Controls.Add(btnAccept);
this.divParameters.Controls.Add(new LiteralControl("</div>"));
}
}
void btnAccept_Click(object sender, EventArgs e)
{
Report rpt = (Report)bs.Current;
SqlConnection con = new SqlConnection(global::System.Configuration.ConfigurationManager.ConnectionStrings["DP2ConnectionString"].ConnectionString);
SqlCommand com = new SqlCommand();
DataTable dataTable = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(com);
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = rpt.DbProcedure;
dataTable.Locale = CultureInfo.CurrentCulture;
foreach (Control control in this.divParameters.Controls)
{
if (control is TextBox)
{
TextBox txt = control as TextBox;
com.Parameters.AddWithValue(txt.ID, txt.Text);
}
}
foreach (ReportParameter parameter in rpt.Parameters)
{
if (!parameter.Visible)
{
com.Parameters.AddWithValue(parameter.Name, parameter.DefaultValue);
}
}
sda.Fill(dataTable);
}
Upvotes: 0
Views: 369
Reputation: 346
Try changing btnAccept.Click += new EventHandler(btnAccept_Click);
to btnAccept.Click += new RoutedEventHandler(btnAccept_Click);
Upvotes: 0
Reputation: 985
Dynamic controls in asp.net are never easy. Your event handler is probably not being included in the View State and therefore not persisting on postbacks, like when they click the button in question. The button has to be remade on each page load, which event handler being attached then as well- if possible, I'd save yourself a headache and try showing and hiding the button.
Upvotes: 1