【问题标题】:hyperlink in grid view not populating values in another page when clicked单击时网格视图中的超链接不会填充另一个页面中的值
【发布时间】:2013-07-01 07:06:21
【问题描述】:

请帮助解决这个超链接的事情。我有一个网格视图,其中一列包含超链接,比如 ViewDetails。单击 ViewDetails 后,它应该导航到另一个名为 Reports.aspx 的页面,该页面应在标签的网格视图中显示所选列的网格行值。我使用了行数据绑定事件。如果我单击超链接,我会得到空白标签。我已经编写了存储过程来获取行值。我通过 KEY 调用它。它的 TaskID 是一个自动生成的不可见列。根据我需要显示行值的键值。

这里是代码

           <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="TaskID" HeaderText="SL No" Visible="False" ReadOnly="True">
          <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" Width="70px" ForeColor="#0061C1" Text="ViewDetails" NavigateUrl="Reports.aspx">View</asp:HyperLink>
          </ItemTemplate>
          <FooterStyle BackColor="#0061C1" />
          <HeaderStyle BackColor="#0061C1" ForeColor="White" />
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        </asp:TemplateField>
        </Columns>
     </asp:GridView>

aspx.cs 代码

    protected void GrdViewMyTasks_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        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.cs 中的代码

         protected void Page_Load(object sender, EventArgs e)
       {
        MTMSService obj = new MTMSService();
        DBAccess db = new DBAccess();
        {
            MTMSDTO objc = new MTMSDTO();
            {

                //objc.TaskID = Convert.ToInt32(Request.QueryString["TaskID"]);//
                DataSet rep = obj.GetReports();
                DataView Rprts = new DataView();
                Rprts.Table = rep.Tables[0];

                var table = rep.Tables[0];

                if (table.Rows.Count > 0)
                {
                    LblTaskID.Text = rep.Tables[0].Rows[0]["TaskID"].ToString();
                    LblTaskName.Text = rep.Tables[0].Rows[0]["TaskName"].ToString();
                    LblDueDate.Text = rep.Tables[0].Rows[0]["DueDate"].ToString();
                    LblDescription.Text = rep.Tables[0].Rows[0]["Description"].ToString();
                    LblAssignBy.Text = rep.Tables[0].Rows[0]["AssignBy"].ToString();
                    LblStatus.Text = rep.Tables[0].Rows[0]["Status"].ToString();
                    LblPercentageComplete.Text = rep.Tables[0].Rows[0]["PercentageComplete"].ToString();
                }

                else
                {

                }


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

        From dbo.Task

        Where TaskID = @TaskID;

请让我知道我哪里出错了。从过去的 1 周开始,我无法得到完美的解决方案……这让我很头疼……

reports.cs 文件的注释行出现错误“输入字符串格式不正确”

【问题讨论】:

  • 确保在导航到 Url 时,Url 在查询字符串中具有值。如果没有,那么调试“e.Row.Cells[0].Text”这是否给你正确的 ID。
  • 是的 .. 它给出了正确的 ID

标签: c# gridview stored-procedures hyperlink


【解决方案1】:

您正在使用 Session["TaskId"] 查询数据库,并且您正在从查询字符串的 UI 上显示任务 ID。你确定两者是一样的吗?

我认为您可能希望使用来自查询字符串而不是会话的 taks id。

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

更新 -

根据您的查询字符串过滤表

rep.Tables[0].Select(string.Format("TaskID={0}", Request.QueryString["TaskID"]));

【讨论】:

  • 我使用会话而不是 f 查询字符串,然后我将超链接代码更改为 (NavigateUrl='' >)然后我收到此错误(“DataBinding:'System.Data.DataRowView' 不包含名为'TaskID'的属性。”)
  • 我现在在 d Reports 页面中获得输出,但是如果我点击 2nd r 3rd task 它只显示第 1 个任务..请帮助我...prblm 是我在reports.aspx 中的代码。 cs ...我已经更新了我的线程,请通过它...
  • 为什么你不使用查询字符串值而不是会话?尝试一次它必须工作。
  • 我试过了,但它不是 wrkin... nehw 我正在获取值 nw 但我猜 thrs prblm wid count 部分.. 请通过它我已经更新了我的帖子 nw
  • thnks everybdy ... i gt d 解决方案... 在我的网格视图 SP 中缺少 taskid 列名称
猜你喜欢
  • 2018-05-06
  • 1970-01-01
  • 2013-12-17
  • 1970-01-01
  • 2022-11-30
  • 2016-11-28
  • 1970-01-01
  • 2011-08-31
  • 2012-11-14
相关资源
最近更新 更多