Click here to Skip to main content
15,915,501 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblMsg.Text = "";
FillGrid();
FillGridViewDropdown();

}
}

protected void gvTrainStatus_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
SaveCheckedValues();
gvTrainStatus.PageIndex = e.NewPageIndex;
this.FillGrid();
PopulateCheckedValues();
}

protected void btnSendDataToDisplay_Click(object sender, EventArgs e)
{


string str = string.Empty;
string strname = string.Empty;
foreach (GridViewRow gvrow in gvTrainStatus.Rows)
{
PopulateCheckedValues();
SaveCheckedValues();

//if (Session["CHECKED_ITEMS"] != null)

{
CheckBox chk = (CheckBox)gvrow.FindControl("ChkSelect");
if (chk != null & chk.Checked)
{

str += (gvrow.Cells[1].FindControl("lblTrainNo") as Label).Text;// gvrow.Cells[1].Text + ", ";
str += (gvrow.Cells[2].FindControl("lblTrainName") as Label).Text;// gvrow.Cells[2].Text + ", ";
str += (gvrow.Cells[3].FindControl("lblScheduleArrival") as Label).Text;// "Company :- " + gvrow.Cells[3].Text + ", ";
str += (gvrow.Cells[4].FindControl("lblScheduleDeparture") as Label).Text;// "Addess :- " + gvrow.Cells[4].Text;
str += (gvrow.Cells[5].FindControl("lblAD") as Label).Text;
str += (gvrow.Cells[6].FindControl("lblEAT") as Label).Text;
str += (gvrow.Cells[7].FindControl("lblEDT") as Label).Text;
str += (gvrow.Cells[8].FindControl("lblDefaultPlatform") as Label).Text;
//str += "
";
}
}
}

var builder = new StringBuilder(str.Length * 5);
foreach (char chr in str)
{
if (builder.Length > 0)
//builder.Append(' '); With Space
builder.Append(""); //without space

builder.AppendFormat("U+{0:X4}", (ushort)chr);
}
string text = builder.ToString();

lblPF.BackColor = System.Drawing.Color.Black;
lblPF.ForeColor = System.Drawing.Color.Aqua;
lblPF.Text = "Selected Train Status: " + text;

}


//This method is used to populate the saved checkbox values
private void PopulateCheckedValues()
{
ArrayList userdetails = (ArrayList)Session["CHECKED_ITEMS"];
if (userdetails != null && userdetails.Count > 0)
{
foreach (GridViewRow gvrow in gvTrainStatus.Rows)
{
int index = (int)gvTrainStatus.DataKeys[gvrow.RowIndex].Value;
if (userdetails.Contains(index))
{
CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkSelect");
myCheckBox.Checked = true;
}
}
}
}
//This method is used to save the checkedstate of values
private void SaveCheckedValues()
{
ArrayList userdetails = new ArrayList();
int index = -1;
foreach (GridViewRow gvrow in gvTrainStatus.Rows)
{
index = (int)gvTrainStatus.DataKeys[gvrow.RowIndex].Value;
bool result = ((CheckBox)gvrow.FindControl("chkSelect")).Checked;
// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
userdetails = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!userdetails.Contains(index))
userdetails.Add(index);
}
else
userdetails.Remove(index);
}
if (userdetails != null && userdetails.Count > 0)
Session["CHECKED_ITEMS"] = userdetails;
}

What I have tried:

<asp:GridView ID="gvTrainStatus" runat="server" OnRowEditing="gvTrainStatus_RowEditing" OnRowCancelingEdit="gvTrainStatus_RowCancelingEdit" OnRowUpdating="gvTrainStatus_RowUpdating" AutoPostback="false" AutoGenerateColumns="False" DataKeyNames="TID" OnRowCommand="gvTrainStatus_RowCommand" OnRowDataBound="gvTrainStatus_RowDataBound1" OnRowDeleting="gvTrainStatus_RowDeleting1" Class="table table-striped table-bordered table-hover" Width="100%" PageIndex="10" OnPageIndexChanging="gvTrainStatus_PageIndexChanging" AllowPaging="True" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
<columns>
<asp:templatefield>
<headertemplate>
<asp:CheckBox ID="ChkSelectData" runat="server" onclick="CheckAll(this);" />

<itemtemplate>
<asp:CheckBox ID="ChkSelect" runat="server" />


<asp:TemplateField HeaderText="Train No">
<edititemtemplate>
<asp:TextBox ID="txtTrainNo" runat="server" Text='<%# Bind("TrainNo") %>' CssClass="form-control" Width="80px">

<itemtemplate>
<asp:Label ID="lblTrainNo" runat="server" Text='<%# Bind("TrainNo") %>'>

<ControlStyle Font-Bold="True" ForeColor="BlueViolet" />
<HeaderStyle ForeColor="#ffff00" />

<asp:TemplateField HeaderText="Train Name">
<edititemtemplate>
<asp:TextBox ID="txtTrainName" runat="server" Text='<%# Bind("TrainName") %>' CssClass="form-control">

<itemtemplate>
<asp:Label ID="lblTrainName" runat="server" Text='<%# Bind("TrainName") %>'>

<ControlStyle Font-Bold="True" ForeColor="Turquoise" />
<HeaderStyle ForeColor="#ffff00" />

<asp:TemplateField HeaderText="Schedule Arrival">
<edititemtemplate>
<asp:TextBox ID="txtScheduleArrival" runat="server" Text='<%# Bind("ScheduleArrival") %>' CssClass="form-control" Width="60px">

<itemtemplate>
<asp:Label ID="lblScheduleArrival" runat="server" Text='<%# Bind("ScheduleArrival") %>' Width="60px">

<ControlStyle Font-Bold="True" ForeColor="Tomato" />
<HeaderStyle ForeColor="#ffff00" />

<asp:TemplateField HeaderText="Schedule Departure">
<edititemtemplate>
<asp:TextBox ID="txtScheduleDeparture" runat="server" Text='<%# Bind("ScheduleDeparture") %>' CssClass="form-control" Width="60px">

<itemtemplate>
<asp:Label ID="lblScheduleDeparture" runat="server" Text='<%# Bind("ScheduleDeparture") %>' Width="60px">

<ControlStyle Font-Bold="True" ForeColor="Teal" />
<HeaderStyle ForeColor="#ffff00" />

<asp:TemplateField HeaderText="AD">
<edititemtemplate>
<asp:TextBox ID="txtAD" runat="server" Text='<%# Bind("AD") %>' CssClass="form-control" Width="40px">

<itemtemplate>
<asp:Label ID="lblAD" runat="server" CssClass="btn btn-primary btn-sm" Text='<%# Bind("AD") %>'>

<ControlStyle Font-Bold="True" />
<HeaderStyle ForeColor="#ffff00" />

<asp:TemplateField HeaderText="Train Status">
<itemtemplate>
<asp:DropDownList ID="ddlTrainStatus" runat="server" CssClass="btn btn-success dropdown-toggle" Width="90px">


<HeaderStyle ForeColor="#ffff00" />

<asp:TemplateField HeaderText="LATE">
<ItemStyle />
<itemtemplate>
<asp:DropDownList ID="ddlLATE" runat="server" Width="60px" CssClass="btn btn-warning dropdown-toggle" OnSelectedIndexChanged="ddlLATE_SelectedIndexChanged">
<asp:listitem>0
<asp:listitem>1
<asp:listitem>2
<asp:listitem>3
<asp:listitem>4
<asp:listitem>5
<asp:listitem>6
<asp:listitem>7
<asp:listitem>8
<asp:listitem>9
<asp:listitem>10
<asp:listitem>11
<asp:listitem>12
<asp:listitem>13
<asp:listitem>14
<asp:listitem>15
<asp:listitem>16
<asp:listitem>17
<asp:listitem>18
<asp:listitem>19
<asp:listitem>20
<asp:listitem>21
<asp:listitem>22
<asp:listitem>23
<asp:listitem>24
:
<asp:DropDownList ID="ddlLATE2" runat="server" Width="60px" CssClass="btn btn-info dropdown-toggle">
<asp:listitem>0
<asp:listitem>1
<asp:listitem>2
<asp:listitem>3
<asp:listitem>4
<asp:listitem>5
<asp:listitem>6
<asp:listitem>7
<asp:listitem>8
<asp:listitem>9
<asp:listitem>10
<asp:listitem>11
<asp:listitem>12
<asp:listitem>13
<asp:listitem>14
<asp:listitem>15
<asp:listitem>16
<asp:listitem>17
<asp:listitem>18
<asp:listitem>19
<asp:listitem>20
<asp:listitem>21
<asp:listitem>22
<asp:listitem>23
<asp:listitem>24
<asp:listitem>25
<asp:listitem>26
<asp:listitem>27
<asp:listitem>28
<asp:listitem>29
<asp:listitem>30
<asp:listitem>31
<asp:listitem>32
<asp:listitem>33
<asp:listitem>34
<asp:listitem>35
<asp:listitem>36
<asp:listitem>37
<asp:listitem>38
<asp:listitem>39
<asp:listitem>40
<asp:listitem>41
<asp:listitem>42
<asp:listitem>43
<asp:listitem>44
<asp:listitem>45
<asp:listitem>46
<asp:listitem>47
<asp:listitem>48
<asp:listitem>49
<asp:listitem>50
<asp:listitem>51
<asp:listitem>52
<asp:listitem>53
<asp:listitem>54
<asp:listitem>55
<asp:listitem>56
<asp:listitem>57
<asp:listitem>58
<asp:listitem>59
<asp:listitem>60


<HeaderStyle ForeColor="#ffff00" />

<asp:TemplateField HeaderText="EAT ">
<edititemtemplate>
<asp:TextBox ID="txtEAT" runat="server" Text='<%# Bind("EAT") %>' CssClass="form-control" Width="90px">

<itemtemplate>
<asp:Label ID="lblEAT" runat="server" Text='<%# Bind("EAT") %>'>

<ControlStyle Font-Bold="True" ForeColor="SaddleBrown" />
<HeaderStyle ForeColor="#ffff00" />

<asp:TemplateField HeaderText="EDT ">
<edititemtemplate>
<asp:TextBox ID="txtEDT" runat="server" Text='<%# Bind("EDT") %>' CssClass="form-control" Width="80px">

<itemtemplate>
<asp:Label ID="lblEDT" runat="server" Text='<%# Bind("EDT") %>'>

<ControlStyle Font-Bold="True" ForeColor="SandyBrown" />
<HeaderStyle ForeColor="#ffff00" />

<asp:TemplateField HeaderText="Default Platform">
<itemtemplate>
<asp:DropDownList ID="ddlDefaultPlatform" runat="server" CssClass="btn btn-danger dropdown-toggle">
<asp:listitem>
<asp:listitem>1
<asp:listitem>2
<asp:listitem>3
<asp:listitem>4
<asp:listitem>5
<asp:listitem>6
<asp:listitem>7
<asp:listitem>8
<asp:listitem>9
<asp:listitem>10
<asp:listitem>11
<asp:listitem>12
<asp:listitem>13
<asp:listitem>14
<asp:listitem>15
<asp:listitem>16
<asp:listitem>17
<asp:listitem>18
<asp:listitem>19
<asp:listitem>20
<asp:listitem>21
<asp:listitem>22
<asp:listitem>23
<asp:listitem>24

<asp:Label ID="lblDefaultPlatform" runat="server" Text='<%# Bind("DefaultPlatform") %>'>

<HeaderStyle ForeColor="#ffff00" />



<%-- <asp:TemplateField HeaderText="Hide" >
<itemtemplate>
<asp:LinkButton ID="lnkHide" runat="server" Text="Hide" OnClick="lnkHide_Click" />


<asp:BoundField DataField="Name" HeaderText="Name" />--%>


<asp:TemplateField HeaderText="COD">
<edititemtemplate>
<asp:CheckBox ID="chkCOD" runat="server" class="checkbox" />

<itemtemplate>
<asp:CheckBox ID="chkOCD" runat="server" CssClass="btn btn-info btn-sm" />

<HeaderStyle ForeColor="#ffff00" />

<asp:TemplateField HeaderText="AGDB">
<edititemtemplate>
<asp:CheckBox ID="chkAGDB" runat="server" class="checkbox" />

<itemtemplate>
<asp:CheckBox ID="chkAGDB" runat="server" CssClass="btn btn-warning btn-sm" />

<HeaderStyle ForeColor="#ffff00" />

<asp:TemplateField HeaderText="SLDB">
<edititemtemplate>
<asp:CheckBox ID="chkSLDB" runat="server" class="checkbox" />

<itemtemplate>
<asp:CheckBox ID="chkSLDB" runat="server" CssClass="btn btn-success btn-sm" />

<HeaderStyle ForeColor="#ffff00" />


<%--<asp:templatefield>
<itemtemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Click1" OnClick="LinkButton1_Click" />

--%>


<asp:TemplateField HeaderText="Ann..">
<itemtemplate>
<asp:Button ID="Button1" runat="server" Text="Sound" CssClass="btn btn-danger"
CommandArgument="Button1" OnClick="Button1_Click" />

<HeaderStyle ForeColor="#ffff00" />

<asp:TemplateField HeaderText="Send">
<itemtemplate>
<asp:Button ID="Button2" runat="server" Text="Send" CssClass="btn btn-success"
CommandArgument="Button1" OnClick="Button2_Click" />

<HeaderStyle ForeColor="#ffff00" />

<asp:CommandField ShowEditButton="True" EditText="Save" ControlStyle-ForeColor="BlueViolet" ControlStyle-BorderStyle="Inset">
<ControlStyle BorderStyle="Inset" ForeColor="BlueViolet" />


<asp:TemplateField HeaderText="Delete">
<itemtemplate>
<asp:Button ID="deleteButton" runat="server" CommandName="Delete" Text="Delete" ControlStyle-CssClass="btn btn-primary btn-sm"
OnClientClick="return confirm('Are you sure you want to delete this user?');" />

<HeaderStyle ForeColor="#ffff00" />


<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="black" Font-Bold="True" />
<PagerSettings FirstPageText="First Page" LastPageText="Last Page" />
<PagerStyle CssClass="pgr" BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<AlternatingRowStyle CssClass="alt" BackColor="PaleGoldenrod" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />



function CheckAll(Checkbox) {
var GridVwHeaderCheckbox = document.getElementById("&lt;%=gvTrainStatus.ClientID %>");
for (i = 1; i &lt; GridVwHeaderCheckbox.rows.length; i++) {
GridVwHeaderCheckbox.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = Checkbox.checked;
}
}
Posted
Updated 18-Sep-18 1:02am

1 solution

You can use Session or query string


<asp:templatefield>
    <itemtemplate>
        <asp:CheckBox ID="flexCheckBoxList" runat="server" 
                      AutoPostBack="True" OnCheckedChanged="Check_Clicked" />
    </itemtemplate>
</asp:templatefield>

protected void Check_Clicked(Object sender, EventArgs e) 
{
    // Store the check box name, ID or whatever unique value you want in Session here
    CheckBox theCheckBox = sender as CheckBox;

    // Was the check box found?
    if(theCheckBox != null)
    {
        // Store in Session
        Session["CheckBoxValue"] = theCheckBox.SomePropertyValue;
    }
}
 
Share this answer
 
Comments
Shahbaz435 18-Sep-18 7:05am    
i have to send selected data from gridview and i have data on multiple pages on gridview so how can i send multiple data?

right now i have send only single page data i want send multiple pages data to the label plz help me and can u explain me above code plz

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900