GridView Questions

G

Guest

I've got two questions on how to do things in the new GridView. I'm used to
the DataGrid in ASP 1.1, so I need the equavalent in ASP 2.0 Gridview.

1.) What is the equavalent for Item Command event in GridView? I thought it
would be RowCommand, but I can't get that event to fire with a check box
field that I drag/drop in a template column in the ItemTemplate.

2.) For a Templated column, like a check box above, where do I tell it the
data field to bind from the data source? There is not a Template DataField
property to set to the database field name as in a bound field.
 
T

Teemu Keiski

Hi,

first of all, DataGrid still exists in ASP.NET 2.0, so you can use it also.
But the replies:

1) RowCommand is the equivalent. CheckBox doesn't raise ItemCommand event
since it doesn't provide CommandName and CommandArgument
attributes/properties to apply event bubbling which raising
ItemCommand/RowCommand basically is (it doesn't raise it in ASP.NET v1.1
either). By default only Button, ImageButton or ImageButton (=IButtonControl
interface) or the built-in command columns/fields also raise correspondiong
event in GridView. Can you show an exa,ple what you want to do,.or how you
have done it in v1.1?

2) You would use a databinding expression as in v1.1. For example
<%#Eval("FieldName") %> within <ItemTemplate>...</ItemTemplate>. Tenplate
field is templated and setting the layout etc is in developer's hands so
having datafield for it wouldn't make sense.
 
G

Guest

Hello Chris,
The equavalent of ItemCommand in GridView is RowCommand.
What I understood from your question part (1) is you added a checkbox to a
templated field in your GridView, and you set AutoPostBack to true and
implemented RowCommand event, and when executing the event is not firing?!
Well this will not work also in DataGrid. So you can impelement selectChange
Event of your CheckBox Instead. Hope I hit your idea in this.

About the second part,
You want to bind a DataSource Field to your CheckBox located in your
GridView Templated Field, Well, Select your CheckBox and note the smart tag
icon in the top right. click on it and you will get Edit DataBindings link.
click on too and start do what you want.

Regards,
--
Muhammad Mosa
Software Engineer & Solution Developer
MCT/MCSD.NET
MCTS: .Net 2.0 Web Applications
MCTS: .Net 2.0 Windows Applications
 
G

Guest

Thanks for the ideas. They may help, but here is more of what I need.

For #1:
If I use the selectChange event to check the check box, how do I tell which
row the check box was checked (multiple rows in the gridview)? Also, how do I
get say another field ie a unique Index to be used for DB calls etc.?

For #2:
I found the smart tag(Edit Data bIndings), but it is not enabled. Binding
for Checked fields below it are all disabled. I see the fields, but they are
disabled. Only the custom binding is enabled. How do I enable the bindings?
 
G

Guest

For #1:
a)Implement RowDataBound of your GridView as the following:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView row = (DataRowView)e.Row.DataItem;
CheckBox chk = (CheckBox)e.Row.FindControl("CheckBox1");
chk.Attributes["CustomValue"] = row["ProductID"].ToString();
}
}
Note that I have added CustomValue attribute to everu check box with datakey
value stored in it.
b)Now Implement selectChanged event of your checkbox but don't forget to set
autopostback to true
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
string s = ((CheckBox)sender).Attributes["CustomValue"];
}

For #2
in the custom binding area, add Eval("YourDataFieldName") or
Bind("YourDataFieldName")

Hope this would help
--
Muhammad Mosa
Software Engineer & Solution Developer
MCT/MCSD.NET
MCTS: .Net 2.0 Web Applications
MCTS: .Net 2.0 Windows Applications
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top