c# - GridView 行包含超链接,单击它后应在另一个页面中显示该特定行的值

标签 c# asp.net gridview hyperlink

我有一个 GridView ,其中一列中包含超链接(称为 View 详细信息),单击该超链接后,该 GridView 的特定行的详细信息应显示在另一个页面(reports.aspx)的标签中。

请帮帮我。到目前为止我已经尝试过了。

这是我的 GridView

       <asp:GridView ID="GrdViewMyTasks" runat="server" AllowSorting="True" 
            AutoGenerateColumns="False" BackColor="White" BorderColor="#0061C1" 
              BorderStyle="None" CaptionAlign="Bottom" EmptyDataText="No Records Found" 
                Font-Names="Verdana" Font-Size="X-Small" ForeColor="#0061C1" 
                  Height="179px" OnRowDataBound="GrdViewMyTasks_RowDataBound" 
                      ShowFooter="True" ShowHeaderWhenEmpty="True" Width="99%"  onselectedindexchanged="GrdViewMyTasks_SelectedIndexChanged" OnRowCreated="GrdViewMyTasks_RowCreated" >

                           <Columns>
                            <asp:BoundField DataField="Gen_ID" HeaderText="SL No"                ReadOnly="True" Visible="False">
                           <FooterStyle BackColor="#0061C1" />
                            <HeaderStyle BackColor="#0061C1" HorizontalAlign="Center"  VerticalAlign="Middle" />
                           <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                            </asp:BoundField>
                           <asp:TemplateField HeaderText="Task Name">
                            <ItemTemplate>
                                  <asp:Label ID="TaskName" runat="server"  
                              Font-Names="Verdana" Font-Size="X-Small" Height="24px" 
                               Text='<%# Eval("TaskName")%>' Width="70px"></asp:Label>
                      </ItemTemplate>
                            <FooterStyle BackColor="#0061C1" />
                                <HeaderStyle BackColor="#0061C1" ForeColor="White" />
                       <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                   </asp:TemplateField>

                                  <asp:TemplateField HeaderText="Due Date">
                                    <ItemTemplate>
                                          <asp:Label ID="DueDate" runat="server"  
                                         Font-Names="Verdana" Font-Size="X-Small" 
                               Height="20px" Width="70px" Text='<%# Eval("DueDate"," {0:dd/MM/yyyy}")%>' DataFormatString="{0:dd/MM/yyyy}"></asp:Label>
                             </ItemTemplate>
                            <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White" />
                      <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                  </asp:TemplateField>
                <asp:TemplateField HeaderText="Description">
                 <ItemTemplate>
                           <asp:Label ID="Description" runat="server"  
                  Font-Names="Verdana" Font-Size="X-Small" Height="20px" Width="90px"        Text='<%# Eval("Description")%>'></asp:Label>
                         </ItemTemplate>
                      <FooterStyle BackColor="#0061C1" />
                   <HeaderStyle BackColor="#0061C1" ForeColor="White" />
                <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                   </asp:TemplateField>
                   <asp:TemplateField HeaderText="Assign By">
                      <ItemTemplate>
                           <asp:Label ID="AssignBy" runat="server"  
           Font-Names="Verdana" Font-Size="X-Small" Height="20px" Width="90px" Text='<%# Eval("AssignBy")%>'></asp:Label>
                         </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                       <HeaderStyle BackColor="#0061C1" ForeColor="White" />
                 <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                 </asp:TemplateField>
               <asp:TemplateField HeaderText="Status">
                      <ItemTemplate>
                  <asp:Label ID="Status" runat="server"  
                   Font-Names="Verdana" Font-Size="X-Small" Height="20px" Width="90px"    Text='<%# Eval("Status")%>'></asp:Label>
                      </ItemTemplate>
                     <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White" />
                     <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                     </asp:TemplateField>
                            <asp:TemplateField HeaderText="% Complete">
                                <ItemTemplate>
                           <asp:Label ID="PercentageComplete" runat="server"  
                                       Font-Names="Verdana" Font-Size="X-Small"  
                   Height="20px" Width="50px" Text='<%# Eval("PercentageComplete")%>'>                </asp:Label>
                 </ItemTemplate>
                      <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White" />
                     <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                          </asp:TemplateField>
                       <asp:TemplateField HeaderText="View Details">
                         <ItemTemplate>
                             <asp:HyperLink ID="ViewDetails" runat="server" Font- Names="Verdana" Font-Size="X-Small" Height="24px" Text="View" Width="70px" NavigateUrl="Reports.aspx" ForeColor="#0061C1" DataTexField="ID"  DataNavigateUrlFields="TaskID" DataNavigateUrlFormatString="Reports.aspx?TaskID={0}">View</asp:HyperLink>
                               </ItemTemplate>
                            <FooterStyle BackColor="#0061C1" />
                       <HeaderStyle BackColor="#0061C1" ForeColor="White" />
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                      </asp:TemplateField>
                              </Columns>
                         </asp:GridView>

这是我在行数据绑定(bind)上的 GridView 背后的代码

       if (e.Row.RowType == DataControlRowType.DataRow)
           {
            HyperLink ViewDetails = e.Row.FindControl("ViewDetails") as HyperLink;
            ViewDetails.NavigateUrl = "Reports.aspx?TaskID=" + e.Row.Cells[0].Text;
           }

这是我的代码或reports.aspx,其中应显示 GridView 的详细信息

       protected void Page_Load(object sender, EventArgs e)
         {
             MTMSService obj = new MTMSService();
             DBAccess db = new DBAccess();
           {
                MTMSDTO objc = new MTMSDTO();
              {
                objc.TaskID = Convert.ToInt32(Session["TaskID"]);
                DataSet rep = obj.GetReports(objc);
                DataView Rprts = new DataView();
                Rprts.Table = rep.Tables[0];


                LblTaskName.Visible = true;
                LblAssignBy.Visible = true;
                LblDescription.Visible = true;
                LblDueDate.Visible = true;
                LblStatus.Visible = true;
                LblPercentageComplete.Visible = true;
                LblAssignTo.Visible = false;
            }
        }
    }

存储过程

              ALTER PROCEDURE [dbo].[GetReports]

              @TaskID int

              AS

             Select TaskName, DueDate, Description, AssignBy, Status, PercentageComplete

             From dbo.Task

             Where TaskID = @TaskID;

最佳答案

替换这个

objc.TaskID = Convert.ToInt32(Session["TaskID"]); 

objc.TaskID = Convert.ToInt32(Request.QueryString["TaskID"].tostring());

关于c# - GridView 行包含超链接,单击它后应在另一个页面中显示该特定行的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17320630/

相关文章:

c# - 如何只刷新 HTML 页面的特定部分?

c# - 将字符串返回给有关搜索的 SQL 服务器查询

c# list<int> 如何在两个值之间插入一个新值

c# - 使用 "Linq Methods"处理 DateTime

javascript - 选中复选框后如何插入今天的日期?

android - GridView 文本在 Android 中滚动时消失

android - 如何自定义 Leanback 的 ListRows?

c# - 带动态列的 GridView - 显示 html

c# - "Run as administrator"选项没有出现在资源管理器的上下文菜单中

具有缩进嵌套标记的 C# XML 反序列化