Click here to Skip to main content
15,914,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 50 textboxes in a page. i set validators also.
When user forgets to fill the first textbox and clicks button at the bottom of the page i need the control should pass to the unfilled textbox.
how to code for that?
Posted

Hi,

On button click event, you can add the below code:

C#
if(string.IsNullorEmpty(TextBox1.Text))
{
TextBox1.Focus();
}


-Regards
 
Share this answer
 
Comments
sukumari1 14-Mar-13 2:30am    
i have more number of texboxes. What i want is to validate a page. If validators are true, the control should go to the textbox or dropdown
Anurag Sinha V 14-Mar-13 2:35am    
Hi,

Didnot understand what are you trying to say here.
Can you be more precise?
sukumari1 14-Mar-13 6:08am    
i have 25 textboxes on top of page and remaining at the bottom. Finally if user clicks submit button which is at the bottom i want to display "some fields are missing" if user forgets anything. i set reqd field validator, it's working but user wont be aware of that since the missing field is at the top of the page. So i want to display below the submit button "some fields are missing". i tried giving page.Isvalid still not working.
Anurag Sinha V 14-Mar-13 6:37am    
Hi, I understood your scenario.

The required field validators will actually show their text besides the textbox itself, to which they are applied.

So I suggest, in this case, you may remove all the required field validators and call a Javascript function on the Submit button click.Check whether the textboxes are filled and display an alert or write the error message on to a label near to the submit button.Something like this:
<script text="javascript">
function CallMe()
{
var x=document.getElementById("id of textbox").value;
if(x=="" || x== null)
{
alert("Some stuffs are missing");
}
}

You can call this Javascript function from the C# code behind on the button click as below:

protected void button_click()
{
Page.RegisterStartupScript(this.GetType(),"alert","CallMe();",true);
}

The reason why I have chosen Javascript to do this cause it will do client side validation and your app will not have to go to the server or any postback.

Hope it helps.

-Anurag
Anurag Sinha V 14-Mar-13 6:39am    
Ahh, the code aint visible properly in the comment section.I am writing the above code in another solution below.
Have a look at it and try.
Client side code:

JavaScript
<script type="text/javascript">

function CallMe()
{
var x= document.getElementById("id of textbox").value;
if(x=="" || x==null)
{
alert("Some stuffs are missing");
}
}
</script>


Then you can call this Javascript code from your C# code behind as below:

C#
protected void button_click(object sender, EventArgs e)
{
Page.RegisterStartupScript(this.GetType(),"alert","CallMe();",true);
}


Hope it helps.

-Anurag
 
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