PDA

View Full Version : [CLOSED] RowDeselect - Deselected record(row) ID - PLEASE!



caiomarques
Oct 10, 2008, 9:11 AM
Hey, guys!

Im working with some ajax events in my gridpanel.

When user select a row, i insert an record in my database






<SelectionModel>


<ext:CheckboxSelectionModel ID="ctl403" SingleSelect="false">


<AjaxEvents>


<RowSelect EventMask-Msg="Inserindo..." OnEvent="insert" EventMask-ShowMask="true">



To get selected value, im doing this (server side)







public void insert(object sender, AjaxEventArgs e)


{



RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;


foreach (SelectedRow row in sm.SelectedRows)


{

...

String ID = row.RecordID;

...


}



So, im having some problems to work with unselected row.
In my gridpanel when user unselect a row, im having some problems to get the unselected value (record id) to delete it from my database (because when user check it i insert an record inside a database and when user unselect i have to delete it from my database).

Im doing this







</RowSelect>





<RowDeselect EventMask-Msg="Removendo atividade..." OnEvent="remove" EventMask-ShowMask="true">





</RowDeselect>



Server side:






public void remove(object sender, AjaxEventArgs e){






RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;


foreach (SelectedRow row in sm.SelectedRows)


{
...

String ID = row.RecordID; // ITS not returns unselected row id



....


}



There is some way to get unselected value?

Please help-me! If there is no way to do it, im lost!
tnks!

Vladimir
Oct 10, 2008, 9:45 AM
Hi,

The UNSELECTED record id's are not added to the SELECTED rows :)

But it is very easy using UserParams



<SelectionModel>
<ext:CheckboxSelectionModel runat="server">
<AjaxEvents>
<RowDeselect OnEvent="GridPanel1_RowDeselect">
<UserParams>
<%-- the third parameter for RowDeselect event is deselected record--%>
<ext:Parameter Name="DeselectedID" Value="params[2].id" Mode="Raw" />
<%-- the second parameter for RowDeselect event is deselected row index--%>
<ext:Parameter Name="DeselectedRowIndex" Value="params[1]" Mode="Raw" />
</UserParams>
</RowDeselect>
</AjaxEvents>
</ext:CheckboxSelectionModel >
</SelectionModel>

protected void GridPanel1_RowDeselect(object sender, AjaxEventArgs e)
{
string deselectedID = e.UserParams["DeselectedID"];
Label1.Html = deselectedID;
}



Please, let me know if it doesn't work for you

caiomarques
Oct 10, 2008, 11:45 AM
Thanks, Vladimir!
You saved my life again :)

Just another question: sometimes i need to unselect an row selected by user.
How can i unselect this row?
I was thinking to try in this way




.....



RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;

foreach (SelectedRow row in sm.SelectedRows)


{


int index = int.Parse(e.UserParams["DeselectedRowIndex"].ToString());


if (row.RowIndex == index) {
UNSELECT THE ROW

}


}


Can you give-me some idea?

Thank u so much again!

Vladimir
Oct 10, 2008, 1:08 PM
You can change SelectedRows collection of CheckBoxSelectionModel as you want (add/delete) and then call UpdateSelection method of CheckBoxSelectionModel (please note that UpdateSelection need to call only on AjaxEvent). The problem is that after calling UpdateSelection then on client will be called clearSelection which fire deselection event for each selected row (so, you get RowDeselect AjaxEvents, which undesire for you i think). After clear of selection the new selection will be set. For prevent RowDeselect during setting new selection need stop events firing (suspendEvents/resumeEvents)



protected void Button3_Click(object sender, AjaxEventArgs e)
{
RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
sm.AddScript("{0}.suspendEvents();", sm.ClientID);

//for example we need to remove selection for first selected row and set selection for "Boeing Co."
sm.SelectedRows.RemoveAt(0);
sm.SelectedRows.Add(new SelectedRow("Boeing Co."));

sm.UpdateSelection();
sm.AddScript("{0}.resumeEvents();", sm.ClientID);
}


I make quick test and it seems it working good but if you find incorrect behaivor then let me know, please

P.S. You can find useful next examples
http://examples1.ext.net/#/GridPanel/Selection_Models/Checkbox_Selection/
http://examples1.ext.net/#/GridPanel/Selection_Models/Row_Selection/

caiomarques
Oct 15, 2008, 9:22 AM
Hi, Vladmir!
Thanks again.
At first sight it unselect the row, but i think i found an incorrect behavior.

In some cases an error window is showed telling this:

<H2>Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index </H2>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:

Line 71: sm.AddScript("{0}.suspendEvents();", sm.ClientID);
Line 72: int index = int.Parse(e.UserParams["DeselectedRowIndex"].ToString());
Line 73: sm.SelectedRows.RemoveAt(index);
Line 74: panel_informacoes.Html = "<div class='falha'>" + sp.Parameters["@mensagem"].Value.ToString() + "
";
Line 75: sm.UpdateSelection();


What im doing?

In aspx page:





<RowSelect EventMask-Msg="Inserindo atividade..." OnEvent="insere" EventMask-ShowMask="true">
<UserParams>
<ext:Parameter Name="atividade_selecionada" Value="params[2].id" Mode="Raw" />
<ext:Parameter Name="DeselectedRowIndex" Value="params[1]" Mode="Raw" />
</UserParams>
</RowSelect>



Server side:





public void insere(object sender, AjaxEventArgs e){

.....
//if I can isert


if (retorno=="0")


{


panel_informacoes.Html = "<div class='sucesso'>"+sp.Parameters["@mensagem"].Value.ToString()+"
";


}



// if i cant insert, i must unselect row...
else {


RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;


sm.AddScript("{0}.suspendEvents();", sm.ClientID);
int index = int.Parse(e.UserParams["DeselectedRowIndex"].ToString());
sm.SelectedRows.RemoveAt(index);
panel_informacoes.Html = "<div class='falha'>" + sp.Parameters["@mensagem"].Value.ToString() + "
";
sm.UpdateSelection();
sm.AddScript("{0}.resumeEvents();", sm.ClientID);


You can see it at: http://www.fsba.edu.br/semanaacademica/inscricao/ext.aspx

Any idea?

Vladimir
Oct 15, 2008, 10:10 AM
Hi,

You can't apply DeselectedRowIndex to SelectedRows collection. It is different collection with different count of rows. You can use ID from UserParams (in your example it is "atividade_selecionada", i suppose) and do search on SelectedRows on server-side comparing ID and RecordID from selected row

caiomarques
Oct 15, 2008, 10:30 AM
Hi, Vladimir

I'll try, but tell me...



<ext:Parameter Name="DeselectedRowIndex" Value="<U>params[1]"</U> Mode="Raw" />



Does not <U>params[1]</U> represents the row index?

Vladimir
Oct 15, 2008, 10:45 AM
Yes, params[1] is row index. But you confuse index from grid and index of SelectedRows collection.

For example, your grid has 10 rows and two rows selected (for example, with indexes 5 and 6). On server-side the SelectedRows collection will be contains only two rows (which selected on client). In this case the valid indexes for SelectedRows are 0 and 1. But you try apply indexes 5 and 6 (sm.SelectedRows.RemoveAt(index);)

You must loop through SelectedRows collection and find need row



for(int i=0; i<sm.SelectedRows.Count;i++)
{
if(sm.SelectedRows[i].RowIndex == deselectedRowIndex)
{
sm.SelectedRows.RemoveAt(i);
}

//or

if(sm.SelectedRows[i].RecordID == deselectedRecordID)

{

sm.SelectedRows.RemoveAt(i);

}
}

caiomarques
Oct 15, 2008, 10:09 PM
Hi, Vladimir!
Now I understood!
It works fine.

Thanks one more time for explaning.



Im having some unexpected behavior with suspendEvents and resumeEvents in other case... but I will soon post it in other entry.

By now, im suspeding the use of Coolite for this project. I think I need lern more about it.
While I learn more, i will pratice and test the framework.

Let me know if you need some suggestions or any help.

Thanks again!