c#.net中,如何获取控件的所有属性,界面上有个TextBox, 一个按钮,一个listview点击按钮将Textbox属性获

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#.net中,如何获取控件的所有属性,界面上有个TextBox, 一个按钮,一个listview点击按钮将Textbox属性获相关的知识,希望对你有一定的参考价值。

将textbox的所有属性都显示到listview中。
另外,如果说同一个程序中,还有个从窗体,那个窗体上也有个Textbox,又如何获取另外一个窗体的Textbox所有属性呢?
急!!!回答全面的加分,谢谢!!

1 :
for (int i = 0; i < textbox.GetType().GetProperties().Length; i++)

MessageBox.Show(button1.GetType().GetProperties()[i].Name);


2:
form1:
form2 f=new form2(this);
f.ShowDialog();
form2:
form1 f=null;
构造函数:
public form2(form1 fm1)

f=fm1;

窗体load事件(或其他事件) :
for (int i = 0; i < f.textbox.GetType().GetProperties().Length; i++)

MessageBox.Show(button1.GetType().GetProperties()[i].Name);


添加到listview:
循环内:
for (int i = 0; i < textbox.GetType().GetProperties().Length; i++)

ListViewItem it=new ListViewItem();
it.SubItems[0].Text=button1.GetType().GetProperties()[i].Name;
ListView1.Items.Add(it);
参考技术A 1.反射
2.方法很多,最简单的是将一个窗体作为变量给另一个窗体
参考技术B 有个这样的控件

ASP.NET WebForm中JavaScript修改了页面上Label的值,如何在后台代码中获取

在用ASP.NET WebForm开发一个项目时,遇到如下的一个情况

页面上有一个Textbox控件,还有2个Label 控件. 当Textbox控件中的值更改时,两个Label控件上的值做相应的更改, 这一点是通过页面中嵌入的JavaScript来实现的.

但是,Label控件上的值更改后,在后端.cs代码中,通过Label.Text 并不能取到更改后的值。 

order.aspx页面代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="order.aspx.cs" Inherits="order"   %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>Order</title>
</head>
  <body>
    <form id="orderForm" runat="server">
      <div class="form-group">
            <label>Product Size</label>
             <asp:TextBox ID="productSize" runat="server" Text="" CssClass="form-control" type="number" onchange="calculateNumberOfPacks()" ></asp:TextBox>
          </div>
          <div class="row">
            <div class="col-md-6">
              <p>Number of 96 packs:&nbsp;&nbsp;
                 <asp:Label ID="lbl96Pack" runat="server"></asp:Label>
                </p>
            </div>
            <div class="col-md-6">
              <p>Number of 24 packs:&nbsp;&nbsp;
                 <asp:Label ID="lbl24Pack" runat="server"></asp:Label>
                </p>
            </div>
          </div>
        <div class="well">
            <asp:Button ID="submit" CssClass="btn btn-primary" runat="server" Text="Place Order" OnClick="submit_Click"  />
        </div>
          </form>


  </body>
    <script>
        //also do this on document load
        //capture the value of the herd size input
        window.onload = function () {
            calculateNumberOfPacks();
        }

        function calculateNumberOfPacks(){
            var productSize = document.getElementById(\'productSize\').value;
            var largePacks = 0;
            var smallPacks;

            //multiply it by 1.10
            productSize = productSize * 1.1;

            //Round it upwards
            productSize = Math.ceil(productSize);
            console.log(productSize);

            //work out how many packs are required. 
            largePacks = Math.floor((productSize / 96));
            console.log("Large Packs: " + largePacks);
            smallPacks = Math.ceil((productSize - (largePacks * 96)) / 24);
            console.log("Small Packs: " + smallPacks);

            //Now inject the value back into the dom
            document.getElementById(\'lbl96Pack\').innerHTML = largePacks;
            document.getElementById(\'lbl24Pack\').innerHTML = smallPacks;
           }        
    </script>
</html>

 

页面如上面,当Textbox中的值由200改为其他值,下面的两个Label的值也会跟着更改

但是,在order.aspx.cs 后台代码中,如下获取:

this.lbl96Pack.Text , this.lbl24Pack.Text 来获取Lable的值,发现获取不到更新后的值.

原因:

你在ASPX页面上用javascript来更新了服务器端控件Label上的值,你需要回发才能使你的服务器端代码来反映这些更改.

解决方法:

创建两个隐藏的客户端控件<input>控件,把更新后的Label的两个值,同时使用JavaScript来更新到这两个隐藏的客户端控件上。在order.aspx.cs后台代码中,直接获取这两个隐藏的客户端控件的value

修改后的order.aspx 代码如下:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="order.aspx.cs" Inherits="order"   %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>Order</title>
</head>
  <body>
    <form id="orderForm" runat="server">
      <div class="form-group">
            <label>Product Size</label>
             <asp:TextBox ID="productSize" runat="server" Text="" CssClass="form-control" type="number" onchange="calculateNumberOfPacks()" ></asp:TextBox>
          </div>
          <div class="row">
            <div class="col-md-6">
              <p>Number of 96 packs:&nbsp;&nbsp;
                 <asp:Label ID="lbl96Pack" runat="server"></asp:Label>
                 <input type="hidden" name="hidden96Pack" id="hidden96Pack" value="">
                </p>
            </div>
            <div class="col-md-6">
              <p>Number of 24 packs:&nbsp;&nbsp;
                 <asp:Label ID="lbl24Pack" runat="server"></asp:Label>
                 <input type="hidden" name="hidden24Pack" id="hidden24Pack" value="">
                </p>
            </div>
          </div>
        <div class="well">
            <asp:Button ID="submit" CssClass="btn btn-primary" runat="server" Text="Place Order" OnClick="submit_Click"  />
        </div>
          </form>


  </body>
    <script>
        //also do this on document load
        //capture the value of the herd size input
        window.onload = function () {
            calculateNumberOfPacks();
        }

        function calculateNumberOfPacks(){
            var productSize = document.getElementById(\'productSize\').value;
            var largePacks = 0;
            var smallPacks;

            //multiply it by 1.10
            productSize = productSize * 1.1;

            //Round it upwards
            productSize = Math.ceil(productSize);
            console.log(productSize);

            //work out how many packs are required. 
            largePacks = Math.floor((productSize / 96));
            console.log("Large Packs: " + largePacks);
            smallPacks = Math.ceil((productSize - (largePacks * 96)) / 24);
            console.log("Small Packs: " + smallPacks);

            //Now inject the value back into the dom
            document.getElementById(\'lbl96Pack\').innerHTML = largePacks;
            document.getElementById(\'lbl24Pack\').innerHTML = smallPacks;

            document.getElementById(\'hidden96Pack\').value = largePacks;
            document.getElementById(\'hidden24Pack\').value = smallPacks;
           }        
    </script>
</html>

 

在order.aspx.cs代码中,如下来获取值

 protected void submit_Click(object sender, EventArgs e)
 {

    var Pack96 = Request.Form["hidden96Pack"].ToString().Trim();
     var Pack24 = Request.Form["hidden24Pack"].ToString().Trim();



}

 

这样,就完成了

 

以上是关于c#.net中,如何获取控件的所有属性,界面上有个TextBox, 一个按钮,一个listview点击按钮将Textbox属性获的主要内容,如果未能解决你的问题,请参考以下文章

VB.NET如何使控件不能响应KeyDown事件

datalist 控件中套用button控件,如何获取button控件从数据库中获取的id

C#,通过字符串获取控件以及给控件赋值

Winform 各种属性方法控件

winform各种属性方法控件

C#Winform中如何获取contextMenuStrip弹出的父控件的相关信息