Emular click() de un boton al darle Enter a un TextBox.

J

Juan Pablo MV

Hola, agradeceria mucho que me ayudaran.


Pasa lo siguiente, tango una función en la pagína que hace que al
darle click a un campo de texto esta emule el enter en un boton que yo
selecciono.


private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
TieButton(TextBox2,Button2);


}



}


private void TieButton(System.Web.UI.Contro­­l TextBoxToTie,
System.Web.UI.Control ButtonToTie)
{
//EnterPressed()
string formName = GetFormName(ButtonToTie);
string jsString =
"if(event.keyCode==13){"+
"document."+formName+".element­­s
'"+ButtonToTie.UniqueID+"').cl­­i ck();}";
if (TextBoxToTie is System.Web.UI.HtmlControls.Htm­­lControl)


((System.Web.UI.HtmlControls.H­­tmlControl)TextBoxToTie).Attr­i­butes.Add("onkeydown",jsStri­ng­);



else if (TextBoxToTie is
System.Web.UI.WebControls.WebC­­ontrol)


((System.Web.UI.WebControls.We­­bControl)TextBoxToTie).Attrib­u­tes..Add("onkeydown",jsString­);



else
{ // We throw an exception if TextBoxToTie is not of type
HtmlControl
or WebControl.
throw new ArgumentException("Control
TextBoxToTie should be derived
from either System.Web.UI.HtmlControls.Htm­­lControl or
System.Web.UI.WebControls.WebC­­ontrol", "TextBoxToTie");
}



}


protected string GetFormName(System.Web.UI.Cont­­rol sourceControl)
{
string formName;
try
{
int i=0;
System.Web.UI.Control c = sourceControl.Parent;
while(! (c is System.Web.UI.HtmlControls.Htm­­lForm)
&!
(c is
System.Web.UI.Page) && i<500)
{
c = c.Parent;
i++;
}
if(c is System.Web.UI.HtmlControls.Htm­­lForm)
formName=c.ClientID;
else
formName="forms(0)";
}
catch{formName="forms(0)";}
return formName;


}


private void Button2_Click(object sender, System.EventArgs e)
{
Label1.Text = "2";
//Page.


}


Hasta ahí todo va bien, pero el problema se presenta una vez emula el
boton, pues el resultado nunca aparece en la pantalla: "2", al ver esto


hice un seguimiento de ejecución en el código y pude ver que esta
haciendo dos veces el Page_Load , uno al principio de la carga , luego
sigue la ejecución del boton y por ultimo este vuelve al page_load(no
se si esto se el problema? ayudenme por favor...) Gracias.


Lo raro es que funciona bien, pero la página nunca hace la recarga del



valor que se le asigna al label.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top