Showing posts with label property. Show all posts
Showing posts with label property. Show all posts

Saturday, March 24, 2012

Please help about Javascript for Asp.net Button

I have to validate so many controls at a time.I dont know what to Write in ControltoValidateProperty property in this case.So I went for writing Javascript function.

I wrote a function in javascript called "validateform" which checks all controls and validates the form.

Now when User clicks Submit Button I want to use this function.
The Problem is When i included that code in Button.attributes.add(...),Its giving me errormessages but also executing the server side code for button.

please help me

thxHow are you calling the Javascript function, from an OnClick event in the submit button or from an onSubmit call from the form. In either case you need to return false from the javascript function if a field does not validated. This will prevent the form from posting back and executing the server side code. If you could provide a code example, it would be easier for me to determine what is going wrong.
this is sample code

javascript :


function va()
{
if(document.all.p_title1.value=="")
{
alert("Team Name required");
return false;
}
else
return true;
}

Page Load :

ContinueButton.Attributes.Add("onclick","va();");

Button Server Event :


private void ContinueButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Response.Redirect("SelectMaterial.aspx");

}

thx
I noticed the other day that if you don't specify that you want a value returned in the onclick event as well as returning the value from the function that the form postsback to the server regardless of the Javascript function return value, with that said, all you need to do is change you attributes.add statement like so.


ContinueButton.Attributes.Add("onclick","return va();");

That should solve the postback problem.
thank you very much.
It worked

Please help Friends how to control maxlength textbox in datagrid

Is there a way to set the maxlength property for a text box in the datagrid.

My database table field size is only 10 chars for first name.

How can i make the user not to enter more tha 10 chars on the front end.

Is there any Max length property i can assign for the text boxes.

I have 4 text boxes in datagrid.

Thank you very much.Hi,

if it is single-line TextBox, you can use its MaxLength property like you normally can.

<asp:TextBox ID="xxx" MaxLength="4"... /
This works even if TextBox is in a DataGrid.
Thanks Joteke, I have the following code triggered when i click edit in the datagrid, then it will show all text boxes etc. where can i place the maxlength="4" property in the following code.

Thank you very much.

< Code >
Sub MyDataGrid_UpdateCommand(s As Object, e As DataGridCommandEventArgs )
Dim conn As SqlConnection
Dim MyCommand As SqlCommand
Dim strConn as string = "server=Rajender;uid=sa;pwd=sa;database=NORTHWIND"
Dim txtFirstName As textbox = E.Item.cells(2).Controls(0)
Dim txtLastName As textbox = E.Item.cells(3).Controls(0)
Dim txtTitle As textbox = E.Item.cells(4).Controls(0)
Dim strUpdateStmt As String
strUpdateStmt =" UPDATE Employees SET" & _
" FirstName =@.Fname, LastName =@.Lname, Title = @.Title " & _
" WHERE EmployeeID = @.EmpID"
conn = New SqlConnection(strConn)
MyCommand = New SqlCommand(strUpdateStmt, conn)
MyCommand.Parameters.Add(New SQLParameter("@.Fname", txtFirstName.text))
MyCommand.Parameters.Add(New SQLParameter("@.Lname", txtLastName.text))
MyCommand.Parameters.Add(New SQLParameter("@.Title", txtTitle.text))
MyCommand.Parameters.Add(New SQLParameter("@.EmpID", e.Item.Cells(1).Text ))
conn.Open()
MyCommand.ExecuteNonQuery()
MyDataGrid.EditItemIndex = -1
conn.close
BindData
End Sub

</ code >
In that case, you could declare EditItemTemplate for the DataGrid (declaratively in asox) in which you set the TextBox's MaxLength.

If you want to do it programmatically, then you'd do it in ItemDataBound method (when ItemType is EditItem)

Friday, March 16, 2012

Please help override property

Hi,
I've never developed in CSharp before and I'm trying to adapt an
existing user control written in CSharp. The user control inherits from
the DataGridColumn. I'd like for one of my controls properties to
override that of the DataGridColumn. Could somebody help with the
syntax? Currently my code for the property is as follows:
public virtual string HeaderStyle
{
get
{
object savedState = null;
savedState = this.ViewState["HeaderStyle"];
if (savedState != null)
{
return (string)savedState;
}
return "";
}
set
{
this.ViewState["HeaderStyle"] = value;
}
}
When I try to compile I get the message 'HeaderStyle hides inherited
member system.web.ui.webcontrols.datagridcolumn.headerstyle. To make
the current member override that implementation, add the override
keyword. Otherwise add the new keyword'. Please could someone confirm
that instead of
public virtual string HeaderStyle
I should have
public overrides string HeaderStyle
Thanks,
PaulI think it should be "override", without the "s".
An "override" is actually a "virtual" but it emphasize that it override
the virtual function declared in one of its base class. The thing is it
makes the purpose clearer, and avoid the case of accidental overriding
when you actually want a new virtual method, and acidentally create a
new virtual method while you actually want to overriding.