Home » Asp.netRSS

disable required field validator at server side

Hi everyone,

my question is very simple. I had read through many post include this: http://blog.anasghanem.com/post/How-to-get-the-readonly-textbox-value-on-the-server-.aspx

I have a calender extender and the date was target to a texbox. This textbox, i had set it to readonly and it is validate by a required field validator. so mean this texbox must have a value inside. my prob is, i bind the textbox to database and if data is NULL or empty i can't save it because the required field validator was validate textbox as empty value at cllient side. If i remove the required field validator everthing fine. But this is not i want. I have try to set the textbox to javascript onCopy,onPaste,onkeyup. but user also can delete and paste into the textbox.

i have think to disable the requried field validtor at server side when data is inserted. Anyone know how?

 

16 Answers Found

 

Answer 1

Hi Friend,

You just add following code in your page load

txtDate.Attributes.Add("readonly", "readonly");

and read  your valu of text box as like you read for any other normal textbox

string date=txtDate.Text;

prasadP

 

Answer 2

seems your question  is very complicated.

validator control is trying to validate  the value of controls like textbox  on client side.

so javascript regular expression can also do the work. 

 

 

Answer 3

prasadP:

Hi Friend,

You just add following code in your page load

txtDate.Attributes.Add("readonly", "readonly");

and read  your valu of text box as like you read for any other normal textbox

string date=txtDate.Text;

prasadP

Thanks friend, but i got Object reference not set  to an instance of an object error. textbox  in load page, this textbox is inside  a detail view

 

Answer 4

davilee:

seems your question  is very complicated.

validator control is trying to validate  the value of controls like textbox  on client side.

so javascript regular expression can also do the work. 

It may seem complicated for you if you don't read  my ques carefully, but actually what my different than other is my data  contain null  or empty  value, so if you disable  or readonly  = true for textbox, the validator  will treat the textbox as empty cause it run on client.

I use the code: txtdob.Text = Server.HtmlEncode(Request.Form(dob.UniqueID)), but it only work for textbox "already" have value.

So, if does like what you said, javascript  also validate when the textbox is in null value, what the different using javascript alert rather than Rfieldvalidate?

 

Answer 5

try this:

string.IsNullOrEmpty(Server.HtmlEncode(Request.Form(dob.UniqueID))) ? "no uniqueid" : Server.HtmlEncode(Request.Form(dob.UniqueID));

 

Answer 6

SORRY, I don't know how to read  this, can you convert in vb? what is (? "no uniqueid" :)?


 

Answer 7

Hi Friend,

try this

<asp:DetailsView runat="server" ID="FormView1" OnItemCreated="FormView1_ItemCreated">

...................................

</asp:DetailsView>

 

void FormView1_ItemCreated(object sender, EventArgs e)
{
 TextBox t = ((TextBox)FormView1.Rows[0].FindControl("TextBox1"));
 t.Attributes.Add("readonly", "readonly");
}

prasadP

 

Answer 8

below is translation:

string id = Server.HtmlEncode(Request.Form(dob.UniqueID));

if(string.IsNullOrEmpty(id))

       id = "no uniqueid";

 

 

Answer 9

after change to

Protected Sub DetailsView1_ItemCreated(ByVal sender As Object, ByVal e As EventArgs)
        Dim dob As TextBox = DirectCast(DetailsView1.Rows(0).FindControl("txtDOB"), TextBox)
        dob.Attributes.Add("readonly", "readonly")
    End Sub

still have error. Is System.NullReferenceException error

 

Answer 10

Hi,

Add one line:

DetailsView1.ChangeMode(DetailsViewMode.Edit)

Dim dob As TextBox = DirectCast(DetailsView1.Rows(0).FindControl("txtDOB"), TextBox)
dob.Attributes.Add("readonly", "readonly")

 

 

Answer 11

Jerry Weng - MSFT:

Hi,

Add one line:

DetailsView1.ChangeMode(DetailsViewMode.Edit)

Dim dob As TextBox = DirectCast(DetailsView1.Rows(0).FindControl("txtDOB"), TextBox)
dob.Attributes.Add("readonly", "readonly")

Hi, I got this error message:

Server Error in '/ServayMembershipProgram' Application.


Object reference not set  to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 518:        DetailsView1.ChangeMode(DetailsViewMode.Edit)
Line 519: Dim dob As TextBox = DirectCast(DetailsView1.Rows(0).FindControl("txtDOB"), TextBox)
Line 520: dob.Attributes.Add("readonly", "readonly")
Line 521: End Sub
Line 522:

 

Answer 12

Does Row 0 contain a textbox  by ID txtDOB. I hope Row 0 is not the headerRow?? 

 

Answer 13

Hi,

Try this:

Dim dob As TextBox = DirectCast(DetailsView1.FindControl("txtDOB"), TextBox)
 

 

Answer 14

stilll got the same error:(

 

Answer 15

vijayst:

Does Row 0 contain a textbox  by ID txtDOB. I hope Row 0 is not the headerRow?? 

Row 0 is what row? I don't understand you. I just change to

Dim dob As TextBox = DirectCast(DetailsView1.FindControl("txtDOB"), TextBox)

like what jerry said, but the same error still occur.

 

Answer 16

If someone else comes across this post  with the same problem, which I had, then I solved it this way.

In my page load, I set  the value of the textbox  that was readonly  to a specific value.  In my case the readonly text box was a date, so I set it to today's date.  Then the jQuery date  selector worked it's magic and the value was saved properly, and the required  field validator  can be removed...well you have a valid date in place now, and it's really the user's fault if they don't change the date to something more appropriate for it's purpose, and the user  can't empty  the field.

Anyways, set a default value and it's all good to go, but you will still need to ask for the correct value via Request.Form[txtDateOf.UniqueID] as the standard way will always contain the default value.

Colin

http://you.arenot.me

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter