advanced question: making <asp:ButtonColumn> CausesValidation=False

Z

z. f.

Hi,

i stated that this is an advanced question because i have a post from few
days ago that i received answers to with suggestions that looked good but
did not work, so please if you post a reply make sure your suggestion
actually works.

like a <asp:button> control have a CausesValidation property that allows it
to POST a button command event to the server without causing the page to
make validation on form inputs, i need to get the same functionallity to an
<asp:button> culomn with a delete command.

i tried to use the OnItemCreated event of the datagrid, to add this
property, but perhaps becuase the button inside the grid is actually an
array of buttons, setting the property had no effect.

also tried to make an <TemplateItem> with the button inside, but again
setting the property had no effect and the form did passed the validation
check.

please advise, but don't just throw "why don't you try this", and as it did
last time, it did not work.

i'm gessing there must be a way (even use javascript or something) - to make
a command from the grid row without passing the form validation.


TIA, z.
 
S

Scott Allen

Hi z.f:

Perhaps you could post some code to look at. I've tried this with an
ItemTemplate like so:

<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="au_id" HeaderText="au_id"/>
<asp:TemplateColumn HeaderText="test">
<ItemTemplate>
<asp:Button Runat="server" ID="button1"
CausesValidation="False" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:TextBox Runat="server" ID="textbox1" />
<asp:RequiredFieldValidator Runat="server"
ID="requiredFieldValidator"
ControlToValidate="textbox1"
ErrorMessage="You screwed up" />
<asp:Button Runat="server" ID="mainbutton" />
</form>

In this case pressing a button in the DataGrid does not cause
validation.
 
Z

z. f.

i'll check it up ,

for now i managed to do with overriding the Validate method of the page
object and only if i want to, i call
MyBase.Validate()

but i will try your code as soon as i'll make another grid with delete
buttons etc', that should not make validation of other operation that the
page can do.



question: the button inside the <asp:TemplateColumn HeaderText="test">, how
it "knows" which id it acts for, do i need to write code in the
OnItemCreated , or it can handle a grid command like delete?



thanx.
 
S

Scott Allen

In this case it would be a little different than the grid delete
command. You can get to the DataGridItem by asking for the sender's
parent, like this:

protected void Button1_Click(object sender, System.EventArgs e)
{
Button btn = sender as Button;
// now btn.Parent is equivalent to
// e.Item in DataGrid_Delete command
}

You might also find this article that I wrote helpful:
Tips for locating controls with the FindControl method, including
finding controls in DataGrid rows, Repeaters, DataGrid headers, and
user controls:
http://odetocode.com/Articles/116.aspx
 
P

Peter Blum

The correct answer to the original question:

In OnItemCreated, retrieve the ButtonColumn object by its index in
e.Item.Cells. Then access the first control in its control collection. That
is the Button control. Finally set CausesValidation to false on that
control.

Suppose the buttonColumn was the second column. It uses an index of 1.
Here's code in C#:

Button vButton = (Button) e.Item.Cells[1].Controls[0];
vButton.CausesValidation = false;

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top