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

Server-side Row Selection

4 Answers 962 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 08 Jul 2009, 04:44 PM
Hi,

I am trying to duplicate an example from the Grid Demos here but with a few minor differences.  I would like to achieve this serverside if possible.

Instead of counting the number of rows selected (in the example), I want the values of each column within each row selected to be displayed in a separate control (listbox/label/something that can handle multiple selections and values)on the same page.

Example: A user clicks the row(s) they are interested in and the values of every column within the selected row(s) get listed in another control for the user to see.  Basically a shopping cart without using another grid.  These values will be used to send out as fields within an email.

There are LOTS of  great examples I found by Shinu, Princy, and others (a good client-side example for 1 row is here) that demonstrate this functionality with 1 selected row, but I just can't (and don't really know how) seem to paste together multiple row selections and getting their values to another control.  Many of the examples for 1 row at a time look similar to this:
protected void Button3_Click(object sender, EventArgs e)  
{  
    foreach (GridDataItem item in RadGrid1.SelectedItems)  
    {  
        TextBox1.Text = item["ColumnUniqueName1"].Text.ToString();  
        TextBox2.Text = item["ColumnUniqueName2"].Text.ToString();  
        TextBox3.Text = item["ColumnUniqueName3"].Text.ToString();  
        . . .           
    }  
}  
Maybe server-side is a bad way to go?  Is client-side 'better'? Ajax?  Any ideas? Thanks. Joe

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 Jul 2009, 08:31 AM
Hello Joe,

You can try out the following code to achieve the required scenario:
aspx:
 <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1"  AutoGenerateColumns="true" AllowMultiRowSelection="true" OnPreRender="RadGrid1_PreRender" OnItemCommand="RadGrid1_ItemCommand" > 
        <ClientSettings EnablePostBackOnRowClick="true"
        <Selecting AllowRowSelect="true" /> 
        </ClientSettings> 
 </telerik:RadGrid> 
         
 <asp:ListBox ID="ListBox1" runat="server">         
 </asp:ListBox> 

c#:
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "RowClick"
        { 
            string strTxt = string.Empty; 
            if(e.Item is GridDataItem && e.Item.Selected) 
            { 
                GridDataItem dataItem = (GridDataItem)e.Item; 
                foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
                { 
                    if (col.UniqueName != "ExpandColumn" && col.UniqueName != "RowIndicator"
                    { 
                        strTxt += (dataItem[col.UniqueName].Text) + ","
                    } 
                } 
                ListItem item = new ListItem(); 
                item.Text = strTxt.Substring(0, strTxt.Length - 1); 
                ListBox1.Items.Add(item); 
            } 
        } 
    } 

Thanks
Princy.
0
Joe
Top achievements
Rank 1
answered on 09 Jul 2009, 02:08 PM
Princy,

You are a life saver!  Now I can repair the wall I have been banging my head against.

One other question.  I added some checkboxes to the column for the user to click. 
1.  Is there a way to omit the check column in the cs code as it displays in the listbox as a empty space (&nbsp;).
2.  How would I go about "deselecting or unchecking" a row and have the listbox remove the item?

Thanks again Princy.  You are a great source to this forum.

Joe
0
Princy
Top achievements
Rank 2
answered on 10 Jul 2009, 08:01 AM
Hi Joe,

Check out the following example which implements the required scenario:
c#:
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)  
    {          
          if (e.CommandName == "RowClick")  
          {                  
                ListBox1.Items.Clear();  
                foreach (GridDataItem dataItem in RadGrid1.SelectedItems)                 
                {  
                    string strTxt = string.Empty;  
                    foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns)  
                    {  
                        if (col.UniqueName != "ExpandColumn" && col.UniqueName != "RowIndicator" && col.UniqueName != "CheckBoxColumnUniqueName"// to exclude the check column value  
                        {  
                            strTxt += (dataItem[col.UniqueName].Text) + ",";  
                        }  
                    }  
                    ListItem item = new ListItem();  
                    item.Text = strTxt.Substring(0, strTxt.Length - 1);  
                    ListBox1.Items.Add(item);                      
                }                 
            }        
    }  

Hope this helps...
Regards
Princy.
0
Joe
Top achievements
Rank 1
answered on 10 Jul 2009, 03:00 PM
Thanks Princy,

I am still working on what you sent.  I have everything else working almost perfectly. I also figured out the space thing.

Couple of questions.

1. In your previous post you reference an event called OnPreRender="RadGrid1_PreRender".
However, there was no code or routine related to this event.  The functionality still works.  Not sure.

2. Everything works great except when I try to use an Ajax manager.  I can't get things to work properly with AJAX manager.  I have to disable it completely.  Things do work then but  there are lots of postbacks that I would like to avoid.  Any ideas?

3.  With regards to the uncheck code you provided, you reference 2 fields "EmployeeID".  I understand these are example fields, but I can't get my fields to work properly.  A little clarification is needed on my setup perhaps.

I have a drop down box configured to a datasource that queries the Name and ID of the sf_lst_NamedList table from Sitefinity's List Module.
My Grid queries the Headline and Content fields FROM sf_lst_ListItem WHERE the ParentID = the @ID of the sf_lst_NamedList table.
The only fields I really have in my grid are Headline and Content.  Either of those do not work with uncheck code provided.  Here is some code if you are still feeling helpful.

DataSources:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Sitefinity %>" 
    SelectCommand="SELECT Name, ID FROM [sf_lst_NamedList]"></asp:SqlDataSource> 
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Sitefinity %>" 
    SelectCommand="SELECT Headline, [Content] FROM sf_lst_ListItem WHERE (CAST(ParentID AS varchar(50)) = CAST(@ID AS varchar(50)))"
ComboBox:
<telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="SqlDataSource1" 
                        DataTextField="Name" DataValueField="ID" Skin="Black" EnableScreenBoundaryDetection="False" 
                        EmptyMessage="Choose Product" AppendDataBoundItems="True" AutoPostBack="True" 
                        OpenDropDownOnLoad="true"
                        <Items> 
                            <telerik:RadComboBoxItem Text="Select One" Value="0" Selected="true" /> 
                        </Items> 
                    </telerik:RadComboBox> 
Grid
<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Black" DataSourceID="SqlDataSource2" 
                        AllowMultiRowSelection="True" OnItemCommand="RadGrid1_ItemCommand" 
                        AutoGenerateColumns="False" AllowPaging="True" GridLines="None" PageSize="5"
                        <MasterTableView> 
                            <Columns> 
                                <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn"  /> 
                                 
                                <telerik:GridBoundColumn DataField="Headline" HeaderText="Product Feature" UniqueName="Headline"
                                    <HeaderStyle Width="100px" Font-Size="Large" /> 
                                    <ItemStyle Font-Size="Large" /> 
                                </telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn DataField="Content" HeaderText="Concern or Need" UniqueName="Content"
                                    <HeaderStyle Font-Size="Large" /> 
                                    <ItemStyle Font-Size="Large" /> 
                                </telerik:GridBoundColumn> 
                            </Columns> 
                        </MasterTableView> 
                        <ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="true"
                            <Selecting AllowRowSelect="True" /> 
                        </ClientSettings> 
                    </telerik:RadGrid> 
Grid.ascx.cs
using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Configuration; 
using System.Data; 
using System.Data.OleDb; 
using System.Drawing; 
using System.Web; 
using System.Web.SessionState; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using System.Web.Security; 
using System.Web.UI.WebControls.WebParts; 
using System.Net.Mail; 
using Telerik.Web.UI; 
 
public partial class MyControls_ComboBox : System.Web.UI.UserControl 
 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "RowClick") 
        { 
            string strTxt = string.Empty; 
            if (e.Item is GridDataItem && e.Item.Selected) 
            { 
                GridDataItem dataItem = (GridDataItem)e.Item; 
                foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
                { 
                    if (col.UniqueName != "ExpandColumn" && col.UniqueName != "RowIndicator" && col.UniqueName != "ClientSelectColumn") 
                    { 
                        strTxt += (dataItem[col.UniqueName].Text) + ","; 
                    } 
                } 
                ListItem item = new ListItem(); 
                item.Text = strTxt.Substring(0, strTxt.Length - 1); 
                ListBox1.Items.Add(item); 
            } 
        } 
    }  
 
    protected void Confirm_Send(object sender, System.EventArgs e) 
    { 
        var listboxvalues = ""
        foreach (ListItem i in ListBox1.Items) 
        { 
            listboxvalues += i; 
        } 
         
        //create the mail message 
        MailMessage mail = new MailMessage(); 
 
        //set the addresses 
        mail.From = new MailAddress("x"); 
        mail.To.Add("x"); 
        mail.CC.Add("x"); 
        mail.CC.Add("x"); 
        mail.CC.Add("x"); 
        //set the content 
        mail.Subject = "Website Lead"
        mail.Body = "First Name: " + RadTextBox1.Text + "\r\n";  
        mail.Body += "Last Name: " + RadTextBox2.Text + "\r\n";  
        mail.Body += "Email: " + RadTextBox3.Text + "\r\n";  
        mail.Body += "Phone: " + RadTextBox4.Text + "\r\n"; 
        mail.Body += "Best Time to Contact: " + RadTextBox5.Text + "\r\n"; 
        mail.Body += "Comments: " + RadTextBox6.Text + "\r\n"; 
        mail.Body += "Concerns and Needs: " + listboxvalues +"\r\n"; 
        
 
        //send the message 
        SmtpClient smtp = new SmtpClient("x"); 
        smtp.Credentials = new System.Net.NetworkCredential("x", "x"); 
        smtp.Send(mail); 
 
 
 
    } 
Note I have a email routine included.  I wouldn't assume this would cause any problems.

I hope this gives you an idea of what I am doing.  Your examples have been VERY helpful and I am really starting to get this.  Thanks Princy.

joe
Tags
Grid
Asked by
Joe
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Joe
Top achievements
Rank 1
Share this question
or