在 GridView 中获取当前 ID,并在单击按钮时将其作为 ID 插入到另一个 table 中

Get current id in GridView and insert it as id in another table on button click

我有一个名为 Documents 的 table 和一个用于显示其部分信息的 GridView。在 ItemTemplate 中,我将按钮作为评级系统。我想要做的是从 Documents table 中获取 DocumentID,并将其用作名为 RatingTable 的 table 中的 ID,其中 DocumentID 作为主键和外键,并将评级作为 tinyint。

这是网格视图的代码:

<asp:GridView ID="SearchView" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="SearchView_SelectedIndexChanged" ShowHeaderWhenEmpty="true" EmptyDataText="Ingen oppføringer">
        <Columns>
            <asp:BoundField DataField="FileName" HeaderText="Filnavn" />
            <asp:BoundField DataField="ReportCount" HeaderText="Antall rapporteringer" />
            <asp:BoundField DataField="Tags" HeaderText="Tags" />
            <asp:BoundField DataField="CourseCode" HeaderText="Fagkode" />
            <asp:TemplateField HeaderText="Rating">
                <ItemTemplate>
                    <asp:ImageButton class="myButtonLink" ImageUrl="~/Images/empty_star.png" ID="rate1" runat="server" OnClick="rate1_Click"/>
                    <asp:ImageButton class="myButtonLink" ImageUrl="~/Images/empty_star.png" ID="rate2" runat="server" OnClick="rate2_Click"/>
                    <asp:ImageButton class="myButtonLink" ImageUrl="~/Images/empty_star.png" ID="rate3" runat="server" OnClick="rate3_Click"/>
                    <asp:ImageButton class="myButtonLink" ImageUrl="~/Images/empty_star.png" ID="rate4" runat="server" OnClick="rate4_Click"/>
                    <asp:ImageButton class="myButtonLink" ImageUrl="~/Images/empty_star.png" ID="rate5" runat="server" OnClick="rate5_Click"/>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Nedlastingslink">
                <ItemTemplate>
                    <asp:LinkButton ID="downloadbutton" runat="server" Text="Last ned" OnClick="Download_Click"
                        CommandArgument='<%# Eval("FileName") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Rapporter" ItemStyle-Font-Size="Small">
                <ItemTemplate>
                    <asp:LinkButton ID="reportbutton" runat="server" Text="Rapporter dokument" OnClick="Report_Click"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

下面是我尝试将评分值插入 RatingTable 的代码:

protected void updaterating(int rating)
    {
        //string value = SearchView.SelectedDataKey.Value.ToString();
        using (SqlConnection con = new SqlConnection(connectionstring))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO RatingTable (DocumentID, Rating) VALUES(@DocumentID, @Rating)"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.Text;
                    //cmd.Parameters.AddWithValue("@DocumentID", value);
                    cmd.Parameters.AddWithValue("@Rating", rating);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
        Response.Redirect(Request.Url.AbsoluteUri);
    }

    protected void rate1_Click(object sender, ImageClickEventArgs e)
    {
        updaterating(1);
    }

    protected void rate2_Click(object sender, ImageClickEventArgs e)
    {
        updaterating(2);
    }

    protected void rate3_Click(object sender, ImageClickEventArgs e)
    {
        updaterating(3);
    }

    protected void rate4_Click(object sender, ImageClickEventArgs e)
    {
        updaterating(4);
    }

    protected void rate5_Click(object sender, ImageClickEventArgs e)
    {
        updaterating(5);
    }

RatingTable 工具如下所示:

[RatingTable] (
[DocumentID] INT     NOT NULL,
[Rating]     TINYINT NOT NULL,
PRIMARY KEY CLUSTERED ([DocumentID] ASC),
CONSTRAINT [FK_RatingTable_Documents] FOREIGN KEY ([DocumentID]) REFERENCES [dbo].[Documents] ([DocumentId])

);

我想要做的是,当我按下 TemplateField "Rating" 中的一个 ImageButtons 时,我希望它获取我所在行的当前 DocumentID,并使用它来插入它作为主键放入 RatingTable 中,带有单击的星的值。在后面代码的更新函数中,我注释掉了一些我试图在 GridView 中使用 DataKeyNames 来获取 ID 的行,但是这给出了一个空的 GridView。

 if (!IsPostBack)
        {
            SearchView.DataKeyNames = new string[]{"DocumentID"};
            BindGrid();
        }

我添加这个是为了在绑定网格之前尝试获取 DataKeyName,但 GridView 仍然是空的。

我还更改了字符串值,因为我使用了错误的属性:

protected void updaterating(int rating)
    {
        string value = SearchView.DataKeyNames.ToString();
        using (SqlConnection con = new SqlConnection(connectionstring))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO RatingTable (DocumentID, Rating) VALUES(@DocumentID, @Rating)"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@DocumentID", value);
                    cmd.Parameters.AddWithValue("@Rating", rating);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
        Response.Redirect(Request.Url.AbsoluteUri);
    }

非常感谢任何帮助!

您需要向您的 GridView 添加一个 DataKeyName。如果我正确理解这个问题,那就是 DocumentID.

<asp:GridView ID="SearchView" runat="server" 
    AutoGenerateColumns="false" 
    OnSelectedIndexChanged="SearchView_SelectedIndexChanged" 
    ShowHeaderWhenEmpty="true" 
    EmptyDataText="Ingen oppføringer"
    DataKeyNames="DocumentID">

那么当你 运行 这一行时:

string value = SearchView.SelectedDataKey.Value.ToString();

GridView 实际上知道 DataKey 是什么。由于您使用的是 SelectedDataKey,因此还必须为此 属性 选择一个行才能具有值。

要查找未选中行的索引,需要找到被单击的按钮所在的行。

protected void rate1_Click(object sender, ImageClickEventArgs e)
{
    ImageButton rate1 = (ImageButton)sender;
    GridViewRow row = (GridViewRow)rate1.NamingContainer;
    DataKey key = SearchView.DataKeys[row.DataItemIndex];

    // Now you have your key, use it how you'd like.
    // This may mean passing it as another variable
    // to your updaterating method.

    updaterating(1);
}