WinForms 프로젝트를 ASP.Net 프로젝트로 변환하려고합니다. 현재 나는 기본적인 문제로 어려움을 겪고 있습니다. Button
사용자가 .NET Framework에서 행을 선택한 후 페이지에 동적으로 표시 하도록 만들어야 합니다 GridView
. Button
페이지에를 추가하기 전에 Click
이벤트 핸들러를 설정했습니다 . 문제는이 이벤트 핸들러가 실행되지 않는다는 것입니다. 의 SelectedIndexChanged
이벤트가 시작될 때 Button을 동적으로 GridView
만들고 Button
인스턴스 멤버로 만들고 OnInit
클래스 의 메서드 에서 이벤트 처리기를 설정하려고했습니다 . 둘 다 작동하지 않았습니다. 다음은 첫 번째 시도에 대한 코드입니다.
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);
}
asp.net의 동적 제어는 결코 쉽지 않습니다. 이벤트 핸들러가보기 상태에 포함되지 않았기 때문에 해당 버튼을 클릭 할 때와 같이 포스트 백에서 지속되지 않습니다. 버튼은 각 페이지가로드 될 때마다 다시 만들어 져야하며, 어떤 이벤트 핸들러도 연결되어야합니다. 가능한 경우 골칫거리를 피하고 버튼을 표시하고 숨기려고합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다