C#动态添加控件 textbox

C#动态添加控件 textbox,第1张

你这样是肯定没有的,因为click事件点击后立即执行完了,根本不能等待你输入的结果的。

理应的形式是:点击后出现textbox然后对话框d出空。你的输入是没法赋值的。

如果要显示,将 String S

TextBox txt = new TextBox()

txt.SetBounds(250, 0, 200, 400)

this.Controls.Add(txt)

txt.Enabled = true

放在构造函数或者form_load事件中。

这样写:

TextBox txt = new TextBox()

String S

private void button1_Click(object sender, EventArgs e)

{

S = txt.Text

MessageBox.Show(S)

}

private void Form1_Load(object sender, EventArgs e)

{

txt.SetBounds(250, 0, 200, 400)

this.Controls.Add(txt)

txt.Enabled = true

}

protected void Page_Load(object sender, EventArgs e)

{

}

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

{

string Txt = TextBox1.Text

DropDownList1.Items.Add(new ListItem(Txt, Txt))

ListBox1.Items.Add(new ListItem(Txt, Txt))

}

protected void ImageButton2_Click(object sender, ImageClickEventArgs e)

{

int index = DropDownList1.SelectedIndex

DropDownList1.Items.RemoveAt(index)

}

protected void ImageButton3_Click(object sender, ImageClickEventArgs e)

{

int index = ListBox1.SelectedIndex

ListBox1.Items.RemoveAt(index)

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

if (sender.GetType().Name == "DropDownList")

{

TextBox1.Text = DropDownList1.SelectedItem.Text

}

else {

TextBox1.Text = ListBox1.SelectedItem.Text

}

}

<html xmlns=" http://www.w3.org/1999/xhtml">

<head runat="server">

<title>无标题页</title>

<script type="text/javascript">

function vali() {

var index = document.getElementById("ListBox1").selectedIndex

if (index <0) {

alert("请选择!")

return false

}

return true

}

</script>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/img/01.jpg"

onclick="ImageButton1_Click" />

<br />

<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/img/01.jpg"

onclick="ImageButton2_Click" />

<br />

<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/img/01.jpg"

onclick="ImageButton3_Click" OnClientClick="javascript:return vali()"/>

<br />

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"

onselectedindexchanged="DropDownList1_SelectedIndexChanged">

</asp:DropDownList><br />

<asp:ListBox ID="ListBox1" runat="server"

onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True"></asp:ListBox><br />

</div>

</form>

</body>

</html>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11780379.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存