如何以及在何处输入代码以仅编辑数据网格视图的一列

时间:2011-01-14 08:33:15

标签: c# asp.net webforms

alt text

好的,所以我已经创建了datagridview并启用了对列的编辑,如下图所示。接下来,我想为编辑链接编写一个单击事件,以便在单击它时,只有investigatorID列可用于编辑。在那个可以进行更改的情况下,我希望调查员列显示一个可以选择的调查员下拉列表,并将所选项目绑定到单击编辑链接的行。

我有数据源显示调查器ID只是想知道在哪里编写点击代码甚至是编辑链接并让它发挥作用。

让一些魔术师工作.. :) 然后我又想了一个更好的想法,因为我只需要将调查员ID传递到数据库进行更新,我编辑了如下设计(见下图) 所以我没有在网格视图中显示展览类型和图像,而是只调用将要分配InvestigatorID的展示ID,当你查看下面的数据库时,它更有意义:

虽然没有完全填充,但数据库背后的概念是:1。一个案例可能与一个以上的展览有关(所以说一个案例有很多展品,每个展品都有一个调查人员,一次不能在3个以上的展品上工作。

因此,在此阶段,调查员被分配到案例时,caseID和Exhibit详细信息已经填充。打开案例的管理员已经拥有了他们的ID,而尚未分配案例的管理员将其ID和调查员ID分配给字段。

看看我的第二个设计,我已经能够创建一个网格,只显示正在处理2个或更少展品的调查员。这意味着它们可以在至少一个展览中分配。为了进行分配,我从下拉列表中选择一个CaseID,详细说明案例表中的所有案例(此处未显示),并根据所选案例,显示链接到所选casedID的所有ExhibID。从那里,用户可以从下拉列表中选择一个图表,然后选择要分配的任何可用调查员。所以我现在需要做的就是当用户点击分配按钮时,我想知道在哪里以及如何使点击分配按钮的investigatorsID作为更新和gridvire(userID,用户名aka调查员列表),以便最近点击的InvestigatorID达到三个展品的门槛,他们的调查员ID和名称不会显示。同时,已经分配的展览也不会再显示在下拉列表中。因此,实际上,单击了“分配”按钮,并将该旁边的ID发送到展览表进行更新,并且刷新了gridview以及展览下拉列表。

网格视图不应该有问题,因为它的数据直接绑定,因此一个简单的exhibitgridview.databind()应该可以解决它,并且可能会调用先前填充了Exhibdropdownlist的方法,也可以为下拉列表进行刷新。

所以现在的问题是如何获取分配按钮后面的代码,以便进行此事件。请在下面的代码中找到主页的代码(使用嵌套的母版页)和后面的代码以及当前页面应该发生的.cs文件。

第一个母版页(SITE.MASTER)代码

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Prototype5.SiteMaster" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script type="text/javascript">
//            $(function () {
//                $("#showOpenCasePage").click(function () {
//                    $("#CaseMainPanel").load("/EntryForms/OpenCase.aspx");
//                });
//                $("#showAddExhibitPage").click(function () {
//                    $("#CaseMainPanel").load("/EntryForms/AddExhibit.aspx");
//                });
//                $("#showOpenCasePage").click(function () {
//                    $("#CaseManagementMainPanel").load("/EntryForms/OpenCase.aspx");
//                });
//                $("#showAddExhibitPage").click(function () {
//                    $("#CaseManagementMainPanel").load("/EntryForms/AddExhibit.aspx");
//                });
//                $("#showAllocateOfficerPage").click(function () {
//                    $("#CaseManagementMainPanel").load("/EntryForms/AllocateOfficer.aspx");
//                });

//                $("#showReallocateOfficerPage").click(function () {
//                    $("#CaseManagementMainPanel").load("/EntryForms/ReallocateOfficer.aspx");
//                });

//                $("#showPrioritizeCasePage").click(function () {
//                    $("#CaseManagementMainPanel").load("/EntryForms/CasePriority.aspx");
//                });
//                $("#showRecordFindingPage").click(function () {
//                    $("#InvestigatorMainPanel").load("/EntryForms/RecordFinding.aspx");
//                });
//                $("#showUpdateStatusPage").click(function () {
//                    $("#InvestigatorMainPanel").load("/EntryForms/UpdateStatus.aspx");
//                });
//            });


        </script>

    <title></title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
    <script type="text/javascript">


        function updateClock() {
            var currentTime = new Date();

            var currentHours = currentTime.getHours();
            var currentMinutes = currentTime.getMinutes();
            var currentSeconds = currentTime.getSeconds();

            // Pad the minutes and seconds with leading zeros, if required
            currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
            currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;

            // Choose either "AM" or "PM" as appropriate
            var timeOfDay = (currentHours < 12) ? "AM" : "PM";

            // Convert the hours component to 12-hour format if needed
            currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;

            // Convert an hours component of "0" to "12"
            currentHours = (currentHours == 0) ? 12 : currentHours;

            // Compose the string for display
            var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

            // Update the time display
            document.getElementById("clock").firstChild.nodeValue = currentTimeString;
        }


</script>
        <style type="text/css">
            .style1
            {
                width: 213px;
            }
            .menu
            {}
            .style2
            {
                width: 100%;
            }
            .style3
            {
                width: 264px;
            }
            .style4
            {
                width: 291px;
            }
        </style>
</head>
<body onload="updateClock(); setInterval('updateClock()', 1000 )">
    <form id="Form1" runat="server">
    <div class="page">
        <div class="header">
            <div class="title">
                <span style="font-size: 5em; font-family:Old English Text MT; color: #FF6600;"><strong>Case Management System</strong></span>
            </div>
            <div class="clock">
                <span id="clock">&nbsp;</span>
            </div>
            <div class="clear hideSkiplink">
                <table class="style2">
                    <tr>
                        <td class="style4" align="center">
                    <asp:LoginView ID="MasterLoginView" runat="server">
                        <LoggedInTemplate>
                            Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /> 

                        [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/Logout.aspx"/> ]
                            <%--Welcome: 
                            <span class="bold"><asp:LoginName ID="MasterLoginName" runat="server" /> </span>!--%>                       
                        </LoggedInTemplate>
                        <AnonymousTemplate>
                            Welcome: Guest
                            [ <a href="Account/Login.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ]
                        </AnonymousTemplate>

                    </asp:LoginView>
                        </td>
                        <td class="style3">
                            &nbsp;</td>
                        <td valign="top">
                <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="False" 
                                IncludeStyleBlock="False" Orientation="Horizontal" Height="36px" Width="120px">
                    <Items>
                        <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home" 
                            ImageUrl="~/Images/homeIcon.png"/>
                        <asp:MenuItem NavigateUrl="~/About.aspx" Text="About" 
                            ImageUrl="~/Images/aboutIcon.png"/>
                        <asp:MenuItem ImageUrl="~/Images/contactUsIcon.png" NavigateUrl="~/ContactUs.aspx" 
                            Text="Contact Us" Value="Contact Us"></asp:MenuItem>
                    </Items>
                </asp:Menu>
                        </td>
                    </tr>
                </table>
            </div>
                </div>

                </div>

                <div class="page" style="margin-top:5px;height:auto;">


                            <div style="border-style:solid;">
                                <table style="width:100%; background-color:#3a4f63">
                                    <tr>
                                        <td class="style1" valign="top">
                                                   <p style="padding-left: 4px; padding-right:4px;">
                                            <asp:Button ID="functionButton" runat="server" Text="System Functions" 
                                                    class="fnctButton" Height="41px" Width="192px" />

                                            <asp:ContentPlaceHolder ID="LeftNavigation" runat="server">            


                                            </asp:ContentPlaceHolder>
                                            </p>
                                        </td>

                                        <td rowspan="2" valign="top" align="center">
                                                <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
                                        </td>
                                    </tr>

                                        <tr>
                                            <td class="style1" align="left" valign="top">

                                                    <asp:ContentPlaceHolder ID="RightNewsItem" runat="server"/>           


                                            </td>
                                        </tr>
                            </table>                 

                            </div>

                </div>



        <div class="clear">
        </div>

        <div class="footer">

            <span style="font-size: small;color: #FFFFFF;"><strong>Copyright 2011 JustRite Software Inc.</strong></span></div>
    </form>
</body>
</html>

第二个Master页面基于第一个(Manage.Master)

   <%@ Master Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Manager.master.cs" Inherits="Prototype5.Manager" %>
<asp:Content ID="ManagerHead" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="ManagerLeft" ContentPlaceHolderID="LeftNavigation" runat="server">

    <div style="margin-top:20px; margin-bottom:20px;">    
            <p class="actionButton">
                <asp:Button ID="allocateInvestigatorsButton" runat="server" 
                    Text="Allocate Investigators" height="30px" width="135px" 
                    onclick="allocateInvestigatorsButton_Click" />
                 </p>
            <p class="actionButton">
                <asp:Button ID="realocateInvestigatorsButton" runat="server" 
                    Text="Reallocate Investigators" Height="30px" Width="135px" 
                    onclick="realocateInvestigatorsButton_Click" />
                 </p>
            <p class="actionButton">
                <asp:Button ID="prioritizeCaseButton" runat="server" 
                    Text="Prioritize Case" height="30px" width="135px" 
                    onclick="prioritizeCaseButton_Click" />
                 </p>
            <p class="actionButton">
                <asp:Button ID="openCaseButton" runat="server" 
                    Text="Open Case" height="30px" width="135px" 
                    onclick="openCaseButton_Click" />
            </p>
            <p class="actionButton">
                <asp:Button ID="registerExhibitButton" runat="server" 
                    Text="Register Exhibit" height="30px" width="135px" 
                    onclick="registerExhibitButton_Click" />
                </p>
    </div>
</asp:Content>


<asp:Content ID="ManagerMain" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</asp:Content>
<asp:Content ID="ManagerRight" ContentPlaceHolderID="RightNewsItem" runat="server">
    <asp:Button ID="AlertButton" runat="server" Text="Alerts" 
                            class="fnctButton" Height="25px" Width="70px" />

    <div style="border-style: none; border-color: inherit; border-width: medium; width:210px; height: 103px;"> 
                    <p style="text-align: justify; font-size:1.2em; color:White; width: 209px; height: 95px;">
                        This is a place holder for allerts about cases to which the investigator has been assigned to.
                    </p>
                    </div>
</asp:Content>

当前页面包含基于第二主页的基于第二主页的元素

    <%@ Page Title="" Language="C#" MasterPageFile="~/Manager.master" AutoEventWireup="true" CodeBehind="AllocateInvestigators.aspx.cs" Inherits="Prototype5.AllocateInvestigators" %>
<asp:Content ID="AllocateInvestigatorsMain" ContentPlaceHolderID="MainContent" runat="server">

    <div>
    <table class="style2">
        <tr>
            <td colspan="4" style="background-color: #C0C0C0">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style1" 
                style="width: 79px; color: #000000; background-color: #666666;">
                <strong>Case ID:</strong></td>
            <td  align="left" class="style1" style="width: 187px">
                <asp:DropDownList ID="caseIDDropDownList" runat="server" Height="22px" 
                    Width="116px" DataSourceID="distinctCaseIDSqlDataSource" 
                    DataTextField="CaseID" DataValueField="CaseID" 
                    onselectedindexchanged="caseIDDropDownList_SelectedIndexChanged">
                </asp:DropDownList>
                <asp:SqlDataSource ID="distinctCaseIDSqlDataSource" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:CMSSQL3ConnectionString1 %>" SelectCommand="SELECT DISTINCT CaseID FROM Exhibits
    WHERE InvestigatorID = NULL"></asp:SqlDataSource>
            </td>
            <td class="style3" 
                style="width: 119px; color: #000000; background-color: #666666;">
                <strong>Case Priority:</strong></td>
            <td align="left">
                <asp:DropDownList ID="casePriorityDropDownList" runat="server" height="22px" 
                    width="116px">
                    <asp:ListItem>Low</asp:ListItem>
                    <asp:ListItem>Medium</asp:ListItem>
                    <asp:ListItem>High</asp:ListItem>
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td colspan="4" style="background-color: #C0C0C0">
                &nbsp;</td>
        </tr>
    </table>
</div>
<div>

    <table class="style2">
        <tr>
            <td style="width: 79px; color: #000000;">
                <strong>Exhibit ID:</strong></td>
            <td align="left">
                <asp:DropDownList ID="exhibitsDropDownList" runat="server" height="22px" 
                    onselectedindexchanged="exhibitsDropDownList_SelectedIndexChanged" 
                    width="116px">
                </asp:DropDownList>
                <asp:SqlDataSource ID="exhibitIDSqlDataSource" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:CMSSQL3ConnectionString1 %>" 
                    SelectCommand="SELECT ExhibitID FROM Exhibits"></asp:SqlDataSource>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;</td>
        </tr>
    </table>

</div>
<div>
    <table class="style2">
        <tr>
            <td>
                <asp:GridView ID="exhibitGridView" runat="server" AutoGenerateColumns="False" 
                    CellPadding="4" DataKeyNames="UserID" DataSourceID="SqlDataSource1" 
                    EmptyDataText="There are no data records to display." ForeColor="#333333" 
                    GridLines="None" Height="241px" 
                    onselectedindexchanged="GridView1_SelectedIndexChanged" Width="323px" 
                    style="margin-right: 0px">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True" 
                            SortExpression="UserID" />
                        <asp:BoundField DataField="UserName" HeaderText="UserName" 
                            SortExpression="UserName" />
                        <asp:ButtonField ButtonType="Button" CommandName="Select" Text="Assign" />
                    </Columns>
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <SortedAscendingCellStyle BackColor="#FDF5AC" />
                    <SortedAscendingHeaderStyle BackColor="#4D0000" />
                    <SortedDescendingCellStyle BackColor="#FCF6C0" />
                    <SortedDescendingHeaderStyle BackColor="#820000" />
                </asp:GridView>
                <asp:SqlDataSource ID="assignInvestigatorsSqlDataSource" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:CMSSQL3ConnectionString1 %>" 
                    SelectCommand="SELECT ExhibitID, ExhibitType, ExhibitImage, InvestigatorID FROM Exhibits">
                </asp:SqlDataSource>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:CMSSQL3ConnectionString1 %>" 
                    DeleteCommand="DELETE FROM [Exhibits] WHERE [ExhibitID] = @ExhibitID AND [CaseID] = @CaseID" 
                    InsertCommand="INSERT INTO [Exhibits] ([CaseID], [ExhibitType], [DateReceived], [StoredLocation], [OfficerID], [SuspectID], [InvestigatorID], [ManagerID], [AdminID]) VALUES (@CaseID, @ExhibitType, @DateReceived, @StoredLocation, @OfficerID, @SuspectID, @InvestigatorID, @ManagerID, @AdminID)" 
                    ProviderName="<%$ ConnectionStrings:CMSSQL3ConnectionString1.ProviderName %>" 
                    SelectCommand="SELECT UserID, UserName
FROM Users
WHERE UserID IN (SELECT UserID
        FROM Users
        WHERE UserType LIKE 'Investigator'
        AND UserID NOT IN (SELECT InvestigatorID 
                From Exhibits 
                GROUP BY (InvestigatorID) 
                HAVING COUNT(InvestigatorID) &gt; 2));" 

                    UpdateCommand="UPDATE [Exhibits] SET [ExhibitType] = @ExhibitType, [DateReceived] = @DateReceived, [StoredLocation] = @StoredLocation, [OfficerID] = @OfficerID, [SuspectID] = @SuspectID, [InvestigatorID] = @InvestigatorID, [ManagerID] = @ManagerID, [AdminID] = @AdminID WHERE [ExhibitID] = @ExhibitID AND [CaseID] = @CaseID">
                    <DeleteParameters>
                        <asp:Parameter Name="ExhibitID" Type="Int32" />
                        <asp:Parameter Name="CaseID" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="CaseID" Type="Int32" />
                        <asp:Parameter Name="ExhibitType" Type="String" />
                        <asp:Parameter Name="DateReceived" Type="DateTime" />
                        <asp:Parameter Name="StoredLocation" Type="String" />
                        <asp:Parameter Name="OfficerID" Type="String" />
                        <asp:Parameter Name="SuspectID" Type="Int32" />
                        <asp:Parameter Name="InvestigatorID" Type="String" />
                        <asp:Parameter Name="ManagerID" Type="String" />
                        <asp:Parameter Name="AdminID" Type="String" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="ExhibitType" Type="String" />
                        <asp:Parameter Name="DateReceived" Type="DateTime" />
                        <asp:Parameter Name="StoredLocation" Type="String" />
                        <asp:Parameter Name="OfficerID" Type="String" />
                        <asp:Parameter Name="SuspectID" Type="Int32" />
                        <asp:Parameter Name="InvestigatorID" Type="String" />
                        <asp:Parameter Name="ManagerID" Type="String" />
                        <asp:Parameter Name="AdminID" Type="String" />
                        <asp:Parameter Name="ExhibitID" Type="Int32" />
                        <asp:Parameter Name="CaseID" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>
            </td>
        </tr>
    </table>
</div>
<div>
    <table class="style2">
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
</div>
</asp:Content>

最后是当前页面的.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace Prototype5
{
    public partial class AllocateInvestigators : System.Web.UI.Page
    {
        SqlConnection caseConnection = new SqlConnection();
        SqlConnection exhibitConnection = new SqlConnection();
        DataSet caseDataSet = new DataSet();
        DataSet exhibitDataSet = new DataSet();
        SqlDataAdapter caseSqlDataAdapter = new SqlDataAdapter();
        SqlDataAdapter exhibitSqlDataAdapter = new SqlDataAdapter();
        protected void Page_Load(object sender, EventArgs e)
        {
            //exhibitGridView.Enabled = false;
        }

        protected void caseIDDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            exhibitGridView.Enabled = true;

            string selectedCaseID = caseIDDropDownList.SelectedItem.Text.ToString();
            CreateExhibitDataset();
            DataView exhibitDataView = new DataView(exhibitDataSet.Tables[0]);
            exhibitDataView.RowFilter = "CaseID = '" + selectedCaseID + "' ";
           exhibitsDropDownList.DataSource = exhibitDataView;
           exhibitsDropDownList.DataBind();
        }
        private void CreateDataSet()
        {
            caseConnection.ConnectionString =
           distinctCaseIDSqlDataSource.ConnectionString;
            caseSqlDataAdapter.SelectCommand = new
            SqlCommand(distinctCaseIDSqlDataSource.SelectCommand, caseConnection);
            caseSqlDataAdapter.Fill(caseDataSet);
        }
        private void CreateExhibitDataset()
        {
            exhibitConnection.ConnectionString =
           exhibitIDSqlDataSource.ConnectionString;
            exhibitSqlDataAdapter.SelectCommand = new
            SqlCommand(exhibitIDSqlDataSource.SelectCommand, caseConnection);
            exhibitSqlDataAdapter.Fill(exhibitDataSet);
        }



    }
}

依靠你的帮助... @ _ @ ...

alt text alt text

1 个答案:

答案 0 :(得分:1)

这是你在找什么?

alt text

我使用了示例NorthWind DB,因此您可能必须将DataSource添加到GridView。 这是标记

<asp:GridView ID='GridView1' runat="server" AutoGenerateColumns='False'
        DataSourceID='ObjectDataSource1' DataKeyNames='ProductID' 
        AllowPaging='True'>
        <Columns>
            <asp:BoundField HeaderText='Name' DataField='ProductName' ReadOnly='true'/>
            <asp:TemplateField HeaderText='Order'>
                <EditItemTemplate>
                    <asp:DropDownList ID='DropDownList1' runat='server' DataTextField='ProductName' DataValueField='ProductID'>
                        <asp:ListItem Enabled='true' Selected='True' Value='2'>

                        </asp:ListItem>
                        <asp:ListItem Value='1'></asp:ListItem>
                        <asp:ListItem Value='3'></asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
               <ItemTemplate>
                    <asp:Label ID='Label1' runat="server" Text='<%# Eval("UnitsOnOrder") %>'></asp:Label>
               </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton='true'  ButtonType='Link' EditText='Edit' UpdateText='Update' CancelText='Cancel' />
        </Columns>
</asp:GridView>

此外,您还必须设置DropDownList的数据源。在上面的标记中,我对ListItem进行了硬编码。


更新 - 在GridView上处理OnRowUpdating事件

点击更新链接后,GridView会触发OnRowUpdating事件...... 在事件处理程序中,您可以获取控件的值(在该行中)并对其进行操作,例如更新数据。 以下是代码段...(您必须将OnRowUpdating='HandleOnGridViewRowUpdating'添加到gridview的标记中)

 protected void HandleOnGridViewRowUpdating(Object sender, GridViewUpdateEventArgs e)
    {
        DropDownList dropDownList = 
            GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList;
        //got the selected value of dropdown..
        //get other required values from the GridView1...
        //the DataKeyNames is set to ProductID.. so e.Keys["ProductID"] should give you the   //ProductID..
        //write a sql that takes these values and updates the db...
    }