gpt4 book ai didi

c# - 动态创建的按钮未触发按钮单击事件

转载 作者:太空宇宙 更新时间:2023-11-03 23:32:12 30 4
gpt4 key购买 nike

我在 DropdownListSelectedIndexChanged 事件上创建了动态控件。按钮是这些控件之一。我还为该按钮分配了事件,但调试器没有出现该单击事件。以下是我的代码。

protected void Page_Load(object sender, EventArgs e)
{
try
{
token = Session["LoginToken"].ToString();
if (!IsPostBack)
{
BindData();
fsSearch.Visible = false;
btnDownload.Visible = false;
}
else
{
foreach (HtmlTableRow row in (HtmlTableRowCollection)Session["dynamicControls"])
{
tblSearch.Rows.Add(row);
}
}
}
catch
{
}
}

private void BindData()
{
ddlReportName.DataSource = svcCommon.GetReports(token, out message);
ddlReportName.DataValueField = "Key";
ddlReportName.DataTextField = "Value";
ddlReportName.DataBind();
ddlReportName.Items.Insert(0, "--Select--");
}

protected void ddlReportName_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string[] reportInfo = ddlReportName.SelectedValue.Split('|');

Session["dynamicControls"] = null;
tblSearch.Rows.Clear();

HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cellFieldNameLbl = new HtmlTableCell();
HtmlTableCell cellFieldNameDdl = new HtmlTableCell();
HtmlTableCell cellOperatorLbl = new HtmlTableCell();
HtmlTableCell cellOperatorDdl = new HtmlTableCell();
HtmlTableCell cellValueLbl = new HtmlTableCell();
HtmlTableCell cellValueTxt = new HtmlTableCell();
HtmlTableCell cellOperatorRbtn = new HtmlTableCell();
HtmlTableCell cellAddMoreFilter = new HtmlTableCell();
Button btnAddMore = new Button();

DropDownList ddlColumn = new DropDownList();
DropDownList ddlOperator = new DropDownList();
TextBox txtValue = new TextBox();
RadioButtonList rbtnOperator = new RadioButtonList();

List<string> filterValues = svcCommon.GetSearchColumns(Convert.ToInt64(reportInfo[0]), token, out message);

fsSearch.Visible = btnDownload.Visible = filterValues.Count > 0 ? true : false;

ddlColumn.ID = "_ddlColumn0";
ddlOperator.ID = "_ddlOperator0";
txtValue.ID = "_txtValue0";
rbtnOperator.ID = "_rbtnOperator0";
btnAddMore.ID = "_btnAddMore0";


rbtnOperator.Items.Add("AND");
rbtnOperator.Items.Add("OR");
rbtnOperator.RepeatDirection = RepeatDirection.Horizontal;

btnAddMore.Text = "Add More";

btnAddMore.Click +=btnAddMore_Click;

ddlColumn.DataSource = filterValues;
ddlColumn.DataBind();

ddlOperator.DataSource = new List<string>()
{
"Equal",
"Not Equal",
"Less Than",
"Less Than Or Equal",
"Greater Than",
"Greater Than Or Equal",
"Start With",
"Not Start With",
"End With",
"Not End With",
"Contains",
"Not Contains",
"Between",
"Not Between",
"In",
"Not In"
};
ddlOperator.DataBind();

cellFieldNameLbl.InnerText = "Field Name:";
cellFieldNameDdl.Controls.Add(ddlColumn);
cellOperatorLbl.InnerText = "Operator";
cellOperatorDdl.Controls.Add(ddlOperator);
cellValueLbl.InnerText = "Value";
cellValueTxt.Controls.Add(txtValue);
cellOperatorRbtn.Controls.Add(rbtnOperator);
cellAddMoreFilter.Controls.Add(btnAddMore);

row.Cells.Add(cellFieldNameLbl);
row.Cells.Add(cellFieldNameDdl);
row.Cells.Add(cellOperatorLbl);
row.Cells.Add(cellOperatorDdl);
row.Cells.Add(cellValueLbl);
row.Cells.Add(cellValueTxt);
row.Cells.Add(cellOperatorRbtn);
row.Cells.Add(cellAddMoreFilter);

tblSearch.Rows.Add(row);
Session["dynamicControls"] = tblSearch.Rows;
}
catch (Exception ex)
{
}
}

protected void btnAddMore_Click(object sender, EventArgs e)
{
try
{

}
catch
{
}
}

最佳答案

在 asp.net webforms 中动态创建控件的问题是它们不会自动添加到 View 状态,因此不会发生回发事件。

这应该可以帮助您理解动态添加控件,并通过 View 状态管理它们 http://forums.asp.net/t/1900207.aspx?Creating+buttons+dynamically

或者,一种更简单的管理方法是让页面上的按钮不可见,然后在 selected_index_changed 事件中,将可见性切换为 true。

关于c# - 动态创建的按钮未触发按钮单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31780641/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com