none
2個GridView,master選取資料後,detail要顯示的Gridview無法顯示 RRS feed

  • 問題

  • 設定2個GridView,

    master顯示資料表所有資料,有"選取"功能  (SqlDataSource1)

    detail預設空值(SqlDataSource2,有設定Where條件),master有點選才顯示該筆資料

    點選資料後,detail要顯示的Gridview無法顯示

    執行結果:

    點選master其中一筆資料後,測試彈跳視窗都有出現

    但GridView2仍未產生

    CS檔如下:

    public partial class _001TEST_2Gridview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //GridView2.DataSourceID = "SqlDatasource2";
            Response.Write("<script language=javascript>alert('第一種彈出框ed');</" + "script>");
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridView2.DataSourceID = "SqlDatasource2";
            Response.Write("<script language=javascript>alert('第一種彈出框ing');</" + "script>");
        }
    }

    ASPX片段如下:

      <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
                <EmptyDataTemplate>
                    找不到!!!!!!!!!找不到!!!!!!!!!找不到!!!!!!!!!找不到!!!!!!!!!
                </EmptyDataTemplate>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString2 %>" SelectCommand="SELECT * FROM [test] WHERE ([id] = @id)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>


    2017年4月27日 上午 02:53

解答

所有回覆

  • 因為你 GridView2 的 AutoGenerateColums 為 false 

    2017年4月27日 上午 04:31
  • 你好,請問你的GridView1的DataKeyNames 是什麼?

    能貼上GridView1的code嗎?


    大家一齊探討、學習和研究,謝謝!
    MCSD, MCAD, MCSE+I, MCDBA, MCDST, MCSA, MCTS, MCITP, MCPD,
    MCT, Microsoft Community Star(TW & HK),
    Microsoft MVP for VB.NET since 2003
    My MSMVP Blog

    請記得將對您有幫助的回覆 標示為解答 以幫助其他尋找解答及參與社群討論的朋友們。
    Please remember to click Mark as Answer on the post that helps you. This can be beneficial to other community members reading the thread.

    2017年4月27日 上午 05:52
  • 感謝回覆, 真的解決了

    書上都沒有提這一點,不知是否是2017版的改變

    GridView1的dateKeyNames = id

    但GridView1的AutoGenerateColums 也是False為何能直接表現出SqlDataSource1的內容

    2017年4月27日 上午 05:57
  • <%@ Page Language="C#" AutoEventWireup="true" CodeFile="2Gridview.aspx.cs" Inherits="_001TEST_2Gridview" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>

    </SCRIPT>
        <form id="form1" runat="server">
        <div>
        
            2個GRID<br />
            Master<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" DataKeyNames="id" DataSourceID="SqlDataSource1" GridLines="Horizontal" PageSize="5" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
                <Columns>
                    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
                    <asp:BoundField DataField="test_time" HeaderText="test_time" SortExpression="test_time" />
                    <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
                    <asp:BoundField DataField="summary" HeaderText="summary" SortExpression="summary" />
                </Columns>
                <FooterStyle BackColor="White" ForeColor="#333333" />
                <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="White" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F7F7F7" />
                <SortedAscendingHeaderStyle BackColor="#487575" />
                <SortedDescendingCellStyle BackColor="#E5E5E5" />
                <SortedDescendingHeaderStyle BackColor="#275353" />
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" DeleteCommand="DELETE FROM [test] WHERE [id] = @original_id AND (([test_time] = @original_test_time) OR ([test_time] IS NULL AND @original_test_time IS NULL)) AND (([title] = @original_title) OR ([title] IS NULL AND @original_title IS NULL)) AND (([summary] = @original_summary) OR ([summary] IS NULL AND @original_summary IS NULL))" InsertCommand="INSERT INTO [test] ([test_time], [title], [summary]) VALUES (@test_time, @title, @summary)" SelectCommand="SELECT [id], [test_time], [title], [summary] FROM [test]" UpdateCommand="UPDATE [test] SET [test_time] = @test_time, [title] = @title, [summary] = @summary WHERE [id] = @original_id AND (([test_time] = @original_test_time) OR ([test_time] IS NULL AND @original_test_time IS NULL)) AND (([title] = @original_title) OR ([title] IS NULL AND @original_title IS NULL)) AND (([summary] = @original_summary) OR ([summary] IS NULL AND @original_summary IS NULL))" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}">
                <DeleteParameters>
                    <asp:Parameter Name="original_id" Type="Int32" />
                    <asp:Parameter Name="original_test_time" Type="DateTime" />
                    <asp:Parameter Name="original_title" Type="String" />
                    <asp:Parameter Name="original_summary" Type="String" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="test_time" Type="DateTime" />
                    <asp:Parameter Name="title" Type="String" />
                    <asp:Parameter Name="summary" Type="String" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="test_time" Type="DateTime" />
                    <asp:Parameter Name="title" Type="String" />
                    <asp:Parameter Name="summary" Type="String" />
                    <asp:Parameter Name="original_id" Type="Int32" />
                    <asp:Parameter Name="original_test_time" Type="DateTime" />
                    <asp:Parameter Name="original_title" Type="String" />
                    <asp:Parameter Name="original_summary" Type="String" />
                </UpdateParameters>
            </asp:SqlDataSource>
            <br />
            Detail<br />
            <asp:GridView ID="GridView2" runat="server">
                <EmptyDataTemplate>
                    找不到!!!!!!!!!找不到!!!!!!!!!找不到!!!!!!!!!找不到!!!!!!!!!
                </EmptyDataTemplate>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString2 %>" SelectCommand="SELECT * FROM [test] WHERE ([id] = @id)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>
        
        </div>
        </form>
    </body>
    </html>

    2017年4月27日 上午 06:01