获取TextBox的值 [英] Getting the value of a TextBox

查看:89
本文介绍了获取TextBox的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文本框和一个按钮。一个是用户输入他的文本,另一个是加扰字母并输出它们。一旦用户在第一个文本框中写入文本,如何在按钮上分配第一个文本框的值,单击一个我稍后输出的字符串?

I've got 2 textboxes and a button. One is for the user to input his text and the other is to scramble the letters and output them. Once the user writes his text in the first text box, how do I assign the value of the first textbox on the buttons click to a string that I'll output later?

推荐答案

在按钮点击事件上写下代码

write below code on button click event
string xyz = TextBox1.Text.ToString();


我想你需要类似下面的代码: -

设计页面代码: -

I guess you need something like the below code :-
Design page code :-
<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>]]>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="width: 100%;">
            <tr>
                <td><asp:label id="Label1" runat="server" text="Enter First Input" xmlns:asp="#unknown"></asp:label></td>
                <td> <asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown"></asp:textbox></td>
            </tr>
            <tr>
                <td><asp:label id="Label2" runat="server" text="Enter Second Input" xmlns:asp="#unknown"></asp:label></td>
                <td> <asp:textbox id="TextBox2" runat="server" xmlns:asp="#unknown"></asp:textbox></td>
            </tr>
            <tr>
                <td><asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" xmlns:asp="#unknown" /></td>
                <td> </td> 
            </tr>
        </table>
    </div>
    </form>
</body>
</html>





代码隐藏页面代码=





Code behind page code =

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox2.Text = scramble(TextBox1.Text);
    }

    private string scramble(string inputString)
    {
        string output = inputString;

        /* your logic to scramble the input string */

        return output;
    }
}





希望它能为您提供帮助。



Hope it will help you.


1.在页面后面的代码中找到buttonclick事件(方法)

2.找到文本框的ID,或者给它一个像id =txt1的id。

3.点击按钮点击事件:

字符串输入=(在这里给出你的文本框id)txt1.Text;

ScrambleWord(输入)

4.找到第二个文本框的ID或给它一个像id =text2的id。

text2.Text = ScrambleWord(输入);



public string ScrambleWord(string word)

{char [] chars = new char [word.Length];

随机rand = new Random(10000);

int index = 0;

while(word.Length> 0)

{

int next = rand.Next(0,word.Length - 1);

chars [index] = word [next];

word = word.Substring(0,next)+ word.Substring(next + 1);

++指数;

}返回新字符串(字符);

}
1.In code behind page find the buttonclick event(method)
2.Find the ID of the textbox,or give it an id like id ="txt1".
3.Inside the button click event :
string input= (give your text box id here) txt1.Text;
ScrambleWord(input)
4.Find the ID of the second textbox or give it an id like id="text2".
text2.Text=ScrambleWord(input);

public string ScrambleWord(string word)
{ char[] chars = new char[word.Length];
Random rand = new Random(10000);
int index = 0;
while (word.Length > 0)
{
int next = rand.Next(0, word.Length - 1);
chars[index] = word[next];
word = word.Substring(0, next) + word.Substring(next + 1);
++index;
} return new String(chars);
}


这篇关于获取TextBox的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆