Problem in ASP.NET

  • Thread starter Thread starter Mortel
  • Start date Start date
M

Mortel

Hi,
I have a DataGrid on my page. I show CheckBox in every row to select my rows
and show one CheckBox in Title of that column to select all. I can use
function and parameter id to read data and set my CheckBox for DataSet, for
any Arrays, nevermind. But I have problem with get data from CheckBox when
user checked/unchecked it. How I can add my event to CheckBoxes or How I can
call any method when user change value.

Thanks for all.
Boniek
 
You have a couple of options here. When you add your checkbox to the
datagrid as a control, you may need to manually wire it to an event handler.
checkbox1.click += some handler. This gets called when the checkbox is
clicked. However, this depends on how you have added the control to the grid
and the type of datagrid (autogenerated or not) it is as the method used to
retrieve the check selection will vary depending on this implementation.

At most, data retrieval will either be thru code behind in which case you
just subscribe to the event as I've pointed out or it will be available thru
scripting client-side, in which case it is still available to the code
behind thru the request.form object

Consider one possible implementation (radiobutton example):
The control has been added dynamically to a template column containing label
controls.
Label lbl = (Label)e.Item.FindControl("mycolumn");

if(lbl != null)

lbl.Text = "<input type=radio name='samegroup' value=myselection>";

The selected value is retrieved thru

string selected = Request.Form["samegroup"];

the expected value in the string selected is "myselection"

Modify the code as you see fit.
 
The easiest way to do this is

in the .aspx file where you have defined the checkboxes, add an
OnClick="someHandler", then in that handler react to the event

<asp:DataGrid...
<Columns>
<TemplateColumn>
<ItemTemplate>
<asp:CheckBox runat=server id="chkBox" OnClick="SomeHandler"
Text="CheckBox"/>
</ItemTemplate>
</TemplateColumn>
...


Then in the codebehind or in the aspx file in a server tag (<%... %>) do
this:

protected void SomeHandler(Object sender, System.EventArgs e)
{
CheckBox cb = sender as CheckBox //the checkbox that fired the event
DataGridItem gridItem = cb.Parent.Parent as DataGridItem // now you have
access to the datagriditem, you can get other columns controls, etc

//Do something interesting here
...
}
 
Back
Top