gpt4 book ai didi

c# - 我需要从页面上生成的动态控件中获取按钮点击的值(value)

转载 作者:行者123 更新时间:2023-11-30 17:33:34 25 4
gpt4 key购买 nike

生成了很多控件我想在页面上动态生成的按钮单击上保存值,任何人都可以帮助我解决这个问题。我是这个领域的新手,所以我从代码中使用了许多类型的代码,但它不能..

当我们使用 Page_Init 时,在这里我们得到所有 ID,我们只需要 ControlId 而不能得到 Value 并且从 Page_Init 我们不能得到复选框和单选按钮 ID 如果复选框和单选按钮被选中,那么 ID 就会出现,否则不会出现,值是也不来了...

protected void Page_Init(object sender, EventArgs e)
{
List<string> keys =
Request.Form.AllKeys.Where(key => key.Contains("MainContent$")).ToList();
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["ID"] != null)
{

CreateDynamicForm(Convert.ToInt32(Request.QueryString["ID"]));
}
}
}

protected void CreateDynamicForm(int id)
{
DataTable dt = objDl.FetchList(id);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{

string text = Convert.ToString(dt.Rows[i]["ControlToDisplay"]);
string chkval = Convert.ToString(dt.Rows[i]["IsActive"]);
if (text == "Checkbox")
{
CheckBox chk = new CheckBox();
chk.ID = "Checkbox" + i;
string ID = chk.ID;

chk.Attributes["style"] = "margin-left: 87px;line-
height: 3; ";
if (chkval == "True")
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}

Label lbl = new Label();
lbl.Text = Convert.ToString(dt.Rows[i]["FieldLabel"]);

panelDynamic.Controls.Add(lbl);
panelDynamic.Controls.Add(chk);
Literal lt = new Literal();
lt.Text = "<br />";
panelDynamic.Controls.Add(lt);

hdnID.Value = hdnID + "," + ID;
HdnType.Value = HdnType + "," + "Checkbox";
}

if (text == "Dropdownlist")
{
int j = 0;
DropDownList ddlList = new DropDownList();
ddlList.ID = "DdlDisplay" + i;
ddlList.CssClass = "form-group";
// ddlList.CssClass = "col-sm-3";
ddlList.Attributes["style"] = "margin-left: 96px;width:
151px; padding - left: 43px; ";
string texts = Convert.ToString(dt.Rows[i]
["OptionValue"]);
string[] txtcount = texts.Split(',');
foreach (var items in txtcount)
{

if (items.TrimStart().TrimEnd() != "")
{
j++;
ListItem ltm = new ListItem();
//ltm.Attributes["style"] = "padding-left:
43px; ";
ltm.Value = "Listitem" + j;
ltm.Text = Convert.ToString(items);

//ddlList.Items.Add(txt);
ddlList.Items.Add(new ListItem(ltm.Text,
ltm.Value));

}
}

hdnID.Value = hdnID + "," + ddlList.ID;
HdnType.Value = HdnType + "," + "Dropdownlist";



Label lbl = new Label();
lbl.Text = Convert.ToString(dt.Rows[i]["FieldLabel"]);
panelDynamic.Controls.Add(lbl);

panelDynamic.Controls.Add(ddlList);

Literal lt = new Literal();
lt.Text = "<br />";
panelDynamic.Controls.Add(lt);
}

if (text == "SingleLineTextBox")
{

TextBox textb = new TextBox();
textb.ID = "TextBox" + i;
textb.Attributes["style"] = "margin-left: 87px;";
textb.Attributes["required"] = "true";


Label lbl = new Label();
lbl.Text = Convert.ToString(dt.Rows[i]["FieldLabel"]);
panelDynamic.Controls.Add(lbl);

panelDynamic.Controls.Add(textb);

Literal lt = new Literal();
lt.Text = "<br />";
panelDynamic.Controls.Add(lt);

hdnID.Value = hdnID + "," + textb.ID;
HdnType.Value = HdnType + "," + "SingleLineTextBox";
}

if (text == "Multiline Textbox")
{

TextBox textMulti = new TextBox();
textMulti.ID = "MultiTextBox" + i;
textMulti.TextMode = TextBoxMode.MultiLine;
textMulti.Attributes["style"] = "margin-left:
87px; margin - top: 16px; ";
textMulti.Attributes["required"] = "true";

Label lbl = new Label();
lbl.Text = Convert.ToString(dt.Rows[i]["FieldLabel"]);
panelDynamic.Controls.Add(lbl);

panelDynamic.Controls.Add(textMulti);

Literal lt = new Literal();
lt.Text = "<br />";
panelDynamic.Controls.Add(lt);

hdnID.Value = hdnID + "," + textMulti.ID;
HdnType.Value = HdnType + "," + "Multiline Textbox";

}

if (text == "RadioButton")
{
RadioButton rdb = new RadioButton();
rdb.ID = "rdb" + i;
rdb.Attributes["style"] = "margin-left: 87px;line-
height: 4";

Label lbl = new Label();
lbl.Text = Convert.ToString(dt.Rows[i]["FieldLabel"]);
panelDynamic.Controls.Add(lbl);

panelDynamic.Controls.Add(rdb);

Literal lt = new Literal();
lt.Text = "<br />";
panelDynamic.Controls.Add(lt);

hdnID.Value = hdnID + "," + rdb.ID;
HdnType.Value = HdnType + "," + "RadioButton";
}

}
}
}

protected void btn_submit_Click(object sender, EventArgs e)
{

}

最佳答案

这里有一个完整的演示片段,说明如何处理动态控件的创建和读取值。然后,您可以根据自己的需要对其进行调整。

protected void Page_Load(object sender, EventArgs e)
{
createDynamicControls();
}

public void createDynamicControls()
{
//add a textbox
TextBox tb = new TextBox();
tb.ID = "DynamicTextBox";
tb.Text = "TextBox Content";
PlaceHolder1.Controls.Add(tb);

//add a dropdownlist
DropDownList drp = new DropDownList();
drp.ID = "DynamicDropDownList";
drp.Items.Insert(0, new ListItem("Value A", "0", true));
drp.Items.Insert(1, new ListItem("Value B", "1", true));
PlaceHolder1.Controls.Add(drp);

//add a button
Button btn = new Button();
btn.Text = "Submit Dynamic Form";
btn.Click += Button1_Click;
PlaceHolder1.Controls.Add(btn);
}

protected void Button1_Click(object sender, EventArgs e)
{
//find the dynamic controls again with findcontrol
TextBox tb = FindControl("DynamicTextBox") as TextBox;
DropDownList drp = FindControl("DynamicDropDownList") as DropDownList;

//visualize the values
Label1.Text = tb.Text + "<br>";
Label1.Text += drp.SelectedItem.Text;
}

aspx

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>

关于c# - 我需要从页面上生成的动态控件中获取按钮点击的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43731772/

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