This is a migrated thread and some comments may be shown as answers.

get value of CommandItemTemplete item via code

6 Answers 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 31 Oct 2011, 03:22 PM
I have two checkboxes that I've added in a CommandItemTemplete, how can I get the check status  in the RadGrid1_NeedDataSource ?
Thanks

 <CommandItemTemplate>
                                 <table width="100%">
                             <tr class="customFont">
                         
                            <td align="left">
                             <asp:LinkButton ID="LinkButton2" runat="server" OnClientClick="openWin('/portals/0/NewSearch.aspx'); return false;"  >
                                <img style="border:0px;vertical-align:middle;" alt="" src="/portals/0/Images/add-icon.png" />New Search</asp:LinkButton>                              
                               &nbsp;&nbsp;&nbsp; 
                                <td align="left">
                                    <asp:CheckBox ID="cbAffiliation" Text="Show Affiliation"  OnCheckedChanged="CheckBox_CheckedChanged"  runat="server" AutoPostBack="True" />
                                     &nbsp;  &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                                        <asp:CheckBox ID="cbAbstract"  Text="Show Abstract"   OnCheckedChanged="CheckBox_CheckedChanged"   runat="server" AutoPostBack="True" />
                                                                    
                                </td>
                              
                         
                            <td align="right">
                                <asp:LinkButton ID="LinkButton4" runat="server" OnClientClick="refreshGrid(); return false;" >
                                <img style="border:0px;vertical-align:middle;" alt="" src="/portals/0/Images/Refresh.png" /> Refresh</asp:LinkButton>
                                &nbsp;&nbsp;&nbsp;
                            </td>
                        </tr>
         
             </table>
                </CommandItemTemplate>



6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 01 Nov 2011, 04:43 AM
Hello William,

 NeedDataSource event is mainly used to bind the RadGrid. You can try the following in ItemDataBound event.

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
  if (e.Item is GridCommandItem)
 {
   GridItem cmdItem = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
   CheckBox chk1 = cmdItem.FindControl("cbAffiliation") as CheckBox;
   string s = chk1.Text;
   bool val = chk1.Checked;
 }
}

Thanks,
Princy.
0
William
Top achievements
Rank 1
answered on 01 Nov 2011, 04:51 AM
Thanks for the reply but I need to read the value of the check boxes in the needdatasource. If the first checkbox is checked I need to pass one paramenter if the other is check I need to pass a different paramenter to the database.  I hope that this makes sense, sorry that I was not clear.

Thanks

0
Mira
Telerik team
answered on 02 Nov 2011, 12:23 PM
Hello William,

You can use the GetItems method of the grid table view in order to find the command item.
Please examine the Using the GetItems(itemType), GetColumn(columnName) and GetColumnSafe(columnName) methods help topic for additional information.

I hope this helps.

Best wishes,
Mira
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
William
Top achievements
Rank 1
answered on 02 Nov 2011, 08:03 PM
Sorry I still have a problem.  If I put the following code  
 
Dim cmdItem As GridCommandItem = DirectCast(RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)
Dim cbAffiliation As CheckBox = DirectCast(cmdItem.FindControl("cbAffiliation"), CheckBox)
Dim cbAbstract As CheckBox = DirectCast(cmdItem.FindControl("cbAffiliation"), CheckBox)
If cbAbstract.Checked = True Then
'get data 1
Else
'get something else
End If

 

 

In Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender  it runs fine.

But if I put it in  RadGrid1_NeedDataSource then it throw an error

 

A critical error has occurred. Index was outside the bounds of the array.

I need the value of the check boxes to pass as a parameter in my RadGrid1_NeedDataSource query.  Any help is appreciated.




0
William
Top achievements
Rank 1
answered on 04 Nov 2011, 09:50 PM
Any help on this item is still appreciated.

Thanks

0
Jayesh Goyani
Top achievements
Rank 2
answered on 05 Nov 2011, 07:26 AM
Hello,

please look in Event sequence link.

by Attached image or in above link, You can clearly see that needdataource event is fire first then after it fire itemcreated event.
so, it will create your grid's structure as per your assigned datasource.
so, at the time of needdatasource, Grid is not exists in your page so it is not possible to get CommandTemplate.

 Now, How to achieve your requirement,

Create one property
public int datastate
    {
        get
        {
            if (ViewState["datastate"] != null)
            {
                return Convert.ToInt32(ViewState["datastate"]);
            }
            else
            {
                return 0;
            }
        }
        set
        {
            ViewState["datastate"] = value;
        }
    }


Prerender event()
{
Dim cmdItem As GridCommandItem = DirectCast(RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)
Dim cbAffiliation As CheckBox = DirectCast(cmdItem.FindControl("cbAffiliation"), CheckBox)
Dim cbAbstract As CheckBox = DirectCast(cmdItem.FindControl("cbAffiliation"), CheckBox)
If cbAbstract.Checked = True Then
'get data 1
datastate = 1
Else
'get something else
datastate = 2
End If

 
RadGrid1.Rebind()
 
}


void RadGrid3_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
          // assign Datasource as per your requirment
 
        if (datastate == 1)
        {
            // when check box checked
            //RadGrid1.DataSource = null;
        }
        else if (datastate == 2)
        {
            // when check box unchecked
            //RadGrid1.DataSource = null;
        }
        else
        {
            // fire on page load
            //RadGrid1.DataSource = null;
        }
 
    }

Note : (my suggestion is that use below method to achieve your requirement ) another solution is when check / uncheck the checkbox.
store their state in hiddenfield.
and in the  need datasource event you can get this hidden field's value.
and you can achieve your requirement.



let me know if any concern.


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
William
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
William
Top achievements
Rank 1
Mira
Telerik team
Jayesh Goyani
Top achievements
Rank 2
Share this question
or