两个gridview selectedindexchanged事件不起作用

时间:2014-04-09 09:56:06

标签: c# asp.net gridview

我在一个页面中设置了两个Gridview。一个人选择的索引改变了事件加载另一个事件。但是,第二个网格的selectedindexchanged事件继续进行首次选择的索引更改事件。我能做什么?我的功能名称是正确的。没有任何其他冲突。

这是我的asp代码:

<asp:GridView ID="gdv" runat="server" 
                AutoGenerateColumns="False" onrowdatabound="gdv_RowDataBound" 
                onselectedindexchanged="gdv_SelectedIndexChanged" AllowPaging="True" 
                Width="100%" onpageindexchanging="gdv_PageIndexChanging">
                <Columns>
                    <asp:BoundField DataField="Sl" HeaderStyle-HorizontalAlign="Center" 
                        HeaderText="SL #" ItemStyle-HorizontalAlign="Center">
                    <HeaderStyle HorizontalAlign="Center" />
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="SalesOrderNo" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Order No" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="SectionID" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Section Id" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="SectionName" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Section Name" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="OutletID" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Outlet Id" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="OutletName" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="outlet Name" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="SalesPersonID" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Sales Person Name" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="OrderDate" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Order Date" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="OrderValue" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Order Value" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                   <asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:CheckBox ID="checkOrderList" runat="server" ValidationGroup="ValCheckOrder" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
                       <ItemTemplate>
                    <%--<asp:LinkButton ID="btnView" Text="View" runat="server" CommandName="ViewIndividualOrder" />--%>
                     <asp:Button ID="btnViewOrder" runat="server" Text="View" ValidationGroup="ValCheckOrder"
                        CommandName="ViewIndividualOrder" OnClick="btnViewOrder_Click" />
                </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
                <PagerStyle HorizontalAlign="Center" />
            </asp:GridView>

<asp:GridView ID="gdvOrderDetail" runat="server" 
                AutoGenerateColumns="False" onrowdatabound="gdvOrderDetail_RowDataBound" 
                onselectedindexchanged="gdvOrderDetail_SelectedIndexChanged" AllowPaging="True" 
                Width="100%" onpageindexchanging="gdvOrderDetail_PageIndexChanging">
                <Columns>
                    <asp:BoundField DataField="Sl" HeaderStyle-HorizontalAlign="Center" 
                        HeaderText="SL #" ItemStyle-HorizontalAlign="Center">
                    <HeaderStyle HorizontalAlign="Center" />
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField DataField="ProductID" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Product ID" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="ProductName" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Product Name" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="UnitID" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Unit" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="PacketSize" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Packet Size" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
               <asp:TemplateField HeaderText="Sales Rate" ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                     <asp:Label ID="lblSalesRate" runat="server" Text='<%# Eval("SalesRate") %>' Font-Size="Smaller" />
                </ItemTemplate>
            </asp:TemplateField>
                   <%-- <asp:BoundField DataField="SalesRate" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Sales Rate" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>--%>
                    <asp:BoundField DataField="OrderQty" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Order Qty" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
              <asp:TemplateField HeaderText="MemoQty" ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:TextBox ID="txtMemoQty" runat="server" Font-Bold="True" Width="100px" OnTextChanged="txtMemoQty_TextChanged"
                        AutoPostBack="true" Enabled="true" onkeypress="return onlyNumbers();"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidatorMemoQTY" ValidationGroup="ValCheckOrderDetail"
                        runat="server" ControlToValidate="txtMemoQty" ErrorMessage="*" Font-Size="X-Large"
                        ForeColor="Red"></asp:RequiredFieldValidator>
                </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Amount" ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                     <asp:Label ID="lblAmount" runat="server" Text='<%# Eval("Amount") %>' Font-Size="Smaller" />
                </ItemTemplate>
            </asp:TemplateField>
                    <%--<asp:BoundField DataField="Amount" HeaderStyle-HorizontalAlign="Left" 
                        HeaderText="Amount" ItemStyle-HorizontalAlign="Left">
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>--%>
                   <asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:CheckBox ID="checkOrderList" runat="server" ValidationGroup="ValCheckOrderDetail" />
                        </ItemTemplate>
                    </asp:TemplateField>


                </Columns>
                <PagerStyle HorizontalAlign="Center" />
            </asp:GridView>

C#代码:

protected void gdv_SelectedIndexChanged(object sender, EventArgs e)
{
    int indx = 0;
    indx = gdv.SelectedIndex;

    string salesorderno = "";
    string sectionid = "";
    string sectionname = "";
    string outletid = "";
    string outletname = "";
    string salespersonname = "";
    string orderdate = "";
    string ordervalue = "";
    string ordercheck = "";

    for (int i = 0; i < 1; i++)
    {
        salesorderno = gdv.Rows[indx].Cells[1].Text.ToString();
        sectionid = gdv.Rows[indx].Cells[2].Text.ToString();
        sectionname = gdv.Rows[indx].Cells[3].Text.ToString();
        outletid = gdv.Rows[indx].Cells[4].Text.ToString();
        outletname = gdv.Rows[indx].Cells[5].Text.ToString();
        salespersonname = gdv.Rows[indx].Cells[6].Text.ToString();
        orderdate = gdv.Rows[indx].Cells[7].Text.ToString();
        ordervalue = gdv.Rows[indx].Cells[8].Text.ToString();
        ordercheck = gdv.Rows[indx].Cells[9].Text.ToString();
    }

    lblOrderNo.Text = salesorderno;
    lblSectionName.Text = sectionname;
    lblOutletName.Text = outletname;
    lblOrderDate.Text = orderdate;

    LoadOrderReportDetailGrid(salesorderno);
}

protected void gdvOrderDetail_SelectedIndexChanged(object sender, EventArgs e)
{

}

2 个答案:

答案 0 :(得分:0)

测试足以知道事件的起源。 例如:

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(dataGrid1.SelectedItem!=null)
                MessageBox.Show("aaaaaaaaaaa");
        }

        private void dataGrid2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dataGrid2.SelectedItem != null)
                MessageBox.Show("BBBBBbbBBBB    ");
        }

答案 1 :(得分:0)

只需打开gridviews的事件窗口,然后选择其中一个,然后在事件窗口中双击“selected index changed”事件。它会将你带到代码部分。如果您处于真实过程中,则事件绑定为真。如果不是,你必须改变你的程序的定义。

这是一个简单的过程。

  1. 复制程序中的代码并将其粘贴到安全的地方。
  2. 删除与gridviews相关的程序
  3. 检查网格视图的事件页面,以确保“选定的索引已更改”事件中没有任何内容。
  4. 双击第一个gridviews“selected index changed”事件。它会创建一个空白程序。在此处粘贴相关代码。
  5. 做第二次。
  6. 另一件事是在属性窗口中检查gridviews的验证组。它们应该是不同的:)

    不:请原谅我不能直接编写代码。因为我不在自己的电脑上我希望它有效。