I'm having a real hard time coming up with a solution to this problem. I created a custom gridview control from Dino Esposito's "Extending Gridview" article which autogenerates a checkbox column that allows for multiple record selection. Once a user checks a box, the entire row gets selected. I added a dropdown list on the top of the page that has only two options, "Yes" or "No". What I'm trying to do is update a boolean column called "contract" (I'm using the Pubs sample database) for all selected rows (via checkboxes checked) depending on whether the user selects "Yes" or "No" from the drop down menu.
For example,
1) the user selects "No" in the dropdown
2) The user checks all rows in the checkbox column for which he wants the all the values"contract" field set to "No"
3) The user then clicks on a button called "Submit" and all selected records get updated to "No" under the "contracts" column.
The idea is to allow the user to change the boolean values from a field for multiple records. Hence, making individual cells editable is pointless. Anybody have an idea how to go about this?
Yes, do you have code you can post? Post it and I'll help.
|||
Ok I included the aspx and the aspx.cs code. I'm pretty new with asp.net so the code might not be all that great. Pay no heed to the MenuItems. The Post Back Button is what is supposed to trigger the updates after the selections are made. I appreciate your help.
<%@. Page Language="C#" AutoEventWireup="true" CodeFile="TestSSLServer.aspx.cs" Inherits="TestSSLServer" %><%@. Register Namespace="MsdnMag.CuttingEdge.Controls" TagPrefix="x" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head id="Head1" runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <asp:Menu ID="Menu1" runat="server" BackColor="#E0E0E0" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="8pt" ForeColor="#284E98" Orientation="Horizontal" StaticSubMenuIndent="10px" OnMenuItemClick="Menu1_MenuItemClick"> <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> <DynamicHoverStyle BackColor="#284E98" ForeColor="White" /> <DynamicMenuStyle BackColor="#B5C7DE" /> <StaticSelectedStyle BackColor="#507CD1" /> <DynamicSelectedStyle BackColor="#507CD1" /> <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> <Items> <asp:MenuItem Text="Personal notes" Value="Notes"></asp:MenuItem> <asp:MenuItem Text="Orders" Value="Orders"></asp:MenuItem> </Items> <StaticHoverStyle BackColor="#284E98" ForeColor="White" /> </asp:Menu> <hr /> <asp:DropDownList ID="DropDownList1" runat="server" Width="133px"> <asp:ListItem Value="True">Yes</asp:ListItem> <asp:ListItem Value="False">No</asp:ListItem> </asp:DropDownList><br /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PUBSConnectionString%>" SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract] FROM [authors]" /> <x:GridView runat="server" ID="GridView1" AutoGenerateCheckBoxColumn="True" DataSourceID="SqlDataSource1" AllowPaging="True" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" onrowupdating="GridView1_RowUpdating" Font-Names="Verdana" Font-Size="8pt" GridLines="Horizontal" AutoGenerateColumns="False" DataKeyNames="au_id"> <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" /> <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" /> <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" /> <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" /> <AlternatingRowStyle BackColor="#F7F7F7" /> <Columns> <asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" /> <asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" /> <asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" /> <asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" /> <asp:BoundField DataField="address" HeaderText="address" SortExpression="address" /> <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" /> <asp:BoundField DataField="state" HeaderText="state" SortExpression="state" /> <asp:BoundField DataField="zip" HeaderText="zip" SortExpression="zip" /> <asp:TemplateField HeaderText="contract" SortExpression="contract"> <itemtemplate> <asp:Label ID="Label1" runat="server" Text='<%# changeTrueFalseToYesNo(Eval("contract"))%>'></asp:Label> </itemtemplate> </asp:TemplateField> </Columns> </x:GridView> <asp:Button runat="server" ID="Button1" Text="Just post back" OnClick="Button1_Click" /> <hr /> <asp:Label ID="Results" runat="server" Text=""></asp:Label> </div> </form></body></html>
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient;public partialclass TestSSLServer : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) { }// end Page_Load // The following method displays the string depending on whether the user // clicked on Notes or Orders. There is commented just adds the 0-based // index of the row the the txt string. This is just to check if the indexes // are correct when needed.protected void Menu1_MenuItemClick(object sender, MenuEventArgs e) {string txt ="";if (e.Item.Value =="Notes") txt ="Show personal notes for the following indices:<br>";else txt ="Show orders for the following indices:<br>";foreach (int indexin GridView1.GetSelectedIndices()) {string key = (string)GridView1.DataKeys[index].Value; txt += key +", ";// txt += index + ", "; } Results.Text = txt; }// The following method displays the record when the user // enters the submit button.protected void Button1_Click(object sender, EventArgs e) {// Get the GridViewRow object that represents the row being edited // from the Rows collection of the GridView control.foreach (int indexin GridView1.GetSelectedIndices()) { GridViewRow row = GridView1.Rows[index]; Label contractLabel = (Label)row.FindControl("contract");if (contractLabel.Text.Equals("Yes")) { e.NewValues["contract"] ="No"; }// end else ifelse if (contractLabel.Text.Equals("No")) { e.NewValues["contract"] ="Yes"; }// end else if }// end foreach }// end Button1_Clickprotected string changeTrueFalseToYesNo(object o) {string myString;if (Convert.ToBoolean(o) ==true) { myString ="Yes"; }// end ifelse { myString ="No"; }// end elsereturn myString; }// end changeTrueFalseToYesNo}// end of class
"MsdnMag.CuttingEdge.Controls" TagPrefix="x" %>
|||
I edited the code so that it is more readable. It's easier to see what I'm trying to do. Omit the Gridview1_updating in the markup. Sorry, about the messy code.