gpt4 book ai didi

c# - 在 Asp.Net 中检索动态创建的控件的值

转载 作者:行者123 更新时间:2023-11-30 21:10:07 25 4
gpt4 key购买 nike

我需要在 DropDownList 的 SelectedIndexChanged 事件上动态添加 CheckBoxList。我已经实现了这一点,但我无法在回发时保留它的值(value)。

这是我到目前为止所做的:

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
loadTracks();//Needs to generated dynamically
}

protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
loadDegrees();
}
loadTracks();
}

public void loadTracks()
{
try
{
ConfigurationDB objConfig = new ConfigurationDB();
DataSet ds = objConfig.GetTracksByDegreeID(
Convert.ToInt32(ddlDegree.SelectedValue.ToString()));
CheckBoxList CbxList = new CheckBoxList();

CbxList.ID = "Cbx";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
CbxList.Items.Add(new ListItem(ds.Tables[0].Rows[i]["Track_Name"]
.ToString(), ds.Tables[0].Rows[i]["ID"].ToString()));
}
ph.Controls.Add(CbxList);
ViewState["tracks"] = true;
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}

//For testing, I added a button and on its click I have added this code

protected void btnDetails_Click(object sender, EventArgs e)
{
CheckBoxList Cbx = (CheckBoxList)ph.FindControl("chk");
foreach (ListItem ex in Cbx.Items)
{
if (ex.Selected)
{
Response.Write(String.Format("You selected: <i>{0}</i> <br>", ex.Value));
}
}
}

最佳答案

可能是错字:

CbxList.ID = "Cbx";

对比

CheckBoxList Cbx = (CheckBoxList)ph.FindControl("chk");

关于c# - 在 Asp.Net 中检索动态创建的控件的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8656253/

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