943,150 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 1106
  • ASP.NET RSS
Mar 5th, 2010
0

DropDownList in GriedView receive paramater

Expand Post »
Hi everyone, I've got a DropDownList in Product.aspx when the add to cart button is pressed the value selected is passed onto a stored procedure. What I need help with is in ShoppingCart.aspx I want the selected value on the previous page to be the index on this pages DropDownList. But the DropDownList is in a GridView and therefore I do not know how to do this.

Any help will be appreciated.

ShoppingCart.aspx
ASP.NET Syntax (Toggle Plain Text)
  1. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  2.  
  3. <asp:Label ID="titleLabel" runat="server" Text="Your Shopping Cart" CssClass="ShoppingCartTitle" />
  4. <br />
  5. <asp:Label ID="statusLabel" CssClass="AdminPageText" ForeColor="Red" runat="server" /><br />
  6. <asp:GridView ID="grid" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" Width="100%" BorderWidth="0px" OnRowDeleting="grid_RowDeleting">
  7. <Columns>
  8. <asp:BoundField DataField="Name" HeaderText="Product Name" ReadOnly="True" SortExpression="Name" >
  9. <ControlStyle Width="100%" />
  10. </asp:BoundField>
  11. <asp:BoundField DataField="Price" DataFormatString="{0:c}" HeaderText="Price" ReadOnly="True"
  12. SortExpression="Price" />
  13.  
  14. <asp:TemplateField HeaderText="Color">
  15. <ItemTemplate>
  16. <asp:DropDownList ID="editColor" runat="server" DataSourceID="SqlDataSource1" DataTextField="Value" DataValueField="Value" /><asp:SqlDataSource
  17. ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BalloonShopConnectionString %>"
  18. SelectCommand="SELECT [Value] FROM [AttributeValue]"></asp:SqlDataSource>
  19. </ItemTemplate>
  20. </asp:TemplateField>
  21.  
  22. <asp:TemplateField HeaderText="Quantity">
  23. <ItemTemplate>
  24. <asp:TextBox ID="editQuantity" runat="server" CssClass="GridEditingRow" Width="24px" MaxLength="2" Text='<%#Eval("Quantity")%>' />
  25. </ItemTemplate>
  26. </asp:TemplateField>
  27.  
  28. <asp:BoundField DataField="Subtotal" DataFormatString="{0:c}" HeaderText="Subtotal"
  29. ReadOnly="True" SortExpression="Subtotal" />
  30. <asp:ButtonField ButtonType="Button" CommandName="Delete" Text="Delete" >
  31. <ControlStyle CssClass="SmallButtonText " />
  32. </asp:ButtonField>
  33.  
  34. </Columns>
  35. </asp:GridView>
  36. <table width="100%">
  37. <tr>
  38. <td>
  39. <span class="ProductDescription">
  40. Total amount:
  41. </span>
  42. <asp:Label ID="totalAmountLabel" runat="server" Text="Label" CssClass="ProductPrice" />
  43. </td>
  44. <td align="right">
  45. <asp:Button ID="updateButton" runat="server" Text="Update" CssClass="SmallButtonText" OnClick="updateButton_Click" />
  46. <asp:Button ID="loginButton" runat="server" CssClass="SmallButtonText" Text="Login" Width="80px" OnClick="loginButton_Click" />
  47. <asp:Button ID="registerButton" runat="server" CssClass="SmallButtonText" Text="Register" Width="80px" OnClick="registerButton_Click" />
  48. </td>
  49. </tr>
  50. </table>
  51. <br />
  52. <uc1:ProductRecommendations ID="recommendations" runat="server" />
  53. </asp:Content>

ShoppingCart.aspx.cs
ASP.NET Syntax (Toggle Plain Text)
  1. public partial class ShoppingCart : System.Web.UI.Page
  2. {
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5. // popluate the control only in the initial page load
  6. if (!IsPostBack)
  7. PopulateControls();
  8. }
  9.  
  10. // fill the shopping cart controls with data
  11. private void PopulateControls()
  12. {
  13. // display product recommendations
  14. recommendations.LoadCartRecommendations();
  15.  
  16. // get the items in the shopping cart
  17. DataTable dt = ShoppingCartAccess.GetItems();
  18. // if the shopping cart is empty...
  19. if (dt.Rows.Count == 0)
  20. {
  21. titleLabel.Text = "Your Shopping Cart is empty!";
  22. grid.Visible = false;
  23. updateButton.Enabled = false;
  24. //checkoutButton.Enabled = false;
  25. totalAmountLabel.Text = String.Format("{0:c}", 0);
  26. }
  27. else
  28. // if the shopping cart is not empty...
  29. {
  30. // populate the list with the shopping cart content
  31. grid.DataSource = dt;
  32. grid.DataBind();
  33. // set up controls
  34. titleLabel.Text = "These are the products in your Shopping Cart:";
  35. grid.Visible = true;
  36. updateButton.Enabled = true;
  37. //checkoutButton.Enabled = true;
  38. // display the total amount
  39. decimal amount = ShoppingCartAccess.GetTotalAmount();
  40. totalAmountLabel.Text = String.Format("{0:c}", amount);
  41. }
  42. }
  43.  
  44. // remove a product from the cart
  45. protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
  46. {
  47. // index of the row beeing deleted
  48. int rowIndex = e.RowIndex;
  49. // the ID of the product being deleted
  50. string productId = grid.DataKeys[rowIndex].Value.ToString();
  51. // remove the product from the shopping cart
  52. bool success = ShoppingCartAccess.RemoveItem(productId);
  53. // display the status
  54. statusLabel.Text = success ? "Product successfuly removed! " :
  55. "There was an error removing the product! ";
  56. // repopulate the control
  57. PopulateControls();
  58. }
  59.  
  60. // updat shopping cart product quantities
  61. protected void updateButton_Click(object sender, EventArgs e)
  62. {
  63. // number of rows in the grid view
  64. int rowsCount = grid.Rows.Count;
  65. // will store a row of the gridview
  66. GridViewRow gridRow;
  67. // will reference a quantity textBox in the GridView
  68. TextBox quantityTextBox;
  69. DropDownList editColor;
  70. // variable to store productId and quantity
  71. string productId;
  72. int quantity;
  73. // was the update successful?
  74. bool success = true;
  75.  
  76. // go through the rows of the gridview
  77. for (int i = 0; i < rowsCount; i++)
  78. {
  79. // get a row
  80. gridRow = grid.Rows[i];
  81. // the Id of the product being updated
  82. productId = grid.DataKeys[i].Value.ToString();
  83. // get the quantity textbox in the row
  84. quantityTextBox = (TextBox)gridRow.FindControl("editQuantity");
  85. editColor = (DropDownList)gridRow.FindControl("editColor");
  86. string attributes = editColor.SelectedValue;
  87. // get the quantity, gaurding against bogus values
  88. if (Int32.TryParse(quantityTextBox.Text, out quantity))
  89. {
  90. // update the product quantity
  91. success = success && ShoppingCartAccess.UpdateItem(productId, quantity, attributes);
  92. }
  93. else
  94. {
  95. // if TryParse did not succeed
  96. success = false;
  97. }
  98.  
  99. // display the status message
  100. statusLabel.Text = success ?
  101. "Your Shopping Cart was successfuly updated!" :
  102. "Some quantity updates failed! Please verify your cart!";
  103. }
  104. // repopulate the control
  105. PopulateControls();
  106. }
  107.  
  108. // redirect to login page
  109. protected void loginButton_Click(object sender, EventArgs e)
  110. {
  111. Response.Redirect("Checkout.aspx");
  112. }
  113.  
  114. // redirect to register page
  115. protected void registerButton_Click(object sender, EventArgs e)
  116. {
  117. Response.Redirect("Register.aspx");
  118. }
  119. }
Reputation Points: 6
Solved Threads: 0
Junior Poster
jellybeannn is offline Offline
108 posts
since Mar 2010
Mar 7th, 2010
0
Re: DropDownList in GriedView receive paramater
1. From product page, you can pass the selected value of the dropdown list through session or query string.
2. In shopping cart page, in the RowDataBound event of GridView, find the dropdownlist control from the GridView row and set selected value to the value received from session or query string.
Reputation Points: 165
Solved Threads: 113
Posting Pro
Ramesh S is offline Offline
580 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Cancel select validation controls
Next Thread in ASP.NET Forum Timeline: Problem Inserting into SQL database on server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC