当前位置:编程学习 > C#/ASP.NET >>

Asp.net中单击GridView中的某一行,实现页面的跳转 求助

各位大侠,我第一次做论坛的时候,想在GridView显示的帖子列表信息中(帖子编号、帖子标题、发帖人、发帖日期等字段),单击某一行中的帖子标题,发生页面跳转,并且将帖子编号获取到传到跳转的那个页面,实现帖子的浏览,
那位大侠刚刚小弟,再此谢过! --------------------编程问答-------------------- 很简单啊
前台页面gridview里面设置
<asp:ButtonField Text="选择" DataTextField="数据库字段" HeaderText="标题" CommandName="Select">
</asp:ButtonField>

后台
protected void GridView1_RowCommand(object source, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            int i = Convert.ToInt16(e.CommandArgument.ToString());
            string ID = this.GridView1.Rows[i].Cells[0].Text;
            Response.Write("<script language=javascript>parent.location.href='跳转页面.aspx?获取参数=" + ID + "';</script>");
        }
    } --------------------编程问答-------------------- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("ondblclick", "window.open('default1.aspx','_self','')");
        }
    }
或使用 hyperlink --------------------编程问答-------------------- 帖子编号转成模板列,编辑模板列,分别把梯子编号和标题绑定到ToolTip和Text,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
        if (e.Row.RowType == DataControlRowType.DataRow) 
        { 
            Label tiezi=e.Row.FindControl("lbl_tiezi") as Label;
            string link="jumppage.aspx?id="+tiezi.ToolTip.trim();
            e.Row.Attributes.Add("onclick", "window.open(link,'_self','')"); 
        } 
    } 
--------------------编程问答-------------------- 用hyperlink绑定就行了.有这么麻烦吗. --------------------编程问答--------------------

   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField HeaderText="编号" />
            <asp:TemplateField HeaderText="标题">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" 
                        Text='<%# Eval("Coursename") %>' onclick="LinkButton1_Click"  ></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="发帖人" />
            <asp:BoundField HeaderText="发贴日期" />
        </Columns>
    </asp:GridView>





 protected void LinkButton1_Click(object sender, EventArgs e)
    {
        GridViewRow row = ((LinkButton)sender).Parent.Parent as GridViewRow;

        Response.Redirect("***.aspx?id=" + row.Cells[0].Text);

       
    }

--------------------编程问答-------------------- 都可行的 --------------------编程问答--------------------


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("ondblclick", "window.open('ParticularInfo.aspx?id=" + e.Row.Cells[0].Text + "')");//双击行打开新页
            ((LinkButton)(e.Row.Cells[3].Controls[0])).Attributes.Add("onclick","return confirm('确定要删除吗?')");//确定删除消息提示
            e.Row.Cells[2].Text = DateTime.Parse(e.Row.Cells[2].Text).ToString("yyyy-MM-dd");//格式化日期
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");//鼠标随行而变色
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"" + "#9FACCF" + "\"");//鼠标随行而变色
        }
    }
--------------------编程问答-------------------- 路过。。。。。。。。。。 --------------------编程问答--------------------
引用 4 楼 igelf 的回复:
用hyperlink绑定就行了.有这么麻烦吗.

同意做论坛的话添加个hyperlink字段不就行了?干嘛这么麻烦? --------------------编程问答-------------------- 路过学习了  --------------------编程问答-------------------- 我是用JS处理的。
写了一个JS 函数


function redirect(url)
    {
        window.location.href=url;
    }

GridView1_RowDataBound增加如下
                 e.Row.Attributes.Add("onclick", @"return redirect('YURE URL')");
--------------------编程问答-------------------- 单纯的路过 --------------------编程问答-------------------- 直接 用Hyplink多简单额 或者转成编辑列 --------------------编程问答-------------------- http://topic.csdn.net/u/20100412/23/04da12fb-54be-4dca-81b4-3e490e7d866d.html
例子 --------------------编程问答--------------------
引用 13 楼 lijing5916 的回复:
直接 用Hyplink多简单额 或者转成编辑列

up --------------------编程问答-------------------- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
        <Columns>
            <asp:BoundField HeaderText="编号" />
            <asp:TemplateField HeaderText="标题">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" 
                     CommandArgument='<%#Eval("id") %>' CommandName="这里随便起个名,如course"   Text='<%# Eval("Coursename") %>' ></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="发帖人" />
            <asp:BoundField HeaderText="发贴日期" />
        </Columns>
    </asp:GridView>
后台:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("course"))
        {
            int id = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("xxxx.aspx?id=" + id);
         }                   
--------------------编程问答-------------------- 怎么用GridView啊?用DataList多简单啊?编号、帖子标题、发帖人、发帖日期
<asp:DataList ID="DataList" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>编号</td>
<td>标题</td>
<td>发帖人</td>
<td>日期</td>
</tr>
</HeaderTemplate>
<tr>
<td><% Eval("ID")></td>
<td><a href="aaa.aspx?id=<% Eval("ID")>"><% Eval("title")></td>
<td><% Eval("Name")></td>
<td><% Eval("time")></td>
</tr>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>

不知道这样行不? --------------------编程问答-------------------- 谢谢,我知道了谢谢, --------------------编程问答--------------------
引用 13 楼 lijing5916 的回复:
直接 用Hyplink多简单额 或者转成编辑列


up.... --------------------编程问答--------------------
引用 1 楼 irisene 的回复:
很简单啊
前台页面gridview里面设置
<asp:ButtonField Text="选择" DataTextField="数据库字段" HeaderText="标题" CommandName="Select">
</asp:ButtonField>

后台
protected void GridView1_RowCommand(object source, GridViewCom……



我都这样用的、、、 --------------------编程问答-------------------- 不要写服务器端事件,慢,还刷屏,在前台页面加链接我看行。 --------------------编程问答--------------------
引用 2 楼 wuyq11 的回复:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("ondblclick"……
--------------------编程问答-------------------- 该结贴了
--------------------编程问答-------------------- 接分。。 --------------------编程问答-------------------- hehe~~~~~`` --------------------编程问答-------------------- 上面有答案了! --------------------编程问答-------------------- 关注中
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,