So I have a new requirement that is tricky to me and I'm unable to figure out so far. I am using a gridview to insert, and update data. One of the requirements that I have is when a user manually adds a new record, if the same "deal number" exists then display a pop up. This pop up should show the record that already exists. They should be able to click 1 of 3 buttons which are "use", "discard", or "Ok". The "use" button will basically close the pop up and clear the textboxes the user was entering in. The "discard" button must delete the record that exists so the user can insert the new record using that same deal number. The reason for this is because the deal number is the most unique number for the "deals" that occur. With the way we are exporting from an old application to a new one, duplicates are downloaded, sometimes some with more information than the first time it was exported. This is why I must add a requirement for the user to choose which record for them to keep. I hope this makes sense to you all. I have dried a few things, but this is what I currently have and I'm stuck on.
The Jquery Script:
<script type="text/javascript">
$(document).ready(function () {
function showpopup() {
$("#popup").dialog("open");
}
$("#popup").dialog({
modal: true,
width: 450,
autoOpen: false,
open: function (type, data) {
$(this).parent().appendTo("form");
}
});
$("#popup").each(function () {
var popup = $(this);
popup.parent().appendTo($("form:first"));
});
});
</script>
And now the division the script calls which has another gridview to display the existing record..
<div class="popUpStyle" title="Duplicate Deal Found!" id="popup" style="display:none">
<asp:GridView ID="gvDealTracking" runat="server" Width="200px" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Stock #">
<ItemTemplate>
<asp:Label ID="lblDupStockNumber" runat="server" Text='<%# Bind("StockNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deal #">
<ItemTemplate>
<asp:Label ID="lblDupDealNumber" runat="server" Text='<%# Bind("FIMAST") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DealDate">
<ItemTemplate>
<asp:Label ID="lblDupDealDate" runat="server" Text='<%# Bind("DealDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Buyer">
<ItemTemplate>
<asp:Label ID="lblDupBuyer" runat="server" Text='<%# Bind("Buyer") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GrossProfit">
<ItemTemplate>
<asp:Label ID="lblDupGrossProfit" runat="server" Text='<%# Bind("GrossProfit") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AmtFinanced">
<ItemTemplate>
<asp:Label ID="lblDupAmtFinanced" runat="server" Text='<%# Bind("AmtFinanced") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BankName">
<ItemTemplate>
<asp:Label ID="lblDupBankName" runat="server" Text='<%# Bind("BankName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnUse" Text="Use" runat="server"></asp:Button>
<asp:Button ID="btnDiscard" Text="Discard" runat="server" OnClick="btnDiscard_Click" style="display:none"></asp:Button>
<asp:Label ID="lblMessagePop" runat="server"></asp:Label>
<br />
</div>
And now the code behind that I use to try to delete the existing record..
protected void btnDiscard_Click(object sender, EventArgs e)
{
try
{
GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
string dealnumber = ((Label)gvr.FindControl("lblDupDealNumber")).Text.Trim();
conn.Open();
SqlCommand cmdDeleteDup = new SqlCommand("DELETE * FROM Vehicle WHERE FIMAST = #FIMAST", conn);
cmdDeleteDup.Parameters.AddWithValue("#FIMAST", dealnumber);
cmdDeleteDup.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
lblMessagePop.Text = ex.ToString();
}
}
The button click isn't not firing and I do now know how to make it work. I tried a few different things but same results. I'm using a reader that checks to see if the record does exists and I show this pop up if it does have rows. It displays perfectly, just my buttons don't do anything. If this isn't the proper way to go about this, please let me know. Any guidance is greatly appreciated!
This is how I'm calling the popup in c#. This has reader that checks to see if the rows exist, and if so, it displays the existing record in the popup. I use a data adapter to do this. Then I use Page.ClientScript... to open the popup and display the results.
SqlDataReader rdr = null;
SqlCommand cmdCheckExisting = new SqlCommand("SELECT StockNumber, DealDate, Buyer FROM Vehicle WHERE FIMAST = '" + DealNumber + "';", conn);
rdr = cmdCheckExisting.ExecuteReader();
if (rdr.HasRows)
{
rdr.Close();
DataTable dt = new DataTable();
SqlDataAdapter cmdReturnExisting = new SqlDataAdapter("SELECT StockNumber, FIMAST, DealDate, Buyer, GrossProfit, AmtFinanced, BankName FROM Vehicle WHERE FIMAST = '" + DealNumber + "';", conn);
cmdReturnExisting.Fill(dt);
gvDealTracking.DataSource = dt;
gvDealTracking.DataBind();
conn.Close();
Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "showpopup();", true);
}
EDIT: Try removing "style='display:none'" from your div and add "autoOpen: false" into the dialog. Then the only thing your "showpopup()" function needs to do is call "$("#popup").dialog('open');"
$(document).ready(function() {
$("#popup").dialog({
modal: true,
width: 450,
autoOpen: false,
open: function(type,data) {
$(this).parent().appendTo("form");
}
});
$("#popup").each(function() {
var popup = $(this);
popup.parent().appendTo($("form:first"));
});
function showpopup() {
$("#popup").dialog("open");
}
});
I found a solution. I simply changed the buttons to LinkButtons. For some reason, everything works perfectly if asp linkbutton is placed outside or inside the gridview. Here is the popup with the gridview.
<script type="text/javascript">
function showpopup() {
$("#popup").dialog({
modal: true,
width: 590,
buttons: {
Ok: function () {
$(this).dialog("close");
}
}
});
};
</script>
<div class="popUpStyle" title="Duplicate Deal Found!" id="popup" style="display:none">
<asp:GridView ID="gvDealTracking" runat="server" Width="200px" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Stock #" HeaderStyle-Wrap="false">
<ItemTemplate>
<asp:Label ID="lblDupStockNumber" runat="server" Text='<%# Bind("StockNumber") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Dealership" SortExpression="Dealership">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Dealership") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Dealership") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deal #" HeaderStyle-Wrap="false">
<ItemTemplate>
<asp:Label ID="lblDupDealNumber" runat="server" Text='<%# Bind("FIMAST") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="DealDate">
<ItemTemplate>
<asp:Label ID="lblDupDealDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "DealDate","{0:MM/dd/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Buyer">
<ItemTemplate>
<asp:Label ID="lblDupBuyer" runat="server" Text='<%# Bind("Buyer") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GrossProfit">
<ItemTemplate>
<asp:Label ID="lblDupGrossProfit" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "GrossProfit","{0:n2}") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="AmtFinanced">
<ItemTemplate>
<asp:Label ID="lblDupAmtFinanced" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "AmtFinanced","{0:C}") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="BankName">
<ItemTemplate>
<asp:Label ID="lblDupBankName" runat="server" Text='<%# Bind("BankName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnUse" runat="server" CausesValidation="false" OnClick="btnUse_Click" UseSubmitBehavior="false" Text="Use"></asp:Button>
<asp:Button ID="lbDelete" runat="server" UseSubmitBehavior="false" CausesValidation="False" OnClick="lbDelete_Click" Text="Delete"></asp:Button>
<asp:Label ID="lblMessagePop" runat="server"></asp:Label>
<br />
</div>
EDIT: Thanks to Chad, he pointed out that asp buttons default to use the submit behavior. Setting the UseSubmitBehavior option to false within the button solves the issue. You are now able to use asp buttons within the popup to call your c# methods.
Related
I want to Fill gridview column value into given control
something like Project Title value fill inside project Title text box Problem ID fill inside Selected Problem Dropdown and so on... on button click even
i have taken as control name
Project title as txtProjectTitle,
selectProblem Id as ddlSelectProblem,
Project_Start_Date as txtProjectStartDate,
Project_Target_Date as TextBox1,
gridview as GrdTemp,
Procedd button as Button2_Click
ASPX CODE:
[![<asp:GridView ID="GrdTemp" runat="server" Style="width: 100%; text-align: center" class="table table-striped table-bordered" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="S.No." HeaderStyle-Width="5%">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("ID") %>' ID="lblID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Title">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Project_Title") %>' ID="lblID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Problem ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Problem") %>' ID="lblID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Start Date">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Project_Start_Date") %>' ID="lblID</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Target Date">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Project_Target_Date") %>' ID="lblID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns> </asp:GridView>][2]][2]
C# code:
protected void Button2_Click(object sender, EventArgs e)
{
GridViewRow row = (sender as Label).NamingContainer as GridViewRow;
TextBox txtProject = row.FindControl("txtProjectTitle") as TextBox;
txtProject.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["Project_Title"]);
DropDownList ddlProblem = row.FindControl("ddlSelectProblem") as DropDownList;
ddlSelectProblem.SelectedItem.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["Problem"]);
txtProjectStartDate.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["Project_Start_Date"]);
TextBox1.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["Project_Target_Date"]);
}
I have a Gridview with some data. In that I have 5 rows of paging. Whenever I check on checkbox in first page and go to second page and again I come to first page. The checkbox check value gets disappear.
The value of checked is not retained. How to get it maintain the viewstate of checkbox. Please suggest
CODE:
<asp:GridView ID="grdDisplayCMMData" runat="server" AutoGenerateColumns="false" Width="100%" ShowHeaderWhenEmpty="true" CssClass="heavyTable table" EmptyDataText="No records to display"
AllowPaging="true" PageSize="2" OnPageIndexChanging="grdDisplayCMMData_PageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID_CMM" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SAP ID">
<ItemTemplate>
<asp:Label ID="lblSAP_ID_CMM" runat="server" Text='<%#Eval("SAP_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID OD CHANGE">
<ItemTemplate>
<asp:Label ID="lblID_OD_COUNTCHANGE_CMM" runat="server" Text='<%#Eval("ID_OD_COUNTCHANGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID OD CHANGE DATE">
<ItemTemplate>
<asp:Label ID="lblID_OD_CHANGEDDATE_CMM" runat="server" Text='<%#Eval("ID_OD_CHANGEDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RRH COUNT CHANGE">
<ItemTemplate>
<asp:Label ID="lblRRH_COUNTCHANGE_CMM" runat="server" Text='<%#Eval("RRH_COUNTCHANGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RRH COUNT CHANGE DATE">
<ItemTemplate>
<asp:Label ID="lblRRH_CHANGEDDATE_CMM" runat="server" Text='<%#Eval("RRH_CHANGEDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TENANCY COUNT CHANGE">
<ItemTemplate>
<asp:Label ID="lblTENANCY_COUNTCHANGE_CMM" runat="server" Text='<%#Eval("TENANCY_COUNTCHANGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TENANCY COUNT CHANGE DATE">
<ItemTemplate>
<asp:Label ID="lblTENANCY_CHANGEDDATE_CMM" runat="server" Text='<%#Eval("TENANCY_CHANGEDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="STATUS">
<ItemTemplate>
<asp:Label ID="lblSTATUS_CMM" runat="server" Text='<%#Eval("STATUS") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CREATED BY" Visible="false">
<ItemTemplate>
<asp:Label ID="lblCREATEDBY_CMM" runat="server" Text='<%#Eval("CREATED_BY") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approve/Reject">
<ItemTemplate>
<asp:CheckBox ID="chkApprRejCMM" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
SERVER CODE
protected void grdDisplayCMMData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
BindCMMData();
grdDisplayCMMData.PageIndex = e.NewPageIndex;
grdDisplayCMMData.DataBind();
}
catch (Exception ex)
{
string strErrorMsg = ex.Message.ToString() + " " + "StackTrace :" + ex.StackTrace.ToString();
CommonDB.WriteLog("ERROR:" + strErrorMsg, ConfigurationManager.AppSettings["IPCOLO_LOG"].ToString());
}
}
Let me know if anything else is required
Microsoft provided example here.
check list is stored in view state as List.
To transfer data between pages you cannot use viewstate.
The options of transferring data between pages are Session storage, transferring via query string or storing the needed value into some kind of database.
Example for sessions storage at the most basic level:
On first page:
Session["CheckboxValue"] = chkSomeCheckbox.Checked;
On the second page:
bool isCheckboxChecked = Convert.ToBoolean(Session["CheckboxValue"])
I'm retrieving data from database into the grid for 5 column, there is another 3 columns beside that which I want to fill in using textbox input (using different button).
How to fetch the input from the textbox into the grid after I retrieve the data from database?
Here is the front looks like:
Here is the coding:
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="BNE No: "></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="BNE No: "></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search From Database" />
<br />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:Button ID="Button3" runat="server" Text="Insert to Grid" OnClick="Button3_Click" />
<br />
<%--<asp:Table ID="Table1" runat="server">
</asp:Table>--%>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="BNE No">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Bind("BNENo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ECN No">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%#Bind("KVNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rec Date">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%#Bind("ReceivedDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Issued Date">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%#Bind("IssuedDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%#Bind("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text=''></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text=''></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text=''></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
Here is the behind code where I use to retrieve the data by using button 1
protected void Button1_Click(object sender, EventArgs e)//this Button use To call multiple data from database named tblBNE into GridView
{
count = 0;
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT [BNENo],[KVNo],[ReceivedDate],[IssuedDate],[Status] FROM [tblBNE] where BNENo='" + TextBox1.Text + "' or BNENo='" + TextBox2.Text + "' ";
cmd.ExecuteNonQuery();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
count = Convert.ToInt32(dt.Rows.Count.ToString());
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
if (count == 0)
{
Label3.Text = "record not found";
}
}
So, I want to use button 3 = insert to grid to fill into label9 to 11 using textbox3 to 5. How can I do that?
Thank you in advance :)
I have been searching on here and other sites for three days for a solution. I need to select one or more rows (using checkbox) and delete selected row(s) from a gridview who's datasource is a session datatable as you will see. Not interested in SQLConnections or LINQ Entities. As a result I am not sure if I need to use e.commandarguments, or how to go about it, but I have tried them with no luck.
Error is visible and clearly commented on CART.ASPX.CS page inside the following method
if (chkRemCart != null && chkRemCart.Checked)
Any assistance would be greatly appreciated and if you require any further coding or explanations to help with a solution, just ask.
Thank you in advance.
CART.ASPX
<asp:GridView ID="gvCart" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="true" CellPadding="4"
HeaderStyle-CssClass="header" EmptyDataText="You have successfully cleared your Shopping Cart"
OnRowDataBound="gvCart_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="Movie Selector">
<ItemTemplate>
<asp:CheckBox ID="chkRemCart" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Movie ID">
<ItemTemplate>
<asp:Label ID="lblMovieID" runat="server" Text='<%# Eval("MovieId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duration">
<ItemTemplate>
<asp:Label ID="lblDuration" runat="server" Text='<%# Eval("Duration") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Genre">
<ItemTemplate>
<asp:Label ID="lblGenre" runat="server" Text='<%# Eval("Genre") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rating">
<ItemTemplate>
<asp:Label ID="lblRating" runat="server" Text='<%# Eval("Rating") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price","{0:n}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnRem" runat="server" OnClick="RemCart" Text="Remove Selection" CssClass="button" Title="Remove Movies From Cart" />
CART.ASPX.CS
Page_Load calling RefreshPages() method. gvCart displaying all columns and rows of items in Datasource/Session/Datatable
public void RefreshPages()
{
if (Session["SelectedMovies"] != null)
{
DataTable MovieTable = (DataTable)Session["SelectedMovies"];
gvCart.DataSource = MovieTable;
gvCart.DataBind();
}
}
protected void RemCart(object sender, EventArgs e)
{
// Check session exists
if (Session["selectedMovies"] != null)
{
// Opening / Retreiving DataTable.
DataTable MovieTable = (DataTable)Session["SelectedMovies"];
foreach (GridViewRow row in gvCart.Rows)
{
CheckBox chkRemCart = row.Cells[0].FindControl("chkRemCart") as CheckBox;
if (chkRemCart != null && chkRemCart.Checked)
{
// Error appearing on line below. Scroll right to read.
int MovieId = Convert.ToInt32(gvCart.DataKeys[row.RowIndex].Value); // Error here. //Additional information: Index was out of range. Must be non - negative and less than the size of the collection.
MovieTable.Rows.RemoveAt(row.RowIndex);
//Saving session
Session["selectedMovies"] = MovieTable;
// Updating gvCart
gvCart.DataSource = MovieTable;
gvCart.DataBind();
}
}
}
}
Assign DataKeyNames="PrimaryKey" in gridview like below and check
<asp:GridView ID="gvCart" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="true" DataKeyNames="MovieId" CellPadding="4"
HeaderStyle-CssClass="header" EmptyDataText="You have successfully cleared your Shopping Cart"
OnRowDataBound="gvCart_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="Movie Selector">
<ItemTemplate>
<asp:CheckBox ID="chkRemCart" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Movie ID">
<ItemTemplate>
<asp:Label ID="lblMovieID" runat="server" Text='<%# Eval("MovieId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duration">
<ItemTemplate>
<asp:Label ID="lblDuration" runat="server" Text='<%# Eval("Duration") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Genre">
<ItemTemplate>
<asp:Label ID="lblGenre" runat="server" Text='<%# Eval("Genre") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rating">
<ItemTemplate>
<asp:Label ID="lblRating" runat="server" Text='<%# Eval("Rating") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price","{0:n}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind:
public void RefreshPages()
{
if (Session["SelectedMovies"] != null)
{
DataTable MovieTable = (DataTable)Session["SelectedMovies"];
gvCart.DataSource = MovieTable;
gvCart.DataBind();
}
}
protected void RemCart(object sender, EventArgs e)
{
// Check session exists
if (Session["selectedMovies"] != null)
{
// Opening / Retreiving DataTable.
DataTable MovieTable = (DataTable)Session["SelectedMovies"];
foreach (GridViewRow row in gvCart.Rows)
{
CheckBox chkRemCart = row.Cells[0].FindControl("chkRemCart") as CheckBox;
if (chkRemCart != null && chkRemCart.Checked)
{
// Error appearing on line below. Scroll right to read.
int MovieId = Convert.ToInt32(gvCart.DataKeys[row.RowIndex].Value); // Error here. //Additional information: Index was out of range. Must be non - negative and less than the size of the collection.
DataRow[] drs = dt.Select("MovieId = '" + MovieId + "'"); // replace with your criteria as appropriate
if (drs.Length > 0)
{
MovieTable.Rows.Remove(drs[0]);
}
}
}
//Saving session
Session["selectedMovies"] = MovieTable;
// Updating gvCart
gvCart.DataSource = MovieTable;
gvCart.DataBind();
}
}
my data is not showing after clicking the Edit button from the Gridview.
here is my GridView code
<asp:UpdatePanel ID ="panel1" runat="server">
<ContentTemplate>
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID" AllowPaging="true" OnRowCommand="gv1_RowCommand" CellPadding="4" HeaderStyle-BackColor="CornflowerBlue" BorderWidth="5" BorderColor="CornflowerBlue" Width="100%" CssClass="table table-hover">
<Columns>
<asp:TemplateField HeaderText ="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Bind ("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Type">
<ItemTemplate>
<asp:Label ID="lbltype" runat="server" Text='<%#Bind ("ItemType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Model">
<ItemTemplate>
<asp:Label ID="lblModel" runat="server" Text='<%#Bind ("ItemModel") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Quantity">
<ItemTemplate>
<asp:Label ID="lblQuan" runat="server" Text='<%#Bind ("ItemQuantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Unit">
<ItemTemplate>
<asp:Label ID="lblUnit" runat="server" Text='<%#Bind ("ItemUnit") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Target Date">
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%#Bind ("ItemDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Description">
<ItemTemplate>
<asp:Label ID="lblDesc" runat="server" Text='<%#Bind ("ItemDesc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Remarks">
<ItemTemplate>
<asp:Label ID="lblRem" runat="server" Text='<%#Bind ("ItemRemarks") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="editRecord" ControlStyle-CssClass="btn btn-info" ButtonType="Button" Text="Edit" HeaderText="Edit">
<ControlStyle CssClass="btn btn-info" />
</asp:ButtonField>
<%--<asp:TemplateField HeaderText ="Status">
<ItemTemplate>
<asp:Label ID="lblStat" runat="server" Text='<%#Bind ("ItemStatus") %>'></asp:Label>
<asp:DropDownList ID ="ddlStat" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>--%>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>
and here is my code for .cs
int index = Convert.ToInt32(e.CommandArgument);
//if(e.CommandName.Equals("editRecord"))
//{
GridViewRow gvrow = gv1.Rows[index];
lblIDs.Text = HttpUtility.HtmlDecode(gvrow.Cells[0].Text).ToString() ;
lblitemTypes.Text = HttpUtility.HtmlDecode(gvrow.Cells[1].Text).ToString();
lblModels.Text = HttpUtility.HtmlDecode(gvrow.Cells[2].Text).ToString();
lblQuans.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
lblUnits.Text = HttpUtility.HtmlDecode(gvrow.Cells[4].Text).ToString();
lblDates.Text = HttpUtility.HtmlDecode(gvrow.Cells[5].Text).ToString();
lblDescs.Text = HttpUtility.HtmlDecode(gvrow.Cells[6].Text).ToString();
lblRemarkss.Text = HttpUtility.HtmlDecode(gvrow.Cells[7].Text).ToString();
//ddlStat.Text = HttpUtility.HtmlDecode(gvrow.Cells[8].Text).ToString();
lblResult.Visible = false;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(#"<script type='text/javascript'>");
sb.Append("$('#editModal').modal('show');");
sb.Append(#"</script>");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "EditModalScript", sb.ToString(), false);
the page goes through but it doesnt show the data from the gridview, i tried to put a breakpoint and i see that it doesnt have anyvalue inside, what is my error? is the HttpUtility.HtmlDecode correct? how can i transfer the data to the modal for editing. thank you!
When you have an UpdatePanel warp your code and your code not work after a post back you need to do two thinks to find the issue.
Temporary Remove the UpdatePanel and repeat what you do and see if the problem is on your code behind.
place back the UpdatePanel and open the javascript console of your browser and check for javascript errors.