JQGrid export using SelectPDF, no data in linked JQGrid - asp.net

I have 2 JQGrids on my ASPX page. When an Invoice is selected on the top grid, it loads the corresponding data on the second.
My issue is with exporting. When I export the page using SelectPDF, the 2nd Grid View is always empty.
Here is what it looks like on the page:
...and this is what the exported PDF looks like, there is no data in the second grid.
Here is the ASPX code:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" Inherits="Invoicing" Codebehind="Invoicing.aspx.cs" EnableEventValidation="false" %>
<%# Register Assembly="Trirand.Web" TagPrefix="trirand" Namespace="Trirand.Web.UI.WebControls" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<link href="themes/CustomStyles.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" media="screen" href="themes/jquery-ui.css" />
<link rel="stylesheet" type="text/css" media="screen" href="themes/ui.jqgrid.css" />
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/trirand/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="js/trirand/jquery.jqGrid.min.js" type="text/javascript"></script>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Indigo2.Properties.Settings.Constr %>"
SelectCommand="SELECT [invoice_number],[invoice_date],[custid],[inv_splitter] FROM [Indigo].[dbo].[invoices]" CancelSelectOnNullParameter="false"
UpdateCommand=""
DeleteCommand="">
<UpdateParameters>
<asp:Parameter Name="invoice_number" Type="Int32" />
<asp:Parameter Name="invoice_date" Type="DateTime" />
<asp:Parameter Name="custid" Type="String" />
<asp:Parameter Name="inv_splitter" Type="String" />
</UpdateParameters>
<DeleteParameters>
</DeleteParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Indigo2.Properties.Settings.Constr %>"
SelectCommand="SELECT [inv_item_id],[description],[unit_sell],[qty],[inv_section],[inv_orderby],[inv_splitter],[ref_date_start],[ref_date_end],[custid],[cust_po],[invoice_number], unit_sell * qty as Price FROM [Indigo].[dbo].[invoice_items] WHERE (invoice_number = #invoice_number) " CancelSelectOnNullParameter="false"
UpdateCommand="UPDATE Invoice_items SET cust_po=#cust_po, description=#description, ref_date_start=CONVERT(datetime, #ref_date_start, 105), ref_date_end=CONVERT(datetime, #ref_date_end, 105), unit_sell=#unit_sell, qty=#qty WHERE inv_item_id=#inv_item_id"
DeleteCommand="DELETE FROM Invoice_items WHERE inv_item_id=#inv_item_id">
<SelectParameters>
<asp:Parameter DefaultValue="" Name="invoice_number" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="inv_item_id" Type="Int32" />
<asp:Parameter Name="cust_po" Type="String" />
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="ref_date_start" Type="DateTime" />
<asp:Parameter Name="ref_date_end" Type="DateTime" />
<asp:Parameter Name="unit_sell" Type="Decimal" />
<asp:Parameter Name="qty" Type="Int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="inv_item_id" Type="Int32" />
<asp:Parameter Name="cust_po" Type="String" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<br />
<trirand:JQGrid runat="server" ID="JQGrid1" DataSourceID="SqlDataSource1" Width="1200"
OnRowSelecting="JQGrid1_RowSelecting">
<Columns>
<trirand:JQGridColumn DataField="invoice_number" HeaderText="ID" PrimaryKey="True" Width="100" />
<trirand:JQGridColumn DataField="invoice_date" Editable="true" DataFormatString="{0:d}" Width="100"/>
<trirand:JQGridColumn DataField="custid" Editable="true" Width="100"/>
</Columns>
<ToolBarSettings ShowEditButton="true" ShowRefreshButton="True" ShowAddButton="true" ShowDeleteButton="true" ShowSearchButton="True" />
</trirand:JQGrid>
<br />
<trirand:JQGrid runat="server" ID="JQGrid2" DataSourceID="SqlDataSource2" Width="1200" Height="100%" maxHeight="300" hidegrid="true" ondatarequested="JQGrid2_DataRequested">
<Columns>
<trirand:JQGridColumn CSSClass="indent" DataField="description" Editable="true" Width="800"/>
<trirand:JQGridColumn DataField="inv_section" Editable="true" />
<trirand:JQGridColumn DataField="ref_date_start" Editable="true" DataFormatString="{0:d}" Width="200"/>
<trirand:JQGridColumn DataField="ref_date_end" Editable="true" DataFormatString="{0:d}" Width="200"/>
<trirand:JQGridColumn DataField="cust_po" Editable="true" />
<trirand:JQGridColumn DataField="unit_sell" Editable="true" DataFormatString="{0:c}" Width="200"/>
<trirand:JQGridColumn DataField="qty" Editable="true" Width="200"/>
<trirand:JQGridColumn DataField="price" Editable="false" DataFormatString="{0:c}" Width="200" >
</trirand:JQGridColumn>
</Columns>
<PagerSettings PageSize="12" />
<GroupSettings CollapseGroups="false">
<GroupFields>
<trirand:GroupField
DataField="inv_section"
HeaderText="<b>{0}</b>"
GroupSortDirection="Asc"
ShowGroupColumn="false"
ShowGroupSummary="true"/>
<trirand:GroupField
DataField="cust_po"
HeaderText="PO Reference: <b>{0}</b&gt"
GroupSortDirection="Asc"
ShowGroupColumn="false"
ShowGroupSummary="false"/>
</GroupFields>
</GroupSettings>
<AppearanceSettings ShowFooter="true" />
<ToolBarSettings ShowEditButton="true" ShowRefreshButton="True" ShowAddButton="true" ShowDeleteButton="true" ShowSearchButton="True" />
</trirand:JQGrid>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button runat="server" ID="ExportToPDFButton" Text="Export to PDF" OnClick="BtnCreatePdf_Click" />
...and here is the underlying code
using System;
using System.Web.UI;
using Trirand.Web.UI.WebControls;
using System.IO;
using System.Data;
using SelectPdf;
using System.Web;
public partial class Invoicing : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
JQGrid2.Visible = true; //was false!!!!!!!!!!!!!!
if (Session["invoice_number"] != null)
{
JQGrid2.Visible = true;
SqlDataSource2.SelectParameters["invoice_number"].DefaultValue = Session["invoice_number"] as string;
}
}
protected void JQGrid2_DataRequested(object sender, Trirand.Web.UI.WebControls.JQGridDataRequestedEventArgs e)
{
DataTable dt = e.DataTable; // get only the current page data
double poTotal = 0;
foreach (DataRow row in dt.Rows)
{
double poValue = (double)row["price"];
poTotal += poValue;
}
string formattedMoneyValue = String.Format("{0:C}", poTotal);
JQGrid2.Columns.FromDataField("price").FooterValue = formattedMoneyValue.ToString();
JQGrid2.Columns.FromDataField("qty").FooterValue = "Total:";
}
protected void JQGrid1_RowSelecting(object sender, Trirand.Web.UI.WebControls.JQGridRowSelectEventArgs e)
{
Session["invoice_number"] = JQGrid1.SelectedRow;
JQGrid2.Visible = true;
SqlDataSource2.SelectParameters["invoice_number"].DefaultValue = Session["invoice_number"] as string;
}
//Start of Select PDF Code//
private bool startConversion = false;
protected void BtnCreatePdf_Click(object sender, EventArgs e)
{
startConversion = true;
}
protected override void Render(HtmlTextWriter writer)
{
if (startConversion)
{
Session["invoice_number"] = JQGrid1.SelectedRow;
JQGrid2.Visible = true;
SqlDataSource2.SelectParameters["invoice_number"].DefaultValue = Session["invoice_number"] as string;
// get html of the page
TextWriter myWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(myWriter);
base.Render(htmlWriter);
// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();
// create a new pdf document converting the html string of the page
PdfDocument doc = converter.ConvertHtmlString(
myWriter.ToString(), Request.Url.AbsoluteUri);
// save pdf document
doc.Save(Response, false, "Sample.pdf");
// close pdf document
doc.Close();
}
else
{
// render web page in browser
base.Render(writer);
}
}
}
Please can someone help me, with what I need to do so that the data from the second grid is passed to the PDF when exported.
UPDATE:
If I hardcode the default value for the datasource on the second grid it exports the correct info.
<SelectParameters>
<asp:Parameter DefaultValue="1000" Name="invoice_number" Type="Int32" />
</SelectParameters>
For some reason when I export, it seems to be clearing this parameter value, if I set it dynamically.

The SelectPdf convert loads your web page using a new Session, so it does not know your dynamically set parameters. You need to send them in the url, if possible.

Related

GridView disappears on select edit and update

I have a GridView with a sql Source, which is bound to a searchbox. The problem is that on Postback the GridView disappears. I have tried to DataBind the GridView in the PageLoad event but the problem is that the Data is coming from the searchbox so On Postback it takes out the value of the Searchbox and searches for it.
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Archiv.aspx.cs" Inherits="AutoVerwaltungASP.Archiv" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:TextBox id="searchbox" Value="Search..." runat="server" ForeColor="Gray" onblur="if(this.value=='')this.value=this.defaultValue;" onfocus="if(this.value==this.defaultValue)this.value='';" />
<asp:Button Text="Suchen" runat="server" ID="btnSearch" OnClick="btnSearch_Click" Height="34px" Width="90px" Font-Size="Small"/>
<asp:Button Text="Reset" runat="server" ID="btnClear" OnClick="btnClear_Click" Height="34px" Width="90px" Font-Size="Small"/>
<cc1:AutoCompleteExtender ServiceMethod="markensuchen"
MinimumPrefixLength="2"
CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
TargetControlID="searchbox"
ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSourceAutos" ForeColor="Black" GridLines="Vertical" DataKeyNames="m_id" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="m_id" HeaderText="m_id" SortExpression="m_id" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="m_name" HeaderText="m_name" SortExpression="m_name" />
<asp:BoundField DataField="m_h_id" HeaderText="m_h_id" SortExpression="m_h_id" />
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceAutos" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [m_id], [m_name], [m_h_id] FROM [marke]" FilterExpression="m_name LIKE '%{0}%'" ConflictDetection="CompareAllValues" DeleteCommand="DELETE FROM [marke] WHERE [m_id] = #original_m_id AND [m_name] = #original_m_name AND [m_h_id] = #original_m_h_id" InsertCommand="INSERT INTO [marke] ([m_name], [m_h_id]) VALUES (#m_name, #m_h_id)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [marke] SET [m_name] = #m_name, [m_h_id] = #m_h_id WHERE [m_id] = #original_m_id AND [m_name] = #original_m_name AND [m_h_id] = #original_m_h_id" >
<DeleteParameters>
<asp:Parameter Name="original_m_id" Type="Int32" />
<asp:Parameter Name="original_m_name" Type="String" />
<asp:Parameter Name="original_m_h_id" Type="Int32" />
</DeleteParameters>
<FilterParameters>
<asp:ControlParameter Name="m_name" ControlID="searchbox" PropertyName="Text" />
</FilterParameters>
<InsertParameters>
<asp:Parameter Name="m_name" Type="String" />
<asp:Parameter Name="m_h_id" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="m_name" Type="String" />
<asp:Parameter Name="m_h_id" Type="Int32" />
<asp:Parameter Name="original_m_id" Type="Int32" />
<asp:Parameter Name="original_m_name" Type="String" />
<asp:Parameter Name="original_m_h_id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
public partial class Archiv : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
GridView1.DataBind();
}
}
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> markensuchen(string prefixText, int count)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select m_name from marke where " +
"m_name like #searchbox + '%'";
cmd.Parameters.AddWithValue("#searchbox", prefixText);
cmd.Connection = conn;
conn.Open();
List<string> marken = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
marken.Add(sdr["m_name"].ToString());
}
}
conn.Close();
return marken;
}
}
}
private string SearchString = "";
public string HighlightText(string InputTxt)
{
string Search_Str = searchbox.Text;
// Setup the regular expression and add the Or operator.
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
// Highlight keywords by calling the
//delegate each time a keyword is found.
return RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));
}
public string ReplaceKeyWords(Match m)
{
return ("<span class=highlight>" + m.Value + "</span>");
}
protected void btnSearch_Click(object sender, EventArgs e)
{
SearchString = searchbox.Text;
}
protected void btnClear_Click(object sender, EventArgs e)
{
searchbox.Text = "";
SearchString = "";
GridView1.DataBind();
}
}
}

DetailsView in Modalpopup won't load on second click on edit button

I have a GridView that when you click the edit button it loads a DetailsView in Edit Mode inside of a ModalPopup. It works fine the first time you click edit. If you click edit again to make changes to a second record, it loads but the DetailsView is not in the ModalPopup.
Can anyone tell me what I"m doing wrong? My code is below.
GridView datasource:
<asp:SqlDataSource ID="sdsMembers" runat="server"
ConnectionString="<%$ ConnectionStrings:DoseRec_ABTConnectionString %>"
SelectCommand="SELECT [intMemberID], [vcharTitle], [vcharFirstName], [vcharLastName], [vcharSuffix], [vcharJobTitle], [vcharAddress1], [vcharAddress2], [vcharCity], [vcharState], [vcharZipCode], [vcharPhone], [vcharFax], [bitActive] FROM [tbl_Members]"
DeleteCommand="DELETE FROM [tbl_Members] WHERE [intMemberID] = #intMemberID"
InsertCommand="INSERT INTO [tbl_Members] ([vcharTitle], [vcharFirstName], [vcharLastName], [vcharSuffix], [vcharJobTitle], [vcharAddress1], [vcharAddress2], [vcharCity], [vcharState], [vcharZipCode], [vcharPhone], [vcharFax], [bitActive]) VALUES (#vcharTitle, #vcharFirstName, #vcharLastName, #vcharSuffix, #vcharJobTitle, #vcharAddress1, #vcharAddress2, #vcharCity, #vcharState, #vcharZipCode, #vcharPhone, #vcharFax, #bitActive)"
UpdateCommand="UPDATE [tbl_Members] SET [vcharTitle] = #vcharTitle, [vcharFirstName] = #vcharFirstName, [vcharLastName] = #vcharLastName, [vcharSuffix] = #vcharSuffix, [vcharJobTitle] = #vcharJobTitle, [vcharAddress1] = #vcharAddress1, [vcharAddress2] = #vcharAddress2, [vcharCity] = #vcharCity, [vcharState] = #vcharState, [vcharZipCode] = #vcharZipCode, [vcharPhone] = #vcharPhone, [vcharFax] = #vcharFax, [bitActive] = #bitActive WHERE [intMemberID] = #intMemberID">
<DeleteParameters>
<asp:Parameter Name="intMemberID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="vcharTitle" Type="String" />
<asp:Parameter Name="vcharFirstName" Type="String" />
<asp:Parameter Name="vcharLastName" Type="String" />
<asp:Parameter Name="vcharSuffix" Type="String" />
<asp:Parameter Name="vcharJobTitle" Type="String" />
<asp:Parameter Name="vcharAddress1" Type="String" />
<asp:Parameter Name="vcharAddress2" Type="String" />
<asp:Parameter Name="vcharCity" Type="String" />
<asp:Parameter Name="vcharState" Type="String" />
<asp:Parameter Name="vcharZipCode" Type="String" />
<asp:Parameter Name="vcharPhone" Type="String" />
<asp:Parameter Name="vcharFax" Type="String" />
<asp:Parameter Name="bitActive" Type="Boolean" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="vcharTitle" Type="String" />
<asp:Parameter Name="vcharFirstName" Type="String" />
<asp:Parameter Name="vcharLastName" Type="String" />
<asp:Parameter Name="vcharSuffix" Type="String" />
<asp:Parameter Name="vcharJobTitle" Type="String" />
<asp:Parameter Name="vcharAddress1" Type="String" />
<asp:Parameter Name="vcharAddress2" Type="String" />
<asp:Parameter Name="vcharCity" Type="String" />
<asp:Parameter Name="vcharState" Type="String" />
<asp:Parameter Name="vcharZipCode" Type="String" />
<asp:Parameter Name="vcharPhone" Type="String" />
<asp:Parameter Name="vcharFax" Type="String" />
<asp:Parameter Name="bitActive" Type="Boolean" />
<asp:Parameter Name="intMemberID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
DetailsView datasource:
<asp:SqlDataSource ID="sdsMembersDetail" runat="server"
ConnectionString="<%$ ConnectionStrings:DoseRec_ABTConnectionString %>"
SelectCommand="SELECT [intMemberID] AS ID, [vcharTitle] AS Title, [vcharFirstName] AS 'First Name', [vcharLastName] AS 'Last Name', [vcharSuffix] AS Suffix, [vcharJobTitle] AS 'Job Title', [vcharAddress1] AS Address1, [vcharAddress2] AS Address2, [vcharCity] AS City, [vcharState] AS State, [vcharZipCode] AS 'Zip Code', [vcharPhone] AS Phone, [vcharFax] AS Fax, [bitActive] AS Active FROM [tbl_Members] WHERE ([intMemberID] = #intMemberID)">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="" Name="intMemberID"
QueryStringField="intMemberID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
GridView markup:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvMembers" runat="server" AutoGenerateColumns="False"
DataKeyNames="intMemberID" DataSourceID="sdsMembers" style="background-color:White;">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField ControlStyle-Width="50px" HeaderStyle-Width="60px">
<ItemTemplate>
<asp:LinkButton ID="btnViewDetails" runat="server" OnClick="BtnViewDetails_Click">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="intMemberID" HeaderText="ID"
InsertVisible="False" ReadOnly="True" SortExpression="intMemberID" />
<asp:BoundField DataField="vcharTitle" HeaderText="vcharTitle"
SortExpression="vcharTitle" />
<asp:BoundField DataField="vcharFirstName" HeaderText="First Name"
SortExpression="vcharFirstName" />
<asp:BoundField DataField="vcharLastName" HeaderText="Last Name"
SortExpression="vcharLastName" />
<asp:BoundField DataField="vcharSuffix" HeaderText="Suffix"
SortExpression="vcharSuffix" />
<asp:BoundField DataField="vcharJobTitle" HeaderText="Job Title"
SortExpression="vcharJobTitle" />
<asp:BoundField DataField="vcharAddress1" HeaderText="Address1"
SortExpression="vcharAddress1" />
<asp:BoundField DataField="vcharAddress2" HeaderText="Address2"
SortExpression="vcharAddress2" />
<asp:BoundField DataField="vcharCity" HeaderText="City"
SortExpression="vcharCity" />
<asp:BoundField DataField="vcharState" HeaderText="State"
SortExpression="vcharState" />
<asp:BoundField DataField="vcharZipCode" HeaderText="Zip Code"
SortExpression="vcharZipCode" />
<asp:BoundField DataField="vcharPhone" HeaderText="Phone"
SortExpression="vcharPhone" />
<asp:BoundField DataField="vcharFax" HeaderText="Fax"
SortExpression="vcharFax" />
<asp:CheckBoxField DataField="bitActive" HeaderText="Active"
SortExpression="bitActive" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
DetailsView marup:
<asp:Button id="btnShowPopup" runat="server" style="display:none" />
<ajaxToolKit:ModalPopupExtender
ID="mdlPopup" runat="server" TargetControlID="btnShowPopup" PopupControlID="pnlPopup"
CancelControlID="btnClose" BackgroundCssClass="modalBackground" />
<asp:Panel ID="pnlPopup" runat="server" Width="500px" style="display:none">
<asp:UpdatePanel ID="updPnlCustomerDetail" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblMemberDetail" runat="server" Text="Member Detail" BackColor="lightblue" Width="95%" />
<asp:DetailsView ID="dvMemberDetail" runat="server" DefaultMode="Edit" Width="95%" BackColor="white" OnItemUpdating="dvMembersDetail_ItemUpdating">
</asp:DetailsView>
</ContentTemplate>
</asp:UpdatePanel>
<div align="right" style="width:95%">
<asp:LinkButton
ID="btnSave" runat="server" Text="Save"
Width="50px" onclick="btnSave_Click" />
<asp:LinkButton ID="btnClose" runat="server">Close</asp:LinkButton>
</div>
</asp:Panel>
Click event (to display the DetailsView):
protected void BtnViewDetails_Click(object sender, EventArgs e)
{
LinkButton btnDetails = sender as LinkButton;
GridViewRow row = (GridViewRow)btnDetails.NamingContainer;
this.sdsMembersDetail.SelectParameters.Clear();
this.sdsMembersDetail.SelectParameters.Add("intMemberID", Convert.ToString(this.gvMembers.DataKeys[row.RowIndex].Value));
this.dvMemberDetail.DataSource = this.sdsMembersDetail;
this.dvMemberDetail.DataBind();
this.updPnlCustomerDetail.Update();
this.mdlPopup.Show();
}
If any other code, like the code for my save button is needed just let me know and I will post it.
Here's my Save code:
protected void btnSave_Click(object sender, EventArgs e)
{
if (this.Page.IsValid)
{
this.dvMemberDetail.UpdateItem(true);
this.dvMemberDetail.ChangeMode(DetailsViewMode.ReadOnly);
this.dvMemberDetail.Visible = false;
this.mdlPopup.Hide();
this.gvMembers.DataBind();
this.UpdatePanel1.Update();
}
}
protected void dvMembersDetail_ItemUpdating(object sender, EventArgs e)
{
using (DoseRec_ABTEntities1 dbContext = new DoseRec_ABTEntities1())
{
TextBox id = (TextBox)dvMemberDetail.Rows[0].Cells[1].Controls[0];
int intID = Convert.ToInt32(id.Text);
tbl_Members mem = (from m in dbContext.tbl_Members
where m.intMemberID == intID
select m).Single();
TextBox title = (TextBox)dvMemberDetail.Rows[1].Cells[1].Controls[0];
TextBox firstname = (TextBox)dvMemberDetail.Rows[2].Cells[1].Controls[0];
TextBox lastname = (TextBox)dvMemberDetail.Rows[3].Cells[1].Controls[0];
TextBox suffix = (TextBox)dvMemberDetail.Rows[4].Cells[1].Controls[0];
TextBox jobtitle = (TextBox)dvMemberDetail.Rows[5].Cells[1].Controls[0];
TextBox address1 = (TextBox)dvMemberDetail.Rows[6].Cells[1].Controls[0];
TextBox address2 = (TextBox)dvMemberDetail.Rows[7].Cells[1].Controls[0];
TextBox city = (TextBox)dvMemberDetail.Rows[8].Cells[1].Controls[0];
TextBox state = (TextBox)dvMemberDetail.Rows[9].Cells[1].Controls[0];
TextBox zipcode = (TextBox)dvMemberDetail.Rows[10].Cells[1].Controls[0];
TextBox phone = (TextBox)dvMemberDetail.Rows[11].Cells[1].Controls[0];
TextBox fax = (TextBox)dvMemberDetail.Rows[12].Cells[1].Controls[0];
CheckBox active = (CheckBox)dvMemberDetail.Rows[13].Cells[1].Controls[0];
mem.vcharTitle = title.Text;
mem.vcharFirstName = firstname.Text;
mem.vcharLastName = lastname.Text;
mem.vcharSuffix = suffix.Text;
mem.vcharJobTitle = jobtitle.Text;
mem.vcharAddress1 = address1.Text;
mem.vcharAddress2 = address2.Text;
mem.vcharCity = city.Text;
mem.vcharState = state.Text;
mem.vcharZipCode = zipcode.Text;
mem.vcharPhone = phone.Text;
mem.vcharFax = fax.Text;
mem.bitActive = active.Checked;
dbContext.SaveChanges();
}
}

ASP.NET Could not find control in ControlParamater

I'm pretty lost at this point (been working at it for a while not and am hitting a wall / deadline) but the error message I am being thrown is after I hit btnupdate to update the fields in the database.
Full Error Message:
Could not find control 'txtTitle' in ControlParameter 'Title'.
Page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="floater">
<h3>Books</h3>
<asp:DropDownList
id="DropDownList_Books"
DataSourceId="srcBooks"
DataTextField="Title"
DataValueField="Id"
Runat="server" />
<asp:Button ID="btnSelect" runat="server" Text="View Detials" Width="106px"
onclick="btnSelect_Click" />
</div>
<asp:GridView
id="grdBooks"
DataSourceID="srcBooks_Description"
Runat="server" Visible="False" />
<asp:Button ID="btnEdit" runat="server" onclick="btnEdit_Click" Text="Edit" />
</div>
<asp:Button ID="btnCancel" runat="server" onclick="btnCancel_Click"
Text="Cancel" Visible="False" />
<asp:FormView
id="frmEditBook"
DataKeyNames="Cat_Id"
DataSourceId="srcBooks_Description"
DefaultMode="Edit"
Runat="server" Visible="False"
style="z-index: 1; left: 391px; top: 87px; position: absolute; height: 111px; width: 206px" >
<EditItemTemplate>
<asp:Label
id="lblTitle"
Text="Title:"
AssociatedControlID="txtTitle"
Runat="server" />
<asp:TextBox
id="txtTitle"
Text='<%#Bind("Title")%>'
Runat="server" />
<br />
<asp:Label
id="lblDescription"
Text="Description:"
AssociatedControlID="txtDescription"
Runat="server" />
<asp:TextBox
id="txtDescription"
Text='<%#Bind("Description")%>'
Runat="server" />
<br />
<asp:Button
id="btnUpdate"
Text="Update"
CommandName="Update"
Runat="server" />
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="srcBooks" runat="server"
ConnectionString="Data Source=****;;Initial Catalog=***;Persist Security Info=True;User ID=****;Password=****"
onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT Title,Id FROM PARTIN_ID">
</asp:SqlDataSource>
<asp:SqlDataSource ID="srcBooks_Description" runat="server"
ConnectionString="Data Source=****, 14330";Initial Catalog=****;Persist Security Info=True;User ID=****;Password=****"
onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM PARTIN_INFO WHERE Cat_ID=#Id" UpdateCommand="UPDATE PARTIN_INFO SET Title=#Title,
Description=#Description WHERE Cat_Id=#Id">
<SelectParameters>
<asp:ControlParameter
Name="Id"
Type="int32"
ControlID="DropDownList_Books"
PropertyName="SelectedValue" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="Title" ControlId="txtTitle" PropertyName="Text"/>
<asp:ControlParameter Name="Description" ControlId="txtDescription" PropertyName="Text"/>
<asp:ControlParameter Name="Id" ControlId="DropDownList_Books" PropertyName="SelectedValue"/>
</UpdateParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
Code Behind:
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void srcBooks_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
protected void btnSelect_Click(object sender, EventArgs e)
{
grdBooks.Visible = true;
}
protected void btnCancel_Click(object sender, EventArgs e)
{
frmEditBook.Visible = false;
}
protected void btnEdit_Click(object sender, EventArgs e)
{
frmEditBook.Visible = true;
btnCancel.Visible = true;
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
srcBooks_Description.Update();
}
catch (Exception except)
{
// Handle the Exception.
}
}
}
}
You -can- refer to the control adequately.
You need to prefix the controlid value of your update parameters with the control id of your enclosing view (FormView 'frmEditBook' in this case)
Replace the UpdateParamters section of your SqlDataSource with the following:
<UpdateParameters>
<asp:ControlParameter Name="Title" ControlId="frmEditBook$txtTitle" PropertyName="Text"/>
<asp:ControlParameter Name="Description" ControlId="frmEditBook$txtDescription" PropertyName="Text"/>
<asp:ControlParameter Name="Id" ControlId="DropDownList_Books" PropertyName="SelectedValue"/>
</UpdateParameters>
Note that
ControlId="DropDownList_Books"
remains the same as this control is not within the bounds of the FormView.
I don't recommend using the 'full name' provided by the browser... particularly if you're using master pages... it could change as you rearrange your layouts.
As you can see, the problem is the context. You can also put your sqldatasource inside the formview where the control being refered to is (not properly and very limited but could work)
Let's say you have a detailsview with two dropdowns in it, and one is dependant from another. What works for me is to put the Datasource in the same context (parent control) as the control refered and this will eliminate the need to reference the full path of the control.
My two cents is that this is very reminiscent on how you must reference controls in Access Forms with subforms. :)
<asp:DetailsView runat="server">
<asp:TemplateField>
<EditTemplate>
<asp:DropDownList id="ParentDDL" Datasource="SQLDataSource1">
</asp:DropDownList>
</EditTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditTemplate>
<asp:DropDownList id="ParentDDL" Datasource="SQLDatasource2">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * from table where field1=#field">
<SelectParameters>
<asp:ControlParameter ControlID="ParentDDL" Name="field"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</EditTemplate>
</asp:TemplateField>
</asp:DetailsView>
You need to refer to your control with its full name.
You can easily find the control's full name when you open the page in the browser. Simply view source and you will see it.
The problem is that txtTitle is inside a FormView so it's not accesible in the way you're trying to do on the SqlDataSource. You can't access it direclty by ID.
Here you have an MSDN article that may help to to work with a FormView and SqlDataSource
you shouldn't have used <asp:ControlParameter.. but <asp:Parameter.. and the SqlDataSource control with the help of the bind method is intelligent enough to locate the controls to get values from for the parameters.
Update this portion of the sqlDataSource this way:
....
<UpdateParameters>
<asp:Parameter Name="Title" Type="String"/>
<asp:Parameter Name="Description" Type="String"/>
<asp:Parameter Name="Id" Type="Int32"/>
</UpdateParameters>
.....
Please note the Type attribute and supply the relevant type.
(The problem is SqlDataSource control cannot explicitly access controls defined in a databound control)

shopping cart total price issue

I am working on a shopping cart project for my college project in final page of my cart i want to calculate the total amount for the all the product in the cart help me in that code for that is
for cart.aspx
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="id"
DataSourceID="SqlDataSource1"
EmptyDataText="No Item in the Cart">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="pName"
HeaderText="pName"
SortExpression="pName" />
<asp:BoundField DataField="brand"
HeaderText="brand"
SortExpression="brand" />
<asp:TemplateField HeaderText="img"
SortExpression="img">
<EditItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"
Text='<%# Bind("img") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1"
runat="server"
ImageUrl='<%# Bind("img") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="quantity"
HeaderText="quantity"
SortExpression="quantity" />
<asp:BoundField DataField="price"
HeaderText="price"
SortExpression="price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<% $ConnectionStrings:shopingConnectionString1 %>"
DeleteCommand="DELETE FROM [completeCart] WHERE [id] = #id"
InsertCommand="INSERT INTO [completeCart] ([uName], [pName], [brand], [img], [quantity], [price]) VALUES (#uName, #pName, #brand, #img, #quantity, #price)"
SelectCommand="SELECT * FROM [completeCart] WHERE ([uName] = #uName)"
UpdateCommand="UPDATE [completeCart] SET [uName] = #uName, [pName] = #pName, [brand] = #brand, [img] = #img, [quantity] = #quantity, [price] = #price WHERE [id] = #id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int64" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="uName" Type="String" />
<asp:Parameter Name="pName" Type="String" />
<asp:Parameter Name="brand" Type="String" />
<asp:Parameter Name="img" Type="String" />
<asp:Parameter Name="quantity" Type="Int32" />
<asp:Parameter Name="price" Type="Int64" />
</InsertParameters>
<SelectParameters>
<asp:CookieParameter CookieName="uname"
Name="uName"
Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="uName" Type="String" />
<asp:Parameter Name="pName" Type="String" />
<asp:Parameter Name="brand" Type="String" />
<asp:Parameter Name="img" Type="String" />
<asp:Parameter Name="quantity" Type="Int32" />
<asp:Parameter Name="price" Type="Int64" />
<asp:Parameter Name="id" Type="Int64" />
</UpdateParameters>
</asp:SqlDataSource>
for code behind file
protected void Page_Load(object sender, EventArgs e)
{
string s2 = System.Web.HttpContext.Current.User.Identity.Name;
Response.Cookies["uname"].Value = s2;
}
finally the result i want is the total sum of cost of product display on a label control on the same page
Remove the SqlDataSource control. Don't put SQL in aspx page! Create a class that returns cart and calculates grand total. Databind in code behind.
I am thinking of that your cart.aspx page has a grid view for displaying the cart items and prices , then you can do like this....
you can do like this ... Simply in GridView.RowDataBound Event loop gridview and find control contain price amount and sum them
decimal grdTotal = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "price"));
grdTotal = grdTotal + rowTotal;
}
lbl.Text = grdTotal.ToString("c");
}
}
you can display total price value at the footer of price column .......

aspxgridview get MasterRowKeyValue from parent

I am trying to nest a grid within a grid.
To populate the most inner grid i need the KeyValue from both the Grids' direct parent, and the parent of the Grids' parent.
How can i retrieve the MasterRowKeyValue from the outter most parent?
I have 3 grids nested like so:
<dxwgv:ASPxGridView ID="gridParents" runat="server" ObjectDataSource="odsParents" KeyFieldName="ParentId">
<SettingsDetail ShowDetailRow="true" />
<Columns>
<dx:GridViewDataColumn FieldName="ParentIdId" Visible="false" />
<dx:GridViewDataColumn FieldName="Title" />
</Columns>
<Templates>
<DetailRow>
<dxwgv:ASPxGridView ID="gridParentsChilren" runat="server" ObjectDataSource="odsParentsChildren" KeyFieldName="ChildId" OnBeforePerformDataSelect="gridParentsChilren_DataSelect">
<SettingsDetail ShowDetailRow="true" IsDetailGrid="true" />
<Columns>
<dx:GridViewDataColumn FieldName="ChildId" Visible="false" />
<dx:GridViewDataColumn FieldName="Title" />
</Columns>
<Templates>
<dxwgv:ASPxGridView ID="gridParentsChilrenRoles" runat="server" ObjectDataSource="odsParentsChildrenRoles" KeyFieldName="RoleId" OnBeforePerformDataSelect="gridParentsChilrenRoles_DataSelect">
<SettingsDetail ShowDetailRow="true" IsDetailGrid="true" />
<Columns>
<dx:GridViewDataColumn FieldName="RoleId" Visible="false" />
<dx:GridViewDataColumn FieldName="Title" />
</Columns>
</dxwgv:ASPxGridView>
</Templates>
</dxwgv:ASPxGridView>
</DetailRow>
</Templates>
</dxwgv:ASPxGridView>
<asp:ObjectDataSource id="odsParents" runat="server" SelectMethod="GetParents" />
<asp:ObjectDataSource id="odsParentsChildren" runat="server" SelectMethod="GetParentsChildren">
<SelectParameters>
<asp:Parameter Name="parentid" Type="Object" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource id="odsParentsChildrenRoles" runat="server" SelectMethod="GetParentsRoles">
<SelectParameters>
<asp:Parameter Name="childid" Type="Object" />
<asp:Parameter Name="parentid" Type="Object" />
</SelectParameters>
</asp:ObjectDataSource>
When the OnBeforePerformDataSelect is called for the first Detail grid, i set the select parameters DefaultValue like this:
odsParentsChildren.SelectParameters["parentid"].DefaultValue = ((ASPxGridView)sender).GetMasterRowKeyValue().ToString();
For the most inner (the second) nested grid, i need to set one of my parameters to the MasterRowKeyValue of the most outter grid.
Something like:
odsParentsChildrenRoles.SelectParameters["childid"].DefaultValue = ((ASPxGridView)sender).GetMasterRowKeyValue().ToString();
odsParentsChildrenRoles.SelectParameters["parentid"].DefaultValue = ((ASPxGridView)sender).Master.GetMasterRowKeyValue().ToString();
How do I achieve this?
This can be done if you determine the master ASPxGridView instance for the processed detail. This can be done using the following code:
protected void ASPxGridView1_BeforePerformDataSelect(object sender, EventArgs e) {
ASPxGridView subDetail = sender as ASPxGridView;
GridViewDetailRowTemplateContainer container = subDetail.NamingContainer as GridViewDetailRowTemplateContainer;
ASPxGridView detail = container.Grid;
odsParentsChildrenRoles.SelectParameters["parentid"].DefaultValue = detail.GetMasterRowKeyValue();
}
Another possible version
protected void gvMyGridView_BeforePerformDataSelect(object sender, EventArgs e)
{
var gvMyGridView = sender as ASPxGridView;
var parentRow = gvMyGridView.NamingContainer as GridViewDetailRowTemplateContainer;
odsChildDataSource.SelectParameters["Id"].DefaultValue = parentRow .KeyValue.ToString();
}

Resources