Wednesday, March 28, 2012

Validating Bound Fields

Hi,

I have a DetailsView with first, last, and email fields. When Ilook in the sourc code they are BoundField tags. I was wonderinghow I can validate these. I know how to validate regular Textboxtags, but when I try to validate these, there's no ID for thevalidate control to select. I read that data bound fieldsautomatically validate before updating. However, when I submitempty fields for my boundfields, it gives a server error message ratherthan a nice user-friendly message. How do I fix this?

thanks!

One way to do it would be to wrap your insert methods in a try / catch scenario.

I typically hate to let exceptions dictate program flow, but your database is already doing validation, and will return an error when one exists.

Try

Insert command

Catch ex as exception.

If ex = "1001" Then

label1.text = "You tried to insert an invalid value"

End Try

There may be a better way.


Ok the bad news is you cant really validate boundfileds correctly unless you change your bound columns to be templatefields.

Remeber that when you change the bound field to a template field you need to validate the insertItemTemplate and the UpdateItemTemplate.

Cheers
Gregor


ok, thanks

Validating checkboxes

hi all,

can someone, please tell me how to validate the following checkboxes?

<divid="cbService"style="display: inline;">

<inputid="CheckBox2"runat="server"type=CheckBoxValue="Email"/>Email

<inputid="CheckBox3"runat="server"type=CheckBoxValue="Mainframe"/>Mainframe

<inputid="CheckBox6"runat="server"type=CheckBoxValue="JDL"/>JDL

<br/>

<inputid="CheckBox7"runat="server"type=CheckBoxValue="Tiburon"/>Tiburon

<inputid="CheckBox8"runat="server"type=CheckBoxValue="iasWorld"/>iasWorld

</div>


Thanks in advance

You have to use a "CustomValidator" and create a javascript function:

function ValidateCheckboxes(src, args)

{

if(!document.getElementById("<%=CheckBox2.ClientID %>").checked && !document.getElementById("<%=CheckBox3.ClientID%>").checked ...)

args.IsValid = false;

}

else

{

args.IsValide = true;

}


Thanks for your prompt response.

Just so I understand clearly, I have got to do this for chebobox2 through checkbox8?


Can someone, anyone, please help?

This javascript code is giving me error:

<scriptLanguage="JavaScript"Type="text/javascript">function ValidateCheckboxes(src, args)

{

if(!document.getElementById("<%=CheckBox1.ClientID %>").checked

&& !document.getElementById("<%=CheckBox2.ClientID%>").checked

&& !document.getElementById("<%=CheckBox3.ClientID %>").checked

&& !document.getElementById("<%=CheckBox4.ClientID %>").checked

&& !document.getElementById("<%=CheckBox5.ClientID %>").checked

&& !document.getElementById("<%=CheckBox6.ClientID %>").checked

&& !document.getElementById("<%=CheckBox7.ClientID %>").checked

&& !document.getElementById("<%=CheckBox8.ClientID %>").checked

&& !document.getElementById("<%=CheckBox9.ClientID %>").checked

&& !document.getElementById("<%=CheckBox10.ClientID %>").checked&& !document.getElementById("<%=CheckBox11.ClientID %>").checked

&& !document.getElementById("<%=CheckBox12.ClientID %>").checked)

args.IsValid =false;

}

else

{

args.IsValide =true;

}

</script>


The error is on the ELSE line.

It says, "}" expected.

However, when I add the "}", the validation seems to work but other controls disappear. What could I be doing wrong?

Thanks much


Try to put this javascript function at the end of your page. If it doesn't work, please put some of the aspx code (especially the controls that are disappearing in order to check it.


you have no opening bracket after the if brackets...

should look like this:

<scriptLanguage="JavaScript"Type="text/javascript">function ValidateCheckboxes(src, args)

{

if(!document.getElementById("<%=CheckBox1.ClientID %>").checked

&& !document.getElementById("<%=CheckBox2.ClientID%>").checked

&& !document.getElementById("<%=CheckBox3.ClientID %>").checked

&& !document.getElementById("<%=CheckBox4.ClientID %>").checked

&& !document.getElementById("<%=CheckBox5.ClientID %>").checked

&& !document.getElementById("<%=CheckBox6.ClientID %>").checked

&& !document.getElementById("<%=CheckBox7.ClientID %>").checked

&& !document.getElementById("<%=CheckBox8.ClientID %>").checked

&& !document.getElementById("<%=CheckBox9.ClientID %>").checked

&& !document.getElementById("<%=CheckBox10.ClientID %>").checked&& !document.getElementById("<%=CheckBox11.ClientID %>").checked

&& !document.getElementById("<%=CheckBox12.ClientID %>").checked)

{

args.IsValid =false;

}

else

{

args.IsValide =true;

}


Oops!! SorryEmbarrassed , LD50 is correct.


If you're using FireFox I highly suggest adding on FireBug. It is an excellent Javascript debugger.


Hi simflex,

I think you also select my comment (the one that I provided the code of the validation function) as an answer since this was the answer, eventhough it was missing a "}".

Thanks in advance.

validating cells of a gridview

hi,
is it possible to validate specific cells within a gridview, without generating complete new emptyrow-templates or edit-templates. this is very frustrating as i only need to validate just 2 cells out of 25.

thanks for helpingIf you want to use the Validation controls, you must use templates where you add the validation controls, there are no built in validation for the boundfiled. You can also use the events, such as updating, inserting of the data source control to build your own validation and cancel the command if the validation fails.
yes, this is what i thought, too.

for the last two days i've been fiddling around with the lookup-tables and validation of gridviews. i am stuck and desparately need your help.
at this point i have to say that your support is really great! i don't know if i would have learnt asp.net 2.0 (before that i only developed with ms access vb) that fast without your help or the help of other members of this forum

now back to my problem; i am using now simple examples for a better understanding:

i have a table with name, firstname, salutationID and so on in a table tblContact.
the salutation-value is in a lookuptable tblSalutation (SalutationID and Salutation are the fileds) this means tblContact only contains a number which represents a value in another table.
when opening the gridview for tblContact, it should display the following:
in normal mode: name, firstname, Salutation (not the SalutationID)
in edit mode: name, firstname, dropdownlist of all items within tblSalutation (but here's the big difference now: the dropdownlist shouldnot display the ID, but the field Salutation)
when updating the row, the corresponding ID to the selected Salutation should be stored in field tblContact.salutationid.

if you have developed in ms access there is a way of defining a dropdownlist containing multiple fields from a different source and you choose the column which will be used for other purposes (eg saving in another field).

i already know some bits and pieces how to do that
- selecting for the dropdownlist a second sqldatasource
- assigning the value of the dropdownlist to the field tblcontact.salutationid needs to be done on server side eg. using the onupdating event of the datagrid or onselectedindexchanged event of the dropdownlist.
but i am stuck. maybe this isnt just possible.

btw i really prefer to do this with gridview and not with formsview as i try to keep it plain and simple. if you have code examples, my language is vb.

thanks a LOT
The following code will use the Salutation field as the Text of the items in the DropDownList and the value of the item will be the value from the SalutationID:

<asp:DropDownList DataTextField="Salutation" DataValueField="SalutationID" SelectedValue='<%# Bind("SaluationID")' ....
When you bind a field to the SelectedValue proeprty of the DropDownList, the selected value will be passed to the @.SalutationID parameter of the UpdateCommand or InsertCommand.

You don't need to use any event to get the value or set the values.
thank you so much fredrik.
that way was also one of my first attempts but did not work as i used eval instead of bind...

last question: in normal mode (or view mode) i like to use a texbox (or anything similiar) instead.
can i do this and if yes, how?
Add the TextBox into a ItemTemplate, and the DropDown into a EditTemplate/InsertTemplate.
hi fredrik,
i need something which looks in normal mode like the other columns but displays the value of the lookuptable which is not a member of the gridview's datasource.

what you have suggested means to create a join for the gridview's datasource?
yes, that would work but as this is a huge gridview i would have to create a join to 4 lookup-tables...
Can you be more specific of your problem?

For example what do you want to accomplish?
hi fredrik,
the problem is still the one from above. i am using a simple example for a better understanding:

i have a table with name, firstname, salutationID and so on in a table tblContact.
the salutation-value is in a lookuptable tblSalutation (SalutationID and Salutation are the fileds) this means tblContact only contains a number which represents a value in another table.
the gridview should display name, firstname, Salutation (not the SalutationID)

as i said, it would be easy to handle this by creating a join select on the gridview's sqldatasource. but then i would have to do 4 join selects.
Why do you need four joins? If you want to get the text for the salutationID, don't you only need to join the table where the salutation is located?
sure, but i have 4 more fields whose value is represented in 4 other lookuptables.

let me explain this with the example of above:

tblContact: name, firstname, salutationid (refers to salutation in lookuptable1), positionid (refers to position in lookuptable2), countryid (refers to country in lookuptable3), titelid refers to titel in lookuptable4), and so on...

validating checkboxes - have to select only few checkboxes out of many and insert them int

Hi, I am a newbie,

I am trying to insert into Sql DB from a page. the page contains title, image and a checkbox where the user has to select only few checkboxes and when hit submit, only those selected items should be inserted. I am creating dynamically the title, image and a html input checkbox in a datagrid. here is how i am generating the checkbox. in datagrid.

<asp:TemplateColumn HeaderText ="Vote">
<ItemTemplate>
<input type ="checkbox" id ="IDs" name = "IDs" value = '<%# DataBinder.Eval(Container.DataItem, "000_filename") %>' runat ="server"/>
</ItemTemplate>
</asp:TemplateColumn>

Can anybody please help me with few tips and code, how to validate only 5 checkboxes out of all others and then insert. this is in C# 2005

Thanks in advance.

private void CheckboxValue()

{

CheckBox chkbox = newCheckBox();
TextBox txtbox= new TextBox();
foreach(DataGridItem itemin DataGrid1.Items )
{
chkbox=(CheckBox)item.FindControl("chkbox");
txtbox=(TextBox)item.FindControl("txtbox");
if(chkbox.Checked)
{
Response.Write(item.Cells[1].Text);
// write the required code for processing the checked
// records depending on the requirement.
}
}
}


Thank you so much, it works!!!

Validating controls in a datagrid

I am trying to find a way to validate a control in a datagrid. Specifically, I have a datagrid with textboxes in a footer template of the columns along with an Add button. (When the user clicks the add button, an Insert routine is called that adds the data entered into the textboxes in the footer of the grid and rebinds the grid. This works fine; the new data appear in the grid correctly.) The problem is that I need to require that the user enter values in all of the columns (all of the textboxes in the footer). I have placed a RequiredFieldValidator control just after the grid, but the control it references is not recognized. More specifically, I have a textbox in the footer called txtAddYear. Here's the code for the RequiredFieldValidator:

<asp:RequiredFieldValidator
id="ValidateTxtAddYear"
ErrorMessage="<br />Please enter data for all fields."
ControlToValidate="txtAddYear"
runat="server"
/>

Here's the error I get: Unable to find control id 'txtAddYear' referenced by the 'ControlToValidate' property of 'ValidateTxtAddYear'.

I could validate the input in my Insert routine, but I am hoping there is a simpler way of doing it with either a FieldValidator control or something like that.

Any ideas?

Thanks,

pPut the validators in the footer of the datagrid. If you don't want to do that, then you have to reference the textboxes through the datagrid, which will be a big pain in the ... to do from the html.

HTH,
Works beautifully! Thank you!

p.
Well, now there's another problem. The validator needs to kick in only when the Add button is clicked; otherwise, it's OK for the textboxes in the footer to remain empty. Is there a way to disable the validator and enable it only when the button is clicked? Perhaps something I can put into the click event procedure of the Add button?

Thanks,

p
Couple ways to go about this. If there are no other validators on the page (theentire page) then you can set all other buttons to CausesValidation="false", otherwise, you get to make a CustomValidator that checks all the conditions you're worried about. Having not actually had to do the latter before, I can only suppose how hard it would be. I'm sure there's a tutorial out there though.

HTH,

Validating control inside the datagrid

Hi,
i have dategrid contains one coulum called Action, this edititem template contains edit ,delete, (when edit click the following will appear ) update,cancel.
The thing when ever i click on edit the datagrid need to do some validations in edit mode, i have one textbox and dropdown list.
my code as follows
the code inside datagrid
<asp:TemplateColumn HeaderText="Rate ($/MWH)">
<HeaderStyle Width="10px"></HeaderStyle>
<ItemTemplate>
<asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_RATE") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=txtRate runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_RATE") %>'>
</asp:TextBox>
<asp:RequiredFieldValidator id="vldtrRate" runat="server" Font-Size="Smaller" ControlToValidate="txtRate" Display="Dynamic"
ErrorMessage="*Required"></asp:RequiredFieldValidator>
<asp:RangeValidator id="vldtrDgRate" runat="server" Font-Size="Smaller" ControlToValidate="txtRate"
Display="Dynamic" MinimumValue="1" MaximumValue="1000000000000" Type="Double" ErrorMessage="*Not a valid number"></asp:RangeValidator>
</EditItemTemplate>
</asp:TemplateColumn>
The following function is back end code trying to findout the control(updatelink buttion)
Private Sub dgMailerMain_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgMailerMain.ItemCreated
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.SelectedItem
Dim myTableCell As TableCell
myTableCell = e.Item.Cells(1)
Dim myDeleteButton As LinkButton
myDeleteButton = CType(myTableCell.FindControl("LinkButton2"), LinkButton)
myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete? All relevant information will be deleted as well.');")
End Select
End Sub
problem is it is unable to find the link button
please help me to come out of the problem

e.Item.Cells(1) means second datagrid column, are you sure that's the right column?
Try:
myDeleteButton = CType(e.item.FindControl("LinkButton2"), LinkButton)
you shoud find control regardless of cell in which is placed.

Hi,
first off, in the html you provided here i do not see a definition ofthe LinkButton2. According to your code it should be a secondcolumn (index 1, first column has index 0)
Also, you can use FindControl method of the DataGridItem itself insteadof using FindControl method of a specific cell (you might mix up theindex of the cell - starts with 0 for the first column - and you wouldnot have to worry about your code if later you change the order of thecolumns. Not speaking about the fact that you will have to write lesscode)
Hope this helps

please find my actual coding

<asp:datagrid id="dg" runat="server" Font-Size="X-Small" Width="100%" CellPadding="4" BackColor="White"
BorderWidth="1px" BorderStyle="None" BorderColor="#3366CC" OnCancelCommand="dg_Cancel" OnupdateCommand="dg_Update"
OnEditCommand="dg_Edit" OnDeleteCommand="dg_Delete" AutoGenerateColumns="False">
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="CHARGE_NO" ReadOnly="True" HeaderText="Charge No">
<HeaderStyle Width="8px"></HeaderStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Charge Description">
<ItemTemplate>
<asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_DESC") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=ddlCharge runat="server" DataTextField="CHARGE_DESC" DataValueField="CHARGE_ID" DataSource="<%# GetCharge %>">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Rate ($/MWH)">
<HeaderStyle Width="10px"></HeaderStyle>
<ItemTemplate>
<asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_RATE") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=txtRate runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_RATE") %>'>
</asp:TextBox>
<asp:CompareValidator runat="server" id="compPrimeNumber"
Operator="DataTypeCheck" Type="Integer"
Display="Dynamic" ControlToValidate="txtRate"
ErrorMessage = "You must enter an integer value." />
<asp:CompareValidator runat="server" id="compPrimeNumberPositive"
Operator="GreaterThan" Type="Integer"
Display="Dynamic" ValueToCompare="0"
ControlToValidate="txtRate"
ErrorMessage = "You must enter a value greater than zero." />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText=" Absorb">
<HeaderStyle Width="4px"></HeaderStyle>
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ABSORB") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton id="rdbAbsorb" runat="server" Text="Absorb" GroupName="rdbCharges"></asp:RadioButton><BR>
<asp:RadioButton id="rdbPassthrough" runat="server" Text="Pass Through" GroupName="rdbCharges"></asp:RadioButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Action">
<ItemTemplate>
<asp:LinkButton id="lkBtnEdit" runat="server" CausesValidation="False" CommandName="Edit">Edit</asp:LinkButton>
<asp:LinkButton id="lkBtnDelete" runat="server" CausesValidation="False" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton id="lkBtnCancel" runat="server" CausesValidation="False" CommandName="Cancel">Cancel</asp:LinkButton>
<asp:LinkButton id="lkBtnUpdate" runat="server" CausesValidation="False" CommandName="Update">Update</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:datagrid>

code behind
Private Sub dgMailerMain_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgMailerMain.ItemCreated
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.SelectedItem
Dim myTableCell As TableCell
myTableCell = e.Item.Cells(4)
Dim myDeleteButton As LinkButton
myDeleteButton = CType(myTableCell.FindControl("lkBtnUpdate"), LinkButton)
myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete? All relevant information willbe deleted as well.');")
End Select
End Sub
it is not finding the control
Are you rebinding the datagrid on each postback?
yes, but not for all postbacks, rebinding whenever required like ofter update/delete ...


Dim myTableCell As TableCell
myTableCell = e.Item.Cells(4)
Dim myDeleteButton As LinkButton
myDeleteButton = CType(myTableCell.FindControl("lkBtnUpdate"), LinkButton)
myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete? All relevant information willbe deleted as well.');")


Instead of all the above code, you would be better off finding the control directly from the Item property of the object e of type DataGridItemEventArgs something like below.
Dim myDelButton = CType(e.Item.FindControl("lkBtnUpdate"), LinkButton)
myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete? All relevant information willbe deleted as well.');")


tried doesn't work.

It is inside edititem template
In your code you are trying to locate controllkBtnUpdatebut it is defined in the EditItemTemplate and EditItem is not the type of the Item you are looking in. Your case statement is
Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.SelectedItem
and those templates do not contain the definition of the lkBtnUpdatebutton
Cheers

gr8 to see that..,

can u suggest me the solution..?


Srinivas,
Just see the associtivity of the message to your button. You are trying to associate the message, the before delete message to an update button?? That really doesn't make much sense to me. This Attributes.Add should be on the btnDelete as I see. Dont you??
Exactly, solution would be:
myDeleteButton = CType(myTableCell.FindControl("lkBtnDelete"), LinkButton)

Shravan,
myUpdateButton.Attributes.Add("onclick", "return Client_Validate();")

in this confirm is not the function i am going to use, that will be other function that returns true or false, inside that i will do validations
like follows
function Client_Validate()
{
var errormsg ="";
//validating Textbox
var Control=window.document.getElementById(document.all.tabCusotmer_txtQuotationStartDate.id);
if (Control.value=="")
{
errormsg = errormsg + "\n- Start Date Is Required";
document.all.divFromdate.style.visibility = "visible";
}
else
document.all.divFromdate.style.visibility = "hidden";
//Validating Dropdown list

Control=window.document.getElementById(document.all.tabCusotmer_ddlProductType.id);
if (Control.options[Control.options.selectedIndex].value ==0)
{
errormsg = errormsg + "\n- Product Type Is Required";
document.all.divddlProductType.style.visibility = "visible";
}
else
document.all.divddlProductType.style.visibility = "hidden";

//Validating Radiobutton
var Control_y=window.document.getElementById(document.all.tabCusotmer_rdbAdjusted.id);
var Control_n=window.document.getElementById(document.all.tabCusotmer_rdbNonAdjusted.id);
if (Control_y.checked || Control_n.checked )
document.all.divMeterType.style.visibility = "hidden";
else
{
document.all.divMeterType.style.visibility ="visible";
errormsg = errormsg + "\n- *Required";
}

if (errormsg =="")
return true;
else
{

return false;
}

}

I hope u understand wt my intension of doing, all client side validations, here is a situation where i can't use validators,
i have multiple tabs in my form, if i use validator control , i can't post back right?, untill all conditions are satisfied. in all tabs,
in my case i have so many add and next buttons.


Bug thanks for ur help,


This is still problem. I have datagrid code follows

*******************************************************************************************

<asp:datagrid id="dgAddStdFixPricing" runat="server" Width="100%" HorizontalAlign="Left" AutoGenerateColumns="False">
<SelectedItemStyle CssClass="GridSelected"></SelectedItemStyle>
<AlternatingItemStyle CssClass="GridEvenLine"></AlternatingItemStyle>
<ItemStyle CssClass="GridOddLine"></ItemStyle>
<HeaderStyle CssClass="GridHeader"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Charge Description">
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_DESC") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=ddlOtherCharges runat="server" DataSource="<%# GetCharge() %>" DataTextField="CHARGE_DESC" DataValueField="CHARGE_ID">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Rate">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_RATE") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_RATE") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText=" Absorb">
<HeaderStyle Width="4px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblAbsorb" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ABSORB") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton id="rdbAbsorb" runat="server" Text="Absorb" GroupName="rdbCharges"></asp:RadioButton><BR>
<asp:RadioButton id="rdbPassthrough" runat="server" Text="Pass Through" GroupName="rdbCharges"></asp:RadioButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Action">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkEdit" Text="Edit" CommandName="Edit" CausesValidation="True"></asp:LinkButton>
<asp:LinkButton id="lnkBtnDelete" runat="server" CausesValidation="True" CommandName="Delete"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" ID="lnkUpdate" Text="Update" CommandName="Update"></asp:LinkButton>
<asp:LinkButton runat="server" ID="lnkCancel" Text="Cancel" CommandName="Cancel" CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" Position="TopAndBottom" CssClass="GridPager" Mode="NumericPages"></PagerStyle>
</asp:datagrid>

********************************************************************************************
Private Sub dgAddStdFixPricing_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgAddStdFixPricing.ItemCreated
Dim myTableCell As TableCell
myTableCell = e.Item.Cells(1)
Dim myDeleteButton As LinkButton
Response.Write(e.Item.Cells(3).HasControls) 'Returs flase
'myDeleteButton = CType(myTableCell.FindControl("lnkBtnDelete"), LinkButton)
' myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete? All relevant information will be deleted as well.');")
End Sub
Can any one help me, where i am wrong.

Validating controls within GridView

I'm trying to validate controls within a GridView. I can get it working but the control is still causing a postback, which is something I don't want when a validation error occurs - in fact, I want the validation control to prevent a null value from being stored on the db.

Is there a way of disabling postback when a validation error occurs?

Default behavior is to avoid postback by using client-side validation. Unless this behavior is somehow changed either by some setting or due to browser compatibility, the postback should not occur upon validation error. Also check to make sure the command source (button or linkbutton) has the CausesValidation set to true (which I believe is default value). I've verified this behavior using a simple GridView.