how to get the name in label,when i enter the id in textbox?

Posted by Tejamanimala under ASP.NET on 10/8/2013 | Points: 10 | Views : 957 | Status : [Member] | Replies : 6
HI,i have textbox,and label....
when i enter the userid in textbox i want to display the name immediatly into label beside the textbox,that means without clicking on submit button i want to display the name,can any one please tell me how it is possible?

manimala


Responses

Posted by: Bandi on: 10/8/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down

        <asp:Label Text="text" runat="server" id="Label1"/>
<asp:TextBox runat="server" ID="txtBox" OnTextChanged="txtBox_TextChanged" />


Write TextBox_TextChanged event to change the label value dynamically based on text box value
protected void txtBox_TextChanged(object sender, EventArgs e)
{
string connectionstring = "Data source=XXXX; Initial Catalog=DBName; Integrated Security=true";
SqlConnection conn = new SqlConnection(connectionstring);
conn.Open();
SqlCommand comm = new SqlCommand("SELECT LAST_NAME FROM EMPLOYEES WHERE EMPLOYEE_ID = " + txtBox.Text + ";", conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);

if (ds.Tables[0].Rows.Count != 0)
{
Label1.Text = (String)ds.Tables[0].Rows[0][0]; //1st row and 1st column
}
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 10/9/2013 [Member] Starter | Points: 25

Up
0
Down
hi,but its not working,what should i do?

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 10/9/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Put AutoPostBack event as true.

<asp:TextBox runat="server" ID="txtBox" AutoPostBack="True" OnTextChanged="txtBox_TextChanged" />

Mark as answer if satisfied....

Regards,
Shree M.

Kavya Shree Mandapalli

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/9/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
The OnTextChanged will only occur if you make a change in the textbox and also tab out of the field. OR you can say AutoPostBack on text box will trigger postback when the focus is lost from the text box.

the OnTextChanged event is NOT fired while you are typing in the text box. OnTextChanged is a server-side event and only fires when the page is posted back. Typing into a text box on a page does not post the page back and so this event will only fire once page is posted by different way like: button click etc.. So you make sure that after the text is changed, you are moving out of text box.



Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Learningtorise on: 10/9/2013 [Member] Starter | Points: 25

Up
0
Down
After setting AutoPostBack to 'True' may cause page to flicker/refresh.

For better user experience, use javascript.

Example:
<input id="txtUsrName" type="text" onkeypress="myFunction()" >


<label id="lblUsrName" ></label>

//javascript function

function myFunction()
{
string text = document.getElementByID("txtUsrName").value;
document.getElementByID("lblUsrName").value = text;
}


P.S.: Written above code without testing, may give unexpected result.. BUT you get the idea!


http://hashtagakash.wordpress.com/

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
"Mark it as answer "

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response