动态按钮不触发按钮的点击事件或页面生命周期(非动态)
Dynamic button not firing click event or page life cycle for button(not dynamic)
我有不同的实体类型,根据点击的实体(下拉列表),每次需要上传的数量和类型都不同。
因此,我在动态 table 中创建了多个动态上传控件,并使用动态上传按钮同时上传所有文件(我还尝试在 asp.net 页面上添加一个按钮以及)。一切都很好,我可以选择要上传的文件。
我遇到的问题是动态按钮控件没有触发它的 onclicked 事件,因此我无法保存文件。我尝试在 asp.net 侧创建一个确实会触发的按钮,但由于页面生命周期,它没有获取我的上传控件。
然后我尝试添加 OnInit 事件并在其中创建动态按钮,并在下拉列表的选定索引更改上创建其余的上传动态控件,但随后创建了除动态上传控件之外的所有内容。点击事件然后触发。 (Page_Init 做同样的事情)。
我希望按钮不是动态的,而是到达文件上传控件以保存文件。有没有办法绕过页面生命周期来实现这一点,或者有人可以告诉我我做错了什么吗?或者如何让动态按钮触发点击事件?
任何帮助将不胜感激....
这是我的代码:
protected void lstLegalEntity_SelectedIndexChanged(object sender, EventArgs e)
{
if (CTflag == false)
{
this.Rows = tblRow;
this.Columns = tblCol;
lblAmountOfRows.Text = "4";
CreateDynamicTable();
}
else
{
CTflag = true;
}
clrControls();
}
protected void CreateDynamicTable()
{
string filterstring = "";
filterstring = "SELECT let.ID, UploadType " +
"FROM [dbo].[AssetContract_LegalEntityLinks] lel " +
"INNER Join [dbo].[AssetContract_LegalEntity] le " +
"ON lel.LegalEntityRef = le.ID " +
"INNER JOIN [dbo].[AssetContract_LegalEntityTypes] let " +
"ON lel.LegalTypeRef = let.ID " +
"WHERE lel.LegalEntityRef = @LegalEntityRef ";
cn = new SqlConnection(GetConnectionString());
SqlCommand myCmd = new SqlCommand();
myCmd.CommandText = filterstring;
myCmd.Connection = cn;
myCmd.CommandType = CommandType.Text;
if (lstLegalEntity.SelectedValue != "")
{
myCmd.Parameters.AddWithValue("@LegalEntityRef", Convert.ToInt32(lstLegalEntity.SelectedValue));
}
else
{
myCmd.Parameters.AddWithValue("@LegalEntityRef", Convert.ToInt32(0));
}
cn.Open();
SqlDataReader myReader = myCmd.ExecuteReader();
tblRow = GetUploadControlsCount();
if (CTflag == false)
{
table.Caption = "Upload Requirements";
table.ID = "Upload Requirements";
table.BackColor = System.Drawing.Color.BurlyWood;
divFileUploads.Controls.Add(table);
for (i = 0; i < 1; i++)
{
row = new TableRow();
row.BorderStyle = BorderStyle.Ridge;
for (j = 0; j <= tblCol; j++)
{
cell = new TableCell();
cell.BorderWidth = 5;
cell.BorderStyle = BorderStyle.Ridge;
cell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j <= tblCol; j++)
{
string[] Header = { "Upload Type", "File" };
Label lbl = new Label();
lbl.ID = "lblHeader" + j;
if (j == 1)
{
lbl.Width = 220;
}
else
{
lbl.Width = 100;
}
lbl.Text = Header[j];
cell.Controls.Add(lbl);
}
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
int readCount = 1;
while (myReader.Read())
{
for (i = 0; i < 1; i++)
{
row = new TableRow();
row.ID = "rw" + myReader["UploadType"].ToString();
row.BorderStyle = BorderStyle.Ridge;
for (j = 0; j <= tblCol; j++)
{
cell = new TableCell();
cell.ID = tbColId + i + j + myReader["UploadType"].ToString(); ;
cell.BorderWidth = 5;
cell.BorderStyle = BorderStyle.Ridge;
cell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j <= 0; j++)
{
Label lbl = new Label();
lbl.ID = "lblCCRow" + i + "Col" + j + myReader["UploadType"].ToString();
lbl.Text = myReader["UploadType"].ToString();
lbl.Width = 100;
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (j = 0; j < 1; j++)
{
fileUp = new FileUpload();
//m = i; n = j;
fileUp.ID = filename + i + readCount.ToString();
fileUp.Width = 350;
cell.Controls.Add(fileUp);
cmdArg = fileUp.ID;
}
row.Cells.Add(cell);
}
table.Rows.Add(row);
readCount++;
}
i = 0;
j = 0;
}
for (i = 0; i < 1; i++)
{
rrow = new TableRow();
rrow.ID = "ResultRow";
rrow.BorderStyle = BorderStyle.Ridge;
rrow.HorizontalAlign = HorizontalAlign.Center;
for (j = 0; j <= tblCol; j++)
{
rcell = new TableCell();
rcell.ID = "resultCol" + j;
rcell.BorderWidth = 5;
rcell.BorderStyle = BorderStyle.Ridge;
rcell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j < 1; j++)
{
btnUpload = new Button();
btnUpload.Width = 100;
btnUpload.Text = "Upload";
btnUpload.ID = btnUpload.Text;
rcell.Controls.Add(btnUpload);
btnUpload.Click += new EventHandler(UpLdButton_Click);
}
rrow.Cells.Add(rcell);
}
table.Rows.Add(rrow);
}
CTflag = true;
ViewState["dynamictable"] = true;
cn.Close();
myReader.Close();
}
}
protected void UpLdButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < tblRow; i++)
{
fileUp = (FileUpload)FindControlRecursive(this, string.Format("fileUpLoader{0}{1}", 0, i));
//rest of code to save file
}
}
当您在 OnInit 事件中添加动态按钮时,您走在了正确的轨道上 - 您只需要也添加 FileUpload 控件。
所有控件都需要在每次回发时重新创建。 .NET 自动处理您添加到 ASPX 页面的控件。您对以编程方式添加的动态控件负责。
您可以根据自己所在的位置采取以下几种方法:
继续动态添加控件,但进行更改以便添加所有控件,而不仅仅是按钮。
如果您知道控件的最大数量,那么您可以添加所有控件并使用 Visible 属性 控制显示的内容。这在控件数量较少时有效。看起来你在 table 单元格周围放置了边框,所以这在你的情况下看起来不太好。如果您可以更改布局以便隐藏控件不会导致残留工件,那么这是一个选项。
为什么不使用 asp 转发器来显示文件上传?
您可以轻松设置html模板和设计,因为DataSource使用您数据的DataTable
<asp:repeater id='rp' runat='server'>
<ItemTemplate>
<%Eval("UploadType"%> -this is the caption
<asp:fileUpload id='file' runat='server'/>
<asp:Button id='btnUpload' onclick='UploadClick' runat='server'/>
</ItemTemplate>
protected void lstLegalEntity_SelectedIndexChanged(object sender, EventArgs e)
{
//get data into DataTable
rp.DataSource=dt;
rp.DataBnd();
}
protected void UpLdButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < tblRow; i++)
{
//this way you get the relevant fileUpload
fileUp = (FileUpload)((Button)Sender).Parent.FindControl ("file);
//rest of code to save file
}
}
我有不同的实体类型,根据点击的实体(下拉列表),每次需要上传的数量和类型都不同。
因此,我在动态 table 中创建了多个动态上传控件,并使用动态上传按钮同时上传所有文件(我还尝试在 asp.net 页面上添加一个按钮以及)。一切都很好,我可以选择要上传的文件。
我遇到的问题是动态按钮控件没有触发它的 onclicked 事件,因此我无法保存文件。我尝试在 asp.net 侧创建一个确实会触发的按钮,但由于页面生命周期,它没有获取我的上传控件。
然后我尝试添加 OnInit 事件并在其中创建动态按钮,并在下拉列表的选定索引更改上创建其余的上传动态控件,但随后创建了除动态上传控件之外的所有内容。点击事件然后触发。 (Page_Init 做同样的事情)。
我希望按钮不是动态的,而是到达文件上传控件以保存文件。有没有办法绕过页面生命周期来实现这一点,或者有人可以告诉我我做错了什么吗?或者如何让动态按钮触发点击事件?
任何帮助将不胜感激....
这是我的代码:
protected void lstLegalEntity_SelectedIndexChanged(object sender, EventArgs e)
{
if (CTflag == false)
{
this.Rows = tblRow;
this.Columns = tblCol;
lblAmountOfRows.Text = "4";
CreateDynamicTable();
}
else
{
CTflag = true;
}
clrControls();
}
protected void CreateDynamicTable()
{
string filterstring = "";
filterstring = "SELECT let.ID, UploadType " +
"FROM [dbo].[AssetContract_LegalEntityLinks] lel " +
"INNER Join [dbo].[AssetContract_LegalEntity] le " +
"ON lel.LegalEntityRef = le.ID " +
"INNER JOIN [dbo].[AssetContract_LegalEntityTypes] let " +
"ON lel.LegalTypeRef = let.ID " +
"WHERE lel.LegalEntityRef = @LegalEntityRef ";
cn = new SqlConnection(GetConnectionString());
SqlCommand myCmd = new SqlCommand();
myCmd.CommandText = filterstring;
myCmd.Connection = cn;
myCmd.CommandType = CommandType.Text;
if (lstLegalEntity.SelectedValue != "")
{
myCmd.Parameters.AddWithValue("@LegalEntityRef", Convert.ToInt32(lstLegalEntity.SelectedValue));
}
else
{
myCmd.Parameters.AddWithValue("@LegalEntityRef", Convert.ToInt32(0));
}
cn.Open();
SqlDataReader myReader = myCmd.ExecuteReader();
tblRow = GetUploadControlsCount();
if (CTflag == false)
{
table.Caption = "Upload Requirements";
table.ID = "Upload Requirements";
table.BackColor = System.Drawing.Color.BurlyWood;
divFileUploads.Controls.Add(table);
for (i = 0; i < 1; i++)
{
row = new TableRow();
row.BorderStyle = BorderStyle.Ridge;
for (j = 0; j <= tblCol; j++)
{
cell = new TableCell();
cell.BorderWidth = 5;
cell.BorderStyle = BorderStyle.Ridge;
cell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j <= tblCol; j++)
{
string[] Header = { "Upload Type", "File" };
Label lbl = new Label();
lbl.ID = "lblHeader" + j;
if (j == 1)
{
lbl.Width = 220;
}
else
{
lbl.Width = 100;
}
lbl.Text = Header[j];
cell.Controls.Add(lbl);
}
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
int readCount = 1;
while (myReader.Read())
{
for (i = 0; i < 1; i++)
{
row = new TableRow();
row.ID = "rw" + myReader["UploadType"].ToString();
row.BorderStyle = BorderStyle.Ridge;
for (j = 0; j <= tblCol; j++)
{
cell = new TableCell();
cell.ID = tbColId + i + j + myReader["UploadType"].ToString(); ;
cell.BorderWidth = 5;
cell.BorderStyle = BorderStyle.Ridge;
cell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j <= 0; j++)
{
Label lbl = new Label();
lbl.ID = "lblCCRow" + i + "Col" + j + myReader["UploadType"].ToString();
lbl.Text = myReader["UploadType"].ToString();
lbl.Width = 100;
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (j = 0; j < 1; j++)
{
fileUp = new FileUpload();
//m = i; n = j;
fileUp.ID = filename + i + readCount.ToString();
fileUp.Width = 350;
cell.Controls.Add(fileUp);
cmdArg = fileUp.ID;
}
row.Cells.Add(cell);
}
table.Rows.Add(row);
readCount++;
}
i = 0;
j = 0;
}
for (i = 0; i < 1; i++)
{
rrow = new TableRow();
rrow.ID = "ResultRow";
rrow.BorderStyle = BorderStyle.Ridge;
rrow.HorizontalAlign = HorizontalAlign.Center;
for (j = 0; j <= tblCol; j++)
{
rcell = new TableCell();
rcell.ID = "resultCol" + j;
rcell.BorderWidth = 5;
rcell.BorderStyle = BorderStyle.Ridge;
rcell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j < 1; j++)
{
btnUpload = new Button();
btnUpload.Width = 100;
btnUpload.Text = "Upload";
btnUpload.ID = btnUpload.Text;
rcell.Controls.Add(btnUpload);
btnUpload.Click += new EventHandler(UpLdButton_Click);
}
rrow.Cells.Add(rcell);
}
table.Rows.Add(rrow);
}
CTflag = true;
ViewState["dynamictable"] = true;
cn.Close();
myReader.Close();
}
}
protected void UpLdButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < tblRow; i++)
{
fileUp = (FileUpload)FindControlRecursive(this, string.Format("fileUpLoader{0}{1}", 0, i));
//rest of code to save file
}
}
当您在 OnInit 事件中添加动态按钮时,您走在了正确的轨道上 - 您只需要也添加 FileUpload 控件。
所有控件都需要在每次回发时重新创建。 .NET 自动处理您添加到 ASPX 页面的控件。您对以编程方式添加的动态控件负责。
您可以根据自己所在的位置采取以下几种方法:
继续动态添加控件,但进行更改以便添加所有控件,而不仅仅是按钮。
如果您知道控件的最大数量,那么您可以添加所有控件并使用 Visible 属性 控制显示的内容。这在控件数量较少时有效。看起来你在 table 单元格周围放置了边框,所以这在你的情况下看起来不太好。如果您可以更改布局以便隐藏控件不会导致残留工件,那么这是一个选项。
为什么不使用 asp 转发器来显示文件上传?
您可以轻松设置html模板和设计,因为DataSource使用您数据的DataTable
<asp:repeater id='rp' runat='server'>
<ItemTemplate>
<%Eval("UploadType"%> -this is the caption
<asp:fileUpload id='file' runat='server'/>
<asp:Button id='btnUpload' onclick='UploadClick' runat='server'/>
</ItemTemplate>
protected void lstLegalEntity_SelectedIndexChanged(object sender, EventArgs e)
{
//get data into DataTable
rp.DataSource=dt;
rp.DataBnd();
}
protected void UpLdButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < tblRow; i++)
{
//this way you get the relevant fileUpload
fileUp = (FileUpload)((Button)Sender).Parent.FindControl ("file);
//rest of code to save file
}
}