I have a table called Documents and a GridView where I display some of its info. Inside an ItemTemplate I have put buttons as a rating system. What I want to do is to get the DocumentID from the Documents table and use it as ID in a table called RatingTable with DocumentID as primary and foreign key, and rating as a tinyint.
我有一个名为Documents和GridView的表,我在其中显示了一些信息。在ItemTemplate中,我将按钮作为评级系统。我想要做的是从Documents表中获取DocumentID并将其用作名为RatingTable的表中的ID,其中DocumentID作为主键和外键,并且评级为tinyint。
Here is the code for the gridview:
这是gridview的代码:
<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>
Here is the code behind where I try to insert the value of the rating into the RatingTable:
以下是我尝试将评级值插入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);
}
Here is what the RatingTable tools like:
这是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])
);
What I'm trying to do is when I press one of the ImageButtons in the TemplateField "Rating", I want it to get the current DocumentID for the row I'm in, and use this to insert it into the RatingTable as a primary key with the value of the star clicked. In the updaterating function in the code behind, I have commented out some lines where I tried to use DataKeyNames in the GridView to get the ID, but that gives an empty GridView.
我想要做的是当我按下TemplateField“Rating”中的一个ImageButtons时,我希望它获取我所在行的当前DocumentID,并使用它将它作为主要内容插入到RatingTable中点击星号值的键。在后面的代码中的更新函数中,我已经注释掉了一些行,我尝试在GridView中使用DataKeyNames来获取ID,但这给出了一个空的GridView。
if (!IsPostBack)
{
SearchView.DataKeyNames = new string[]{"DocumentID"};
BindGrid();
}
I added this to try to fetch the DataKeyNames right before I bind the grid, but the GridView is still empty.
我添加了这个以尝试在绑定网格之前获取DataKeyNames,但GridView仍然是空的。
I also changed the string value because I was using the wrong attribute:
我还更改了字符串值,因为我使用了错误的属性:
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);
}
Any help is much appreciated!
任何帮助深表感谢!
1 个解决方案
#1
1
You need to add a DataKeyName
to your GridView. If I understand the question correctly, that would be DocumentID
.
您需要将DataKeyName添加到GridView。如果我正确理解了这个问题,那就是DocumentID。
<asp:GridView ID="SearchView" runat="server"
AutoGenerateColumns="false"
OnSelectedIndexChanged="SearchView_SelectedIndexChanged"
ShowHeaderWhenEmpty="true"
EmptyDataText="Ingen oppføringer"
DataKeyNames="DocumentID">
Then when you run this line:
然后当你运行这一行:
string value = SearchView.SelectedDataKey.Value.ToString();
The GridView actually knows what the DataKey
is. Since you are using SelectedDataKey
, there also must be a selected row for this property to have a value.
GridView实际上知道DataKey是什么。由于您使用的是SelectedDataKey,因此此属性的选定行也必须具有值。
To find the index of the row when it is not selected, you need to find the row that the button that was clicked is contained within.
要在未选择行时查找行的索引,您需要找到包含该按钮的行。
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);
}
#1
1
You need to add a DataKeyName
to your GridView. If I understand the question correctly, that would be DocumentID
.
您需要将DataKeyName添加到GridView。如果我正确理解了这个问题,那就是DocumentID。
<asp:GridView ID="SearchView" runat="server"
AutoGenerateColumns="false"
OnSelectedIndexChanged="SearchView_SelectedIndexChanged"
ShowHeaderWhenEmpty="true"
EmptyDataText="Ingen oppføringer"
DataKeyNames="DocumentID">
Then when you run this line:
然后当你运行这一行:
string value = SearchView.SelectedDataKey.Value.ToString();
The GridView actually knows what the DataKey
is. Since you are using SelectedDataKey
, there also must be a selected row for this property to have a value.
GridView实际上知道DataKey是什么。由于您使用的是SelectedDataKey,因此此属性的选定行也必须具有值。
To find the index of the row when it is not selected, you need to find the row that the button that was clicked is contained within.
要在未选择行时查找行的索引,您需要找到包含该按钮的行。
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);
}