【问题标题】:Gridview hyperlinkGridview 超链接
【发布时间】:2013-02-25 06:00:43
【问题描述】:

我的第一页:

.aspx

<asp:GridView ID="GridView1" runat="server" BackColor="White" 
       BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
       CssClass="grdDataGrid" Height="102px" onrowdatabound="GridView1_RowDataBound"> 
   <RowStyle ForeColor="#000066" /> <FooterStyle BackColor="White" ForeColor="#000066" />
   <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
   <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
   <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> 
</asp:GridView>

后面的代码

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var firstCell = e.Row.Cells[0];
        firstCell.Controls.Clear();
        firstCell.Controls.Add(new HyperLink { NavigateUrl = "ser_job_status1.aspx?Complaint_No = " + firstCell.Text, Text = firstCell.Text, Target = "_blank" });
        Session["Complaint_No"] = firstCell.Text;
        //////Session["Complaint_No"] = GridView1.Rows[e.RowIndex].Cells[HyperLink(NavigateUrl)].Value.ToString();

      }
 }
GridView1.DataBind();

我的第二页:

protected void Page_Load(object sender, EventArgs e)
{     
   string strComplaintNo = Convert.ToString(Session["Complaint_No"]);
   TextBox51.Text = strComplaintNo;
}

我的问题是因为我在将数据源绑定到 Gridview 之前使用了超链接我的 firstCell。
文本值保存最后获取的数据。
因此,如果我单击该链接,我的 sessoin 将获得 firstCell 的值。
又是最后获取值的文本。但我的要求是获取超链接值。。
谁能帮我解决这个问题 我使用 C# 作为我的代码...

【问题讨论】:

  • 你想在哪里使用这个值?

标签: asp.net gridview


【解决方案1】:

根据行索引在每行中为每个超链接指定一个 id,

HyperLink HLLink = GridView1.Rows[e.RowIndex].FindControl("HyperLink"+e.RowIndex) as  HyperLink;

Session["Complaint_No"]=HLLink.NavigateURL.ValueToString();

【讨论】:

    【解决方案2】:

    这里是解决方案。
    当您通过查询字符串将值发送到下一页时。
    您可以在那里设置值。

    protected void Page_Load(object sender, EventArgs e)
    {
        string strComplaintNo = Request.QueryString.Get("Complaint_No");
        Session["Complaint_No"]=strComplaintNo ;
        TextBox51.Text = strComplaintNo ;
    }
    

    编辑 1

    <asp:TemplateField HeaderText="Request No.">
       <ItemTemplate>
           <asp:HyperLink ID="EditHyperLink1" runat="server" 
                NavigateUrl='<%#"ser_job_status1.aspx?reqid=" + Eval("ReqId") %>'
                Text='<%# Eval("ReqId") %>' >
           <!--change the column name "ReqId"-->
           </asp:HyperLink>
       </ItemTemplate>
    </asp:TemplateField>
    

    在您的 ser_job_status1.aspx 页面上

    protected void Page_Load(object sender, EventArgs e)
    {
        string strComplaintNo = Request.QueryString.Get("reqid");
        //call your method on the basis of strComplaintNo
       // Session["Complaint_No"]=strComplaintNo ;
        //TextBox51.Text = strComplaintNo ;
    }
    

    【讨论】:

    • Mr.Shekhar....和我对 session 做的一样..这行得通...我的问题是从第一页获取值本身是一个问题...因为 rowdatabound 事件会获取所有数据,包括超链接详细信息,最后将其绑定到 gridview,因此最后获取的值作为会话值传递...
    • @Sathya 没关系,但是你想做什么会话值?
    • 将该值作为输入将填充第二页中的其他字段。例如:将投诉号作为输入,我必须填充投诉日期、状态等。
    • 如果您可以从查询字符串中获得会话,我不明白为什么要使用会话。
    • 我正在使用超链接方法更新我的示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 2017-11-28
    • 2013-07-14
    相关资源
    最近更新 更多