如何在选定的索引更改属性上找到gridview的datakey值?

时间:2013-07-09 05:16:31

标签: c# asp.net

我的gridview是这样的,但是当我选择查看按钮以查找所选索引的主键值列时,我收到错误。请帮我解决这个问题。

<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;
        }
    }

6 个答案:

答案 0 :(得分:6)

首先,您必须在网格视图声明中定义要创建datakey的字段的字段名称。例如,如果你想要“forumId”datakey.than

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
DataKeyNames="forumId">

而且你可以这样访问

int intforumid = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);

答案 1 :(得分: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;
    }
}

答案 2 :(得分:0)

您可以将DataKeyNames设置为forumId,如下所示

<asp:GridView ID="GridView1" runat="server" DataKeyNames = "forumId" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">

由于您尚未在当前解决方案中提供任何数据密钥名称GridView1.SelectedValue将不包含您期望的值

答案 3 :(得分:0)

您似乎只需要将DataKeyNames属性设置为forumId;

<asp:GridView DataKeyNames = "forumId" ...

答案 4 :(得分:0)

您需要在Gridview中指定要在Datakey选项卡下设置的唯一列名称。

从那里,您需要在后台代码上调用_selectedIndexChanged方法。

答案 5 :(得分:0)

如果你没有在page.cs代码中使用gridview select事件,那么你只需删除OnSelectedIndexChanged =&#34; GridView1_SelectedIndexChanged&#34;来自gridview页面的aspx代码。