10

私のグリッドビューはこのようなものですが、ビューボタンを選択して、選択したインデックスの主キー値列が変更されていることを確認すると、エラーが発生します。問題を解決するのを手伝ってください。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns >
                <asp:TemplateField >
                    <ItemTemplate >
                        <asp:Button ID="btnViewComments" Text ="View Comments" runat ="server" CommandName ="select" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField ="forumId" Visible ="false" />
                <%--<asp:CommandField ButtonType ="Button" ShowSelectButton ="true" SelectText ="View Comments"/>--%>
                <asp:TemplateField HeaderText ="Question">
                    <ItemTemplate >
                        <asp:TextBox ID ="txtQuestion" Text ='<%#Eval("question")%>' runat ="server" TextMode ="MultiLine" Height="100" Width ="350"></asp:TextBox>
                       <%-- <%#Eval("question")%>--%>
                    </ItemTemplate>
                    <%--<EditItemTemplate >
                        <asp:TextBox ID ="txtQuestion" Text ='<%#Eval("question")%>' runat ="server" TextMode ="MultiLine" ></asp:TextBox>
                    </EditItemTemplate>--%>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Poster Name">
                    <ItemTemplate >
                        <%#Eval("posterName") %>
                    </ItemTemplate>
                    <EditItemTemplate >
                        <asp:Label ID ="lblPosterName" Text ='<%#Eval("posterName") %>' runat ="server" ></asp:Label>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date">
                    <ItemTemplate >
                        <%#Eval("dateTim") %>
                    </ItemTemplate>
                    <EditItemTemplate >
                        <asp:Label ID ="lblDateTime" Text ='<%#Eval("dateTim") %>' runat ="server" ></asp:Label>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

私のコードは.....

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            Int64 forumId = (Int64)GridView1.SelectedValue;
            Session["forumId"] = forumId;
            Response.Redirect("Thread.aspx");
        }
        catch (Exception)
        {

            throw;
        }
    }
4

6 に答える 6

2
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        Int64 forumId = Convert.ToInt64(GridView1.SelectedRow.Cells[1].Text);
        Session["forumId"] = forumId;
        Response.Redirect("Thread.aspx");
    }
    catch (Exception)
    {

        throw;
    }
}
于 2013-07-09T05:34:13.323 に答える
0

Datakey タブで設定されるように、グリッドビューで一意の列名を指定する必要があります。

そこから、ビハインド ページ コードで _selectedIndexChanged メソッドを呼び出す必要があります。

于 2013-10-30T03:25:41.623 に答える
0

page.cs コードで gridview select イベントを使用していない場合は、gridview のページの aspx コードから OnSelectedIndexChanged="GridView1_SelectedIndexChanged" を削除するだけです。

于 2015-02-22T18:49:36.617 に答える