2013-07-09 5 views
6

Gridview của tôi là như thế này nhưng tôi nhận được lỗi khi tôi chọn nút xem để tìm cột giá trị khóa chính trên chỉ mục đã chọn thay đổi. Xin hãy giúp tôi giải quyết vấn đề.Làm cách nào để tìm giá trị datakey của GridView trên thuộc tính đã thay đổi chỉ mục đã chọn?

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

mã của tôi là .....

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

      throw; 
     } 
    } 
+0

Lỗi là gì? –

+0

lỗi sau đã xảy ra .................................. Khóa dữ liệu phải được chỉ định trên GridView 'GridView1' trước khi có thể truy xuất các khóa dữ liệu đã chọn. Sử dụng thuộc tính DataKeyNames để chỉ định các khóa dữ liệu. –

Trả lời

6

Đầu tiên bạn hav e để xác định tên trường trong khai báo khung lưới mà trường bạn muốn tạo datakey. ví dụ nếu bạn muốn "forumId" datakey.than

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

và hơn bạn có thể truy cập theo cách này

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

Thnq vấn đề của tôi được giải quyết ..... thnx alotttt –

0

bạn có thể đặt DataKeyNames như forumId như dưới đây

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

Vì bạn đã không đưa ra bất cứ dữ liệu tên chủ chốt trong giải pháp hiện tại GridView1.SelectedValue sẽ không chứa giá trị bạn mong đợi

0

Hình như bạn chỉ cần thiết lập DataKeyNames tài sản để forumId tương tự;

<asp:GridView DataKeyNames = "forumId" ... 
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; 
    } 
} 
0

Bạn sẽ cần phải chỉ định tên cột duy nhất trong GridView được thiết lập dưới tab Datakey.

Từ đó, bạn cần phải gọi phương thức _selectedIndexChanged trên mã trang sau.

0

Nếu bạn không sử dụng sự kiện chọn GridView trong mã page.cs của mình thì bạn chỉ cần xóa OnSelectedIndexChanged = "GridView1_SelectedIndexChanged" khỏi mã aspx của trang của GridView.