Click here to Skip to main content
16,018,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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?
Posted
Updated 28-May-15 20:50pm
v2

write below code on button click event
C#
string xyz = TextBox1.Text.ToString();
 
Share this answer
 
v2
Comments
CPallini 29-May-15 3:01am    
5.
[no name] 29-May-15 3:03am    
thanx!! CPallini
Member 11727489 29-May-15 3:06am    
It says "The name "TextBox1" does not exist in current context"
[no name] 29-May-15 3:08am    
ohh... u must have given any ID for that textbox so just replace TextBox1 with that ID
[no name] 29-May-15 3:09am    
can u post ur code here?
I guess you need something like the below code :-
Design page code :-
C#
<![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 =

C#
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.
 
Share this answer
 
Comments
Member 11727489 29-May-15 3:33am    
Thank you a lot!
Member 11727489 31-May-15 12:08pm    
This doesnt work, help? The second textbox doesnt show anything.
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);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900